From 1fbbf5904a38b442e1119f7d12a5c5e51eb45ea9 Mon Sep 17 00:00:00 2001 From: Michael Fatemi Date: Tue, 6 Jul 2021 20:32:37 -0400 Subject: [PATCH] Add better error handling for authentication --- .../Authentication/Authenticator.tsx | 50 +++++++++++++++---- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/src/components/Authentication/Authenticator.tsx b/src/components/Authentication/Authenticator.tsx index 641ce30..2a3499f 100644 --- a/src/components/Authentication/Authenticator.tsx +++ b/src/components/Authentication/Authenticator.tsx @@ -3,17 +3,18 @@ import { Redirect, useLocation, useParams } from 'react-router-dom'; import AuthenticationContext from './AuthenticationContext'; import { createSession } from './createSession'; -function useCode() { +function useCodeAndError() { const location = useLocation(); const query = new URLSearchParams(location.search); const code = query.get('code'); + const error = query.get('error'); - return code; + return [code, error]; } export default function Authenticator() { const { provider } = useParams<{ provider: string }>(); - const code = useCode(); + const [code, error] = useCodeAndError(); const { refresh } = useContext(AuthenticationContext); const [pending, setPending] = useState(true); @@ -28,27 +29,54 @@ export default function Authenticator() { }, [token]); useEffect(() => { - setPending(true); - createSession(code!) - .then(({ token }) => { - setToken(token ?? null); - }) - .finally(() => setPending(false)); + if (code) { + setPending(true); + createSession(code) + .then(({ token }) => { + setToken(token ?? null); + }) + .finally(() => setPending(false)); + } }, [code, provider]); useEffect(() => { + // Refresh when the token changes refresh(); }, [token, refresh]); let children: JSX.Element; - if (pending) { + if (error != null) { + switch (error) { + case 'access_denied': + children = ( + <> +

Sign In Error

+ We couldn't use your Ion account to log in. +
+
+ Home + + ); + break; + default: + console.error('Unhandled OAuth error case:', error); + children =

Sign In Error

; + } + } else if (pending) { children =

Signing In

; } else if (token) { children = ; } else { // If we aren't pending anymore, but don't have a token, we must have errored - children =

Sign In Error

; + children = ( + <> +

Sign In Error

+
+
+ Home + + ); } return (