mirror of
https://github.com/ansible/awx.git
synced 2026-03-10 22:19:28 -02:30
Redirect user after authenticating through social auth
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
|||||||
Route,
|
Route,
|
||||||
Switch,
|
Switch,
|
||||||
Redirect,
|
Redirect,
|
||||||
|
useHistory,
|
||||||
} from 'react-router-dom';
|
} from 'react-router-dom';
|
||||||
import { ErrorBoundary } from 'react-error-boundary';
|
import { ErrorBoundary } from 'react-error-boundary';
|
||||||
import { I18nProvider } from '@lingui/react';
|
import { I18nProvider } from '@lingui/react';
|
||||||
@@ -27,6 +28,7 @@ import Metrics from './screens/Metrics';
|
|||||||
|
|
||||||
import getRouteConfig from './routeConfig';
|
import getRouteConfig from './routeConfig';
|
||||||
import SubscriptionEdit from './screens/Setting/Subscription/SubscriptionEdit';
|
import SubscriptionEdit from './screens/Setting/Subscription/SubscriptionEdit';
|
||||||
|
import { SESSION_REDIRECT_URL } from './constants';
|
||||||
|
|
||||||
function ErrorFallback({ error }) {
|
function ErrorFallback({ error }) {
|
||||||
return (
|
return (
|
||||||
@@ -84,7 +86,7 @@ const AuthorizedRoutes = ({ routeConfig }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const ProtectedRoute = ({ children, ...rest }) => {
|
const ProtectedRoute = ({ children, ...rest }) => {
|
||||||
const { authRedirectTo, setAuthRedirectTo } = useSession();
|
const { setAuthRedirectTo } = useSession();
|
||||||
const { pathname } = useLocation();
|
const { pathname } = useLocation();
|
||||||
|
|
||||||
if (isAuthenticated(document.cookie)) {
|
if (isAuthenticated(document.cookie)) {
|
||||||
@@ -97,11 +99,13 @@ const ProtectedRoute = ({ children, ...rest }) => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (authRedirectTo !== null) setAuthRedirectTo(pathname);
|
setAuthRedirectTo(pathname);
|
||||||
return <Redirect to="/login" />;
|
return <Redirect to="/login" />;
|
||||||
};
|
};
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
const history = useHistory();
|
||||||
|
const { hash, search, pathname } = useLocation();
|
||||||
let language = getLanguageWithoutRegionCode(navigator);
|
let language = getLanguageWithoutRegionCode(navigator);
|
||||||
if (!Object.keys(locales).includes(language)) {
|
if (!Object.keys(locales).includes(language)) {
|
||||||
// If there isn't a string catalog available for the browser's
|
// If there isn't a string catalog available for the browser's
|
||||||
@@ -112,7 +116,12 @@ function App() {
|
|||||||
dynamicActivate(language);
|
dynamicActivate(language);
|
||||||
}, [language]);
|
}, [language]);
|
||||||
|
|
||||||
const { hash, search, pathname } = useLocation();
|
const redirectURL = window.sessionStorage.getItem(SESSION_REDIRECT_URL);
|
||||||
|
if (redirectURL) {
|
||||||
|
window.sessionStorage.removeItem(SESSION_REDIRECT_URL);
|
||||||
|
if (redirectURL !== '/' || redirectURL !== '/home')
|
||||||
|
history.replace(redirectURL);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<I18nProvider i18n={i18n}>
|
<I18nProvider i18n={i18n}>
|
||||||
|
|||||||
@@ -9,3 +9,4 @@ export const JOB_TYPE_URL_SEGMENTS = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const SESSION_TIMEOUT_KEY = 'awx-session-timeout';
|
export const SESSION_TIMEOUT_KEY = 'awx-session-timeout';
|
||||||
|
export const SESSION_REDIRECT_URL = 'awx-redirect-url';
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ function useStorage(key) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
const setValue = val => {
|
const setValue = val => {
|
||||||
window.localStorage.setItem(key, val);
|
window.localStorage.setItem(key, JSON.stringify(val));
|
||||||
setStorageVal(val);
|
setStorageVal(val);
|
||||||
};
|
};
|
||||||
return [storageVal, setValue];
|
return [storageVal, setValue];
|
||||||
@@ -70,16 +70,18 @@ function SessionProvider({ children }) {
|
|||||||
const sessionIntervalId = useRef();
|
const sessionIntervalId = useRef();
|
||||||
const [sessionTimeout, setSessionTimeout] = useStorage(SESSION_TIMEOUT_KEY);
|
const [sessionTimeout, setSessionTimeout] = useStorage(SESSION_TIMEOUT_KEY);
|
||||||
const [sessionCountdown, setSessionCountdown] = useState(0);
|
const [sessionCountdown, setSessionCountdown] = useState(0);
|
||||||
const [authRedirectTo, setAuthRedirectTo] = useState('/home');
|
const [authRedirectTo, setAuthRedirectTo] = useState('/');
|
||||||
|
|
||||||
const logout = useCallback(async () => {
|
const logout = useCallback(async () => {
|
||||||
if (!isSessionExpired.current) setAuthRedirectTo(null);
|
if (!isSessionExpired.current) {
|
||||||
|
history.replace('/');
|
||||||
|
}
|
||||||
await RootAPI.logout();
|
await RootAPI.logout();
|
||||||
setSessionTimeout(null);
|
setSessionTimeout(0);
|
||||||
setSessionCountdown(0);
|
setSessionCountdown(0);
|
||||||
clearTimeout(sessionTimeoutId.current);
|
clearTimeout(sessionTimeoutId.current);
|
||||||
clearInterval(sessionIntervalId.current);
|
clearInterval(sessionIntervalId.current);
|
||||||
}, [setSessionTimeout, setSessionCountdown]);
|
}, [setSessionTimeout, setSessionCountdown, history]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isAuthenticated(document.cookie)) {
|
if (!isAuthenticated(document.cookie)) {
|
||||||
@@ -104,6 +106,7 @@ function SessionProvider({ children }) {
|
|||||||
}, 1000);
|
}, 1000);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
setSessionCountdown(0);
|
||||||
clearTimeout(sessionTimeoutId.current);
|
clearTimeout(sessionTimeoutId.current);
|
||||||
clearInterval(sessionIntervalId.current);
|
clearInterval(sessionIntervalId.current);
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import { AuthAPI, RootAPI } from '../../api';
|
|||||||
import AlertModal from '../../components/AlertModal';
|
import AlertModal from '../../components/AlertModal';
|
||||||
import ErrorDetail from '../../components/ErrorDetail';
|
import ErrorDetail from '../../components/ErrorDetail';
|
||||||
import { useSession } from '../../contexts/Session';
|
import { useSession } from '../../contexts/Session';
|
||||||
|
import { SESSION_REDIRECT_URL } from '../../constants';
|
||||||
|
|
||||||
const loginLogoSrc = '/static/media/logo-login.svg';
|
const loginLogoSrc = '/static/media/logo-login.svg';
|
||||||
|
|
||||||
@@ -96,6 +97,7 @@ function AWXLogin({ alt, isAuthenticated }) {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchCustomLoginInfo();
|
fetchCustomLoginInfo();
|
||||||
}, [fetchCustomLoginInfo]);
|
}, [fetchCustomLoginInfo]);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
isLoading: isAuthenticating,
|
isLoading: isAuthenticating,
|
||||||
error: authenticationError,
|
error: authenticationError,
|
||||||
@@ -148,6 +150,10 @@ function AWXLogin({ alt, isAuthenticated }) {
|
|||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
function setSessionRedirect() {
|
||||||
|
window.sessionStorage.setItem(SESSION_REDIRECT_URL, authRedirectTo);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Login header={Header} footer={Footer}>
|
<Login header={Header} footer={Footer}>
|
||||||
<LoginMainHeader
|
<LoginMainHeader
|
||||||
@@ -221,6 +227,7 @@ function AWXLogin({ alt, isAuthenticated }) {
|
|||||||
data-cy="social-auth-azure"
|
data-cy="social-auth-azure"
|
||||||
href={loginUrl}
|
href={loginUrl}
|
||||||
key={authKey}
|
key={authKey}
|
||||||
|
onClick={setSessionRedirect}
|
||||||
>
|
>
|
||||||
<Tooltip content={t`Sign in with Azure AD`}>
|
<Tooltip content={t`Sign in with Azure AD`}>
|
||||||
<AzureIcon />
|
<AzureIcon />
|
||||||
@@ -234,6 +241,7 @@ function AWXLogin({ alt, isAuthenticated }) {
|
|||||||
data-cy="social-auth-github"
|
data-cy="social-auth-github"
|
||||||
href={loginUrl}
|
href={loginUrl}
|
||||||
key={authKey}
|
key={authKey}
|
||||||
|
onClick={setSessionRedirect}
|
||||||
>
|
>
|
||||||
<Tooltip content={t`Sign in with GitHub`}>
|
<Tooltip content={t`Sign in with GitHub`}>
|
||||||
<GithubIcon />
|
<GithubIcon />
|
||||||
@@ -247,6 +255,7 @@ function AWXLogin({ alt, isAuthenticated }) {
|
|||||||
data-cy="social-auth-github-org"
|
data-cy="social-auth-github-org"
|
||||||
href={loginUrl}
|
href={loginUrl}
|
||||||
key={authKey}
|
key={authKey}
|
||||||
|
onClick={setSessionRedirect}
|
||||||
>
|
>
|
||||||
<Tooltip content={t`Sign in with GitHub Organizations`}>
|
<Tooltip content={t`Sign in with GitHub Organizations`}>
|
||||||
<GithubIcon />
|
<GithubIcon />
|
||||||
@@ -260,6 +269,7 @@ function AWXLogin({ alt, isAuthenticated }) {
|
|||||||
data-cy="social-auth-github-team"
|
data-cy="social-auth-github-team"
|
||||||
href={loginUrl}
|
href={loginUrl}
|
||||||
key={authKey}
|
key={authKey}
|
||||||
|
onClick={setSessionRedirect}
|
||||||
>
|
>
|
||||||
<Tooltip content={t`Sign in with GitHub Teams`}>
|
<Tooltip content={t`Sign in with GitHub Teams`}>
|
||||||
<GithubIcon />
|
<GithubIcon />
|
||||||
@@ -273,6 +283,7 @@ function AWXLogin({ alt, isAuthenticated }) {
|
|||||||
data-cy="social-auth-github-enterprise"
|
data-cy="social-auth-github-enterprise"
|
||||||
href={loginUrl}
|
href={loginUrl}
|
||||||
key={authKey}
|
key={authKey}
|
||||||
|
onClick={setSessionRedirect}
|
||||||
>
|
>
|
||||||
<Tooltip content={t`Sign in with GitHub Enterprise`}>
|
<Tooltip content={t`Sign in with GitHub Enterprise`}>
|
||||||
<GithubIcon />
|
<GithubIcon />
|
||||||
@@ -286,6 +297,7 @@ function AWXLogin({ alt, isAuthenticated }) {
|
|||||||
data-cy="social-auth-github-enterprise-org"
|
data-cy="social-auth-github-enterprise-org"
|
||||||
href={loginUrl}
|
href={loginUrl}
|
||||||
key={authKey}
|
key={authKey}
|
||||||
|
onClick={setSessionRedirect}
|
||||||
>
|
>
|
||||||
<Tooltip
|
<Tooltip
|
||||||
content={t`Sign in with GitHub Enterprise Organizations`}
|
content={t`Sign in with GitHub Enterprise Organizations`}
|
||||||
@@ -301,6 +313,7 @@ function AWXLogin({ alt, isAuthenticated }) {
|
|||||||
data-cy="social-auth-github-enterprise-team"
|
data-cy="social-auth-github-enterprise-team"
|
||||||
href={loginUrl}
|
href={loginUrl}
|
||||||
key={authKey}
|
key={authKey}
|
||||||
|
onClick={setSessionRedirect}
|
||||||
>
|
>
|
||||||
<Tooltip
|
<Tooltip
|
||||||
content={t`Sign in with GitHub Enterprise Teams`}
|
content={t`Sign in with GitHub Enterprise Teams`}
|
||||||
@@ -316,6 +329,7 @@ function AWXLogin({ alt, isAuthenticated }) {
|
|||||||
data-cy="social-auth-google"
|
data-cy="social-auth-google"
|
||||||
href={loginUrl}
|
href={loginUrl}
|
||||||
key={authKey}
|
key={authKey}
|
||||||
|
onClick={setSessionRedirect}
|
||||||
>
|
>
|
||||||
<Tooltip content={t`Sign in with Google`}>
|
<Tooltip content={t`Sign in with Google`}>
|
||||||
<GoogleIcon />
|
<GoogleIcon />
|
||||||
@@ -330,6 +344,7 @@ function AWXLogin({ alt, isAuthenticated }) {
|
|||||||
data-cy="social-auth-saml"
|
data-cy="social-auth-saml"
|
||||||
href={loginUrl}
|
href={loginUrl}
|
||||||
key={authKey}
|
key={authKey}
|
||||||
|
onClick={setSessionRedirect}
|
||||||
>
|
>
|
||||||
<Tooltip
|
<Tooltip
|
||||||
content={
|
content={
|
||||||
|
|||||||
Reference in New Issue
Block a user