From 42bdd40f916a83a9f78d71e07e64aabbdcfb551a Mon Sep 17 00:00:00 2001 From: keyan Date: Sun, 30 Jul 2023 15:39:18 -0500 Subject: [PATCH] show errors on settings during auth linking --- components/login.js | 30 +++++++++++++++++------------- pages/login.js | 8 ++++++++ pages/settings.js | 12 ++++++++++++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/components/login.js b/components/login.js index 34ecb460..1d9fd5b7 100644 --- a/components/login.js +++ b/components/login.js @@ -31,20 +31,24 @@ export function EmailLoginForm ({ text, callbackUrl }) { ) } -export default function Login ({ providers, callbackUrl, error, text, Header, Footer }) { - const errors = { - OAuthSignin: 'Error constructing OAuth URL. Try again or choose a different method.', - OAuthCallback: 'Error handling OAuth response. Try again or choose a different method.', - OAuthCreateAccount: 'Could not create OAuth account. Try again or choose a different method.', - EmailCreateAccount: 'Could not create Email account. Try again or choose a different method.', - Callback: 'Error in callback handler. Try again or choose a different method.', - OAuthAccountNotLinked: 'This auth method is linked to another account. To link to this account first unlink the other account.', - EmailSignin: 'Failed to send email. Make sure you entered your email address correctly.', - CredentialsSignin: 'Auth failed. Try again or choose a different method.', - default: 'Auth failed. Try again or choose a different method.' - } +const authErrorMessages = { + OAuthSignin: 'Error constructing OAuth URL. Try again or choose a different method.', + OAuthCallback: 'Error handling OAuth response. Try again or choose a different method.', + OAuthCreateAccount: 'Could not create OAuth account. Try again or choose a different method.', + EmailCreateAccount: 'Could not create Email account. Try again or choose a different method.', + Callback: 'Error in callback handler. Try again or choose a different method.', + OAuthAccountNotLinked: 'This auth method is linked to another account. To link to this account first unlink the other account.', + EmailSignin: 'Failed to send email. Make sure you entered your email address correctly.', + CredentialsSignin: 'Auth failed. Try again or choose a different method.', + default: 'Auth failed. Try again or choose a different method.' +} - const [errorMessage, setErrorMessage] = useState(error && (errors[error] ?? errors.default)) +export function authErrorMessage(error) { + return error && (authErrorMessages[error] ?? authErrorMessages.default) +} + +export default function Login ({ providers, callbackUrl, error, text, Header, Footer }) { + const [errorMessage, setErrorMessage] = useState(authErrorMessage(error)) const router = useRouter() if (router.query.type === 'lightning') { diff --git a/pages/login.js b/pages/login.js index 346d3369..f933140c 100644 --- a/pages/login.js +++ b/pages/login.js @@ -23,6 +23,14 @@ export async function getServerSideProps ({ req, res, query: { callbackUrl, erro } if (session && callbackUrl) { + // in the cause of auth linking we want to pass the error back to + // settings + if (error) { + const url = new URL(callbackUrl, process.env.PUBLIC_URL) + url.searchParams.set('error', error) + callbackUrl = url.pathname + url.search + } + return { redirect: { destination: callbackUrl, diff --git a/pages/settings.js b/pages/settings.js index 3825d1b7..ff6a2eba 100644 --- a/pages/settings.js +++ b/pages/settings.js @@ -20,6 +20,7 @@ import { emailSchema, lastAuthRemovalSchema, settingsSchema } from '../lib/valid import { SUPPORTED_CURRENCIES } from '../lib/currency' import PageLoading from '../components/page-loading' import { useShowModal } from '../components/modal' +import { authErrorMessage } from '../components/login' export const getServerSideProps = getGetServerSideProps(SETTINGS) @@ -330,6 +331,8 @@ function UnlinkObstacle ({ onClose, type, unlinkAuth }) { function AuthMethods ({ methods }) { const showModal = useShowModal() + const router = useRouter() + const [err, setErr] = useState(authErrorMessage(router.query.error)) const [unlinkAuth] = useMutation( gql` mutation unlinkAuth($authType: String!) { @@ -369,6 +372,15 @@ function AuthMethods ({ methods }) { return ( <>
auth methods
+ {err && { + const { pathname, query: { error, nodata, ...rest } } = router + router.replace({ + pathname, + query: { nodata, ...rest } + }, { pathname, query: { ...rest } }, { shallow: true }) + setErr(undefined) + }} dismissible>{err}} + {providers?.map(provider => { if (provider === 'email') { return methods.email