State token fix
This commit is contained in:
parent
1de2c8ee48
commit
3eaf001df7
5 changed files with 29 additions and 33 deletions
|
@ -48,13 +48,13 @@ function App(_: AppProps) {
|
||||||
<div className="app bg-gray-50 min-h-screen flex flex-col">
|
<div className="app bg-gray-50 min-h-screen flex flex-col">
|
||||||
{!isValid(auth) ? (
|
{!isValid(auth) ? (
|
||||||
<Router>
|
<Router>
|
||||||
<Login path="/login" />
|
<Login default path="/login" />
|
||||||
<LoginCallback path="/login-callback" />
|
<LoginCallback path="/login-callback" />
|
||||||
</Router>
|
</Router>
|
||||||
) : (
|
) : (
|
||||||
<Layout>
|
<Layout>
|
||||||
<Router>
|
<Router>
|
||||||
<Dashboard path="/dashboard" />
|
<Dashboard default path="/dashboard" />
|
||||||
<Users path="/users" />
|
<Users path="/users" />
|
||||||
<Apps path="/apps" />
|
<Apps path="/apps" />
|
||||||
<AppSingle path="/apps/:id" />
|
<AppSingle path="/apps/:id" />
|
||||||
|
|
|
@ -4,6 +4,7 @@ import clsx from 'clsx';
|
||||||
import { LockClosedIcon } from '@heroicons/react/solid';
|
import { LockClosedIcon } from '@heroicons/react/solid';
|
||||||
|
|
||||||
import { performApiCall } from 'src/services/api';
|
import { performApiCall } from 'src/services/api';
|
||||||
|
import { showToast, ToastType } from 'src/common/util/show-toast';
|
||||||
|
|
||||||
type LoginProps = RouteComponentProps;
|
type LoginProps = RouteComponentProps;
|
||||||
|
|
||||||
|
@ -20,27 +21,10 @@ export function Login(_: LoginProps) {
|
||||||
window.location.href = data.authorizationUrl;
|
window.location.href = data.authorizationUrl;
|
||||||
}
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
// continue
|
showToast('Something went wrong', ToastType.Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
// if (res.ok) {
|
|
||||||
// showToast('Logged in!');
|
|
||||||
// } else {
|
|
||||||
// showToast('Username or password incorrect', ToastType.Error);
|
|
||||||
// }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// useEffect(() => {
|
|
||||||
// if (isValid(auth)) {
|
|
||||||
// navigate('/dashboard');
|
|
||||||
// }
|
|
||||||
// }, [auth]);
|
|
||||||
|
|
||||||
// if (isValid(auth)) {
|
|
||||||
// return null;
|
|
||||||
// }
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8">
|
<div className="min-h-screen flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8">
|
||||||
<div className="max-w-md w-full space-y-8">
|
<div className="max-w-md w-full space-y-8">
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import React, { useEffect } from 'react';
|
import React, { useEffect } from 'react';
|
||||||
import { RouteComponentProps } from '@reach/router';
|
import { navigate, RouteComponentProps } from '@reach/router';
|
||||||
import { useAuth } from 'src/services/auth';
|
import { useAuth } from 'src/services/auth';
|
||||||
import { showToast, ToastType } from 'src/common/util/show-toast';
|
import { showToast, ToastType } from 'src/common/util/show-toast';
|
||||||
|
|
||||||
|
@ -7,19 +7,25 @@ type LoginCallbackProps = RouteComponentProps;
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
export function LoginCallback(_: LoginCallbackProps) {
|
export function LoginCallback(_: LoginCallbackProps) {
|
||||||
|
const currentURL = window.location.href;
|
||||||
|
const urlParams = new URLSearchParams(currentURL);
|
||||||
|
const state = urlParams.get('state');
|
||||||
|
|
||||||
const { logIn } = useAuth();
|
const { logIn } = useAuth();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const res = logIn();
|
if (state) {
|
||||||
|
const res = logIn(state);
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
showToast('Logged in');
|
showToast('Logged in');
|
||||||
} else {
|
} else {
|
||||||
showToast('Something went wrong', ToastType.Error);
|
showToast('Something went wrong', ToastType.Error);
|
||||||
// navigate('/login');
|
navigate('/login');
|
||||||
}
|
}
|
||||||
}, [logIn]);
|
}
|
||||||
|
}, [logIn, state]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8">
|
<div className="min-h-screen flex items-center justify-center bg-gray-50 py-12 px-4 sm:px-6 lg:px-8">
|
||||||
|
|
|
@ -6,9 +6,12 @@ export function useAuth() {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const auth = useSelector(getAuth);
|
const auth = useSelector(getAuth);
|
||||||
|
|
||||||
const logIn = useCallback(() => {
|
const logIn = useCallback(
|
||||||
return dispatch(signIn());
|
(state) => {
|
||||||
}, [dispatch]);
|
return dispatch(signIn(state));
|
||||||
|
},
|
||||||
|
[dispatch],
|
||||||
|
);
|
||||||
|
|
||||||
const logOut = useCallback(() => {
|
const logOut = useCallback(() => {
|
||||||
return dispatch(signOut());
|
return dispatch(signOut());
|
||||||
|
|
|
@ -15,11 +15,14 @@ const signOutAction = (): SuccessAction => ({
|
||||||
payload: null,
|
payload: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const signIn = () =>
|
export const signIn = (state: string) =>
|
||||||
createApiAction(
|
createApiAction(
|
||||||
{
|
{
|
||||||
path: '/hydra/callback',
|
path: '/hydra/callback',
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
state,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
[AuthActionTypes.SIGN_IN_START, AuthActionTypes.SIGN_IN_SUCCESS, AuthActionTypes.SIGN_IN_FAILURE],
|
[AuthActionTypes.SIGN_IN_START, AuthActionTypes.SIGN_IN_SUCCESS, AuthActionTypes.SIGN_IN_FAILURE],
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue