diff --git a/src/App.tsx b/src/App.tsx index dd8fc05..4ebcb09 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -48,13 +48,13 @@ function App(_: AppProps) {
{!isValid(auth) ? ( - + ) : ( - + diff --git a/src/modules/login/Login.tsx b/src/modules/login/Login.tsx index adba608..445cf00 100644 --- a/src/modules/login/Login.tsx +++ b/src/modules/login/Login.tsx @@ -4,6 +4,7 @@ import clsx from 'clsx'; import { LockClosedIcon } from '@heroicons/react/solid'; import { performApiCall } from 'src/services/api'; +import { showToast, ToastType } from 'src/common/util/show-toast'; type LoginProps = RouteComponentProps; @@ -20,27 +21,10 @@ export function Login(_: LoginProps) { window.location.href = data.authorizationUrl; } } 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 (
diff --git a/src/modules/login/LoginCallback.tsx b/src/modules/login/LoginCallback.tsx index 497578d..c58c671 100644 --- a/src/modules/login/LoginCallback.tsx +++ b/src/modules/login/LoginCallback.tsx @@ -1,5 +1,5 @@ import React, { useEffect } from 'react'; -import { RouteComponentProps } from '@reach/router'; +import { navigate, RouteComponentProps } from '@reach/router'; import { useAuth } from 'src/services/auth'; 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 export function LoginCallback(_: LoginCallbackProps) { + const currentURL = window.location.href; + const urlParams = new URLSearchParams(currentURL); + const state = urlParams.get('state'); + const { logIn } = useAuth(); useEffect(() => { - const res = logIn(); + if (state) { + const res = logIn(state); - // @ts-ignore - if (res.ok) { - showToast('Logged in'); - } else { - showToast('Something went wrong', ToastType.Error); - // navigate('/login'); + // @ts-ignore + if (res.ok) { + showToast('Logged in'); + } else { + showToast('Something went wrong', ToastType.Error); + navigate('/login'); + } } - }, [logIn]); + }, [logIn, state]); return (
diff --git a/src/services/auth/hooks/use-auth.ts b/src/services/auth/hooks/use-auth.ts index 6630032..38d017d 100644 --- a/src/services/auth/hooks/use-auth.ts +++ b/src/services/auth/hooks/use-auth.ts @@ -6,9 +6,12 @@ export function useAuth() { const dispatch = useDispatch(); const auth = useSelector(getAuth); - const logIn = useCallback(() => { - return dispatch(signIn()); - }, [dispatch]); + const logIn = useCallback( + (state) => { + return dispatch(signIn(state)); + }, + [dispatch], + ); const logOut = useCallback(() => { return dispatch(signOut()); diff --git a/src/services/auth/redux/actions.ts b/src/services/auth/redux/actions.ts index 0634689..770bd8e 100644 --- a/src/services/auth/redux/actions.ts +++ b/src/services/auth/redux/actions.ts @@ -15,11 +15,14 @@ const signOutAction = (): SuccessAction => ({ payload: null, }); -export const signIn = () => +export const signIn = (state: string) => createApiAction( { path: '/hydra/callback', method: 'GET', + headers: { + state, + }, }, [AuthActionTypes.SIGN_IN_START, AuthActionTypes.SIGN_IN_SUCCESS, AuthActionTypes.SIGN_IN_FAILURE], );