Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

Auth fix (#624)

Danny Avilaf5672ddcf888
diff --git a/.eslintrc.js b/.eslintrc.js
index 3dddeb8ea5..72ed417d5f 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -13,6 +13,9 @@ module.exports = {
'plugin:jest/recommended',
'prettier'
],
+ ignorePatterns: [
+ 'packages/data-provider/types/**/*',
+ ],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
diff --git a/client/src/components/Auth/Login.tsx b/client/src/components/Auth/Login.tsx
index 2999c6a220..5e7562bbcf 100644
--- a/client/src/components/Auth/Login.tsx
+++ b/client/src/components/Auth/Login.tsx
@@ -19,7 +19,7 @@ function Login() {
useEffect(() => {
if (isAuthenticated) {
- navigate('/chat/new');
+ navigate('/chat/new', { replace: true });
}
}, [isAuthenticated, navigate]);
diff --git a/client/src/components/Input/SubmitButton.jsx b/client/src/components/Input/SubmitButton.jsx
index 6f80277ff8..44c892d64e 100644
--- a/client/src/components/Input/SubmitButton.jsx
+++ b/client/src/components/Input/SubmitButton.jsx
@@ -16,6 +16,7 @@ export default function SubmitButton({
const { getToken } = store.useToken(endpoint);
const isTokenProvided = endpointsConfig?.[endpoint]?.userProvide ? !!getToken() : true;
+ const endpointsToHideSetTokens = new Set(['openAI', 'azureOpenAI', 'bingAI']);
const clickHandler = (e) => {
e.preventDefault();
@@ -38,7 +39,7 @@ export default function SubmitButton({
</div>
</button>
);
- } else if (!isTokenProvided && (endpoint !== 'openAI' || endpoint !== 'azureOpenAI' )) {
+ } else if (!isTokenProvided && (!endpointsToHideSetTokens.has(endpoint))) {
return (
<>
<button
diff --git a/client/src/hooks/AuthContext.tsx b/client/src/hooks/AuthContext.tsx
index be3fc41c32..0ec496fe8a 100644
--- a/client/src/hooks/AuthContext.tsx
+++ b/client/src/hooks/AuthContext.tsx
@@ -83,7 +83,7 @@ const AuthContextProvider = ({
setTokenHeader(token);
setIsAuthenticated(isAuthenticated);
if (redirect) {
- navigate(redirect);
+ navigate(redirect, { replace: true });
}
},
[navigate]
@@ -101,7 +101,8 @@ const AuthContextProvider = ({
setUserContext({ token, isAuthenticated: true, user, redirect: '/chat/new' });
},
onError: (error) => {
- doSetError(error.message);
+ doSetError((error as Error).message);
+ navigate('/login', { replace: true });
}
});
};
@@ -122,7 +123,7 @@ const AuthContextProvider = ({
});
},
onError: (error) => {
- doSetError(error.message);
+ doSetError((error as Error).message);
}
});
};
@@ -131,9 +132,8 @@ const AuthContextProvider = ({
if (userQuery.data) {
setUser(userQuery.data);
} else if (userQuery.isError) {
- //@ts-ignore - userQuery.error is of type unknown
- doSetError(userQuery?.error.message);
- navigate('/login');
+ doSetError((userQuery?.error as Error).message);
+ navigate('/login', { replace: true });
}
if (error && isAuthenticated) {
doSetError(undefined);
@@ -143,7 +143,7 @@ const AuthContextProvider = ({
if (tokenFromCookie) {
setUserContext({ token: tokenFromCookie, isAuthenticated: true, user: userQuery.data });
} else {
- navigate('/login');
+ navigate('/login', { replace: true });
}
}
}, [
diff --git a/client/src/routes/Chat.jsx b/client/src/routes/Chat.jsx
index 9b6cef6d01..0bdf031e46 100644
--- a/client/src/routes/Chat.jsx
+++ b/client/src/routes/Chat.jsx
@@ -1,4 +1,5 @@
import { useState, useEffect } from 'react';
+import { useAuthContext } from '~/hooks/AuthContext';
import { useNavigate, useParams } from 'react-router-dom';
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
@@ -14,6 +15,7 @@ import {
} from '@librechat/data-provider';
export default function Chat() {
+ const { isAuthenticated } = useAuthContext();
const [shouldNavigate, setShouldNavigate] = useState(true);
const searchQuery = useRecoilValue(store.searchQuery);
const [conversation, setConversation] = useRecoilState(store.conversation);
@@ -29,6 +31,18 @@ export default function Chat() {
const getConversationMutation = useGetConversationByIdMutation(conversationId);
const { data: config } = useGetStartupConfig();
+ useEffect(() => {
+ let timeout = setTimeout(() => {
+ if (!isAuthenticated) {
+ navigate('/login', { replace: true });
+ }
+ }, 300)
+
+ return () => {
+ clearTimeout(timeout);
+ }
+ }, [isAuthenticated, navigate]);
+
useEffect(() => {
if (!isSubmitting && !shouldNavigate) {
setShouldNavigate(true);
@@ -41,7 +55,7 @@ export default function Chat() {
if (conversation === null && conversationId === 'new') {
newConversation();
setShouldNavigate(true);
- }
+ }
// No current conversation and conversationId exists
else if (conversation === null && conversationId) {
getConversationMutation.mutate(conversationId, {
@@ -53,24 +67,24 @@ export default function Chat() {
onError: (error) => {
console.error('Failed to fetch the conversation');
console.error(error);
- navigate(`/chat/new`);
+ navigate('/chat/new');
newConversation();
setShouldNavigate(true);
}
});
setMessages(null);
- }
+ }
// No current conversation and no conversationId
else if (conversation === null) {
- navigate(`/chat/new`);
+ navigate('/chat/new');
setShouldNavigate(true);
- }
+ }
// Current conversationId is 'search'
else if (conversation?.conversationId === 'search') {
navigate(`/search/${searchQuery}`);
setShouldNavigate(true);
- }
- // Conversation change and isSubmitting
+ }
+ // Conversation change and isSubmitting
else if (conversation?.conversationId !== conversationId && isSubmitting) {
setShouldNavigate(false);
}
@@ -101,6 +115,10 @@ export default function Chat() {
}
}, [messagesQuery.data, messagesQuery.isError, setMessages]);
+ if (!isAuthenticated) {
+ return null;
+ }
+
// if not a conversation
if (conversation?.conversationId === 'search') return null;
// if conversationId not match
diff --git a/client/src/routes/Root.jsx b/client/src/routes/Root.jsx
index 31bdae8025..a04403f835 100644
--- a/client/src/routes/Root.jsx
+++ b/client/src/routes/Root.jsx
@@ -23,7 +23,7 @@ export default function Root() {
const setIsSearchEnabled = useSetRecoilState(store.isSearchEnabled);
const setEndpointsConfig = useSetRecoilState(store.endpointsConfig);
const setPresets = useSetRecoilState(store.presets);
- const { user } = useAuthContext();
+ const { user, isAuthenticated } = useAuthContext();
const searchEnabledQuery = useGetSearchEnabledQuery();
const endpointsQuery = useGetEndpointsQuery();
@@ -57,6 +57,10 @@ export default function Root() {
}
}, [searchEnabledQuery.data, searchEnabledQuery.isError]);
+ if (!isAuthenticated) {
+ return null;
+ }
+
return (
<>
<div className="flex h-screen">
diff --git a/client/src/routes/Search.jsx b/client/src/routes/Search.jsx
index f91e7e224b..3a137c67c7 100644
--- a/client/src/routes/Search.jsx
+++ b/client/src/routes/Search.jsx
@@ -23,7 +23,7 @@ export default function Search() {
searchPlaceholderConversation();
setSearchQuery(query);
} else {
- navigate(`/chat/new`);
+ navigate('/chat/new');
}
} else if (conversation?.conversationId === 'search') {
// jump to search page
diff --git a/client/src/routes/index.jsx b/client/src/routes/index.jsx
index ffd04cc6b1..d4b236f8aa 100644
--- a/client/src/routes/index.jsx
+++ b/client/src/routes/index.jsx
@@ -16,7 +16,7 @@ const AuthLayout = () => (
export const router = createBrowserRouter([
{
path: 'register',
- element: <Registration />
+ element: <Registration />
},
{
path: 'forgot-password',
diff --git a/packages/data-provider/src/index.ts b/packages/data-provider/src/index.ts
index 10511d7bef..a292ca23cc 100644
--- a/packages/data-provider/src/index.ts
+++ b/packages/data-provider/src/index.ts
@@ -4,4 +4,4 @@ export * from './types';
export * from './react-query-service';
export * from './headers-helpers';
export * from './sse.mjs';
-export {default as createPayload} from './createPayload';
\ No newline at end of file
+export { default as createPayload } from './createPayload';
\ No newline at end of file
diff --git a/packages/data-provider/tsconfig.json b/packages/data-provider/tsconfig.json
index 203cda7ecd..a21afa2b87 100644
--- a/packages/data-provider/tsconfig.json
+++ b/packages/data-provider/tsconfig.json
@@ -23,5 +23,5 @@
}
},
"exclude": ["node_modules", "dist", "types"],
- "include": ["src/**/*"]
+ "include": ["src/**/*", "types/index.d.ts"]
}