mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-19 18:00:15 +01:00
🔒feat: Enable OpenID Auto-Redirect (#6066)
* added feature for oidc auto redirection * Added Cooldown logic for OIDC auto redirect for failed login attempts * 🔧 feat: Implement custom logout redirect handling and enhance OpenID auto-redirect logic * 🔧 refactor: Update getLoginError to use TranslationKeys for improved type safety * 🔧 feat: Localize redirect message to OpenID provider in Login component --------- Co-authored-by: Ruben Talstra <RubenTalstra1211@outlook.com>
This commit is contained in:
parent
09abce063f
commit
f95d5aaf4d
11 changed files with 102 additions and 17 deletions
|
|
@ -6,6 +6,7 @@ import {
|
|||
useContext,
|
||||
useCallback,
|
||||
createContext,
|
||||
useRef,
|
||||
} from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useRecoilState } from 'recoil';
|
||||
|
|
@ -35,6 +36,8 @@ const AuthContextProvider = ({
|
|||
const [token, setToken] = useState<string | undefined>(undefined);
|
||||
const [error, setError] = useState<string | undefined>(undefined);
|
||||
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(false);
|
||||
const logoutRedirectRef = useRef<string | undefined>(undefined);
|
||||
|
||||
const { data: userRole = null } = useGetRole(SystemRoles.USER, {
|
||||
enabled: !!(isAuthenticated && (user?.role ?? '')),
|
||||
});
|
||||
|
|
@ -52,16 +55,17 @@ const AuthContextProvider = ({
|
|||
//@ts-ignore - ok for token to be undefined initially
|
||||
setTokenHeader(token);
|
||||
setIsAuthenticated(isAuthenticated);
|
||||
if (redirect == null) {
|
||||
// Use a custom redirect if set
|
||||
const finalRedirect = logoutRedirectRef.current || redirect;
|
||||
// Clear the stored redirect
|
||||
logoutRedirectRef.current = undefined;
|
||||
if (finalRedirect == null) {
|
||||
return;
|
||||
}
|
||||
if (redirect.startsWith('http://') || redirect.startsWith('https://')) {
|
||||
// For external links, use window.location
|
||||
window.location.href = redirect;
|
||||
// Or if you want to open in a new tab:
|
||||
// window.open(redirect, '_blank');
|
||||
if (finalRedirect.startsWith('http://') || finalRedirect.startsWith('https://')) {
|
||||
window.location.href = finalRedirect;
|
||||
} else {
|
||||
navigate(redirect, { replace: true });
|
||||
navigate(finalRedirect, { replace: true });
|
||||
}
|
||||
},
|
||||
[navigate, setUser],
|
||||
|
|
@ -106,7 +110,16 @@ const AuthContextProvider = ({
|
|||
});
|
||||
const refreshToken = useRefreshTokenMutation();
|
||||
|
||||
const logout = useCallback(() => logoutUser.mutate(undefined), [logoutUser]);
|
||||
const logout = useCallback(
|
||||
(redirect?: string) => {
|
||||
if (redirect) {
|
||||
logoutRedirectRef.current = redirect;
|
||||
}
|
||||
logoutUser.mutate(undefined);
|
||||
},
|
||||
[logoutUser],
|
||||
);
|
||||
|
||||
const userQuery = useGetUserQuery({ enabled: !!(token ?? '') });
|
||||
|
||||
const login = (data: t.TLoginUser) => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue