@@ -303,38 +303,40 @@ export async function loader({ request }: Route.LoaderArgs) {
303
303
const user = session .get (' user' )
304
304
305
305
// If the user is already authenticated, redirect to dashboard.
306
- if (user ) return redirect (' /dashboard' )
307
-
308
- // Get the TOTP cookie and the token from the URL.
309
- const cookie = new Cookie (request .headers .get (' Cookie' ) || ' ' )
310
- const totpCookie = cookie .get (' _totp' )
306
+ if (user ) return redirect (' /profile' )
311
307
308
+ // Get token from the URL.
312
309
const url = new URL (request .url )
313
310
const token = url .searchParams .get (' t' )
314
311
315
312
// Authenticate the user via magic-link URL.
316
313
if (token ) {
317
314
try {
318
315
return await authenticator .authenticate (' TOTP' , request )
319
- } catch (error : unknown ) {
316
+ } catch (error ) {
320
317
if (error instanceof Response ) return error
321
- if (error instanceof Error ) return { error: error .message }
322
- return { error: ' Invalid TOTP' }
318
+ if (error instanceof Error ) {
319
+ console .error (error )
320
+ return { authError: error .message }
321
+ }
322
+ return { authError: ' Invalid TOTP' }
323
323
}
324
324
}
325
325
326
- // Get the email from the TOTP cookie.
327
- let email = null
326
+ // Get TOTP cookie values.
327
+ const cookie = new Cookie (request .headers .get (' Cookie' ) || ' ' )
328
+ const totpCookieValue = cookie .get (' _totp' )
328
329
329
- if (totpCookie ) {
330
- const params = new URLSearchParams (totpCookie )
331
- email = params .get (' email' )
330
+ if (totpCookieValue ) {
331
+ const params = new URLSearchParams (totpCookieValue )
332
+ return {
333
+ authEmail: params .get (' email' ),
334
+ authError: params .get (' error' ),
335
+ }
332
336
}
333
337
334
- // If no email is found, redirect to login.
335
- if (! email ) return redirect (' /auth/login' )
336
-
337
- return { email }
338
+ // If the TOTP cookie is not found, redirect to the login page.
339
+ throw redirect (' /auth/login' )
338
340
}
339
341
340
342
/**
@@ -363,18 +365,20 @@ export async function action({ request }: Route.ActionArgs) {
363
365
364
366
export default function Verify() {
365
367
const loaderData = useLoaderData <typeof loader >()
366
-
367
368
const [value, setValue] = useState (' ' )
369
+
370
+ const authEmail = ' authEmail' in loaderData ? loaderData .authEmail : undefined
371
+ const authError = ' authError' in loaderData ? loaderData .authError : null
372
+
368
373
const fetcher = useFetcher ()
369
374
const isSubmitting = fetcher .state !== ' idle' || fetcher .formData != null
370
375
371
- const email = ' email' in loaderData ? loaderData .email : undefined
372
- const error = ' error' in loaderData ? loaderData .error : null
373
- const errors = fetcher .data ?.error || error
376
+ // Either get the error from the fetcher (action) or the loader.
377
+ const errors = fetcher .data ?.authError || authError
374
378
375
379
return (
376
380
<div style = { { display: ' flex' , flexDirection: ' column' }} >
377
- { /* Code Verification Form */ }
381
+ { /* Code Verification Form. */ }
378
382
<fetcher.Form method = " POST" >
379
383
<input
380
384
required
@@ -386,7 +390,7 @@ export default function Verify() {
386
390
<button type = " submit" >Continue</button >
387
391
</fetcher.Form >
388
392
389
- { /* Renders the form that requests a new code . */ }
393
+ { /* Request New Code . */ }
390
394
{ /* Email input is not required, it's already stored in Session. */ }
391
395
<fetcher.Form method = " POST" action = " /auth/login" >
392
396
<button type = " submit" >Request new Code</button >
@@ -412,7 +416,7 @@ export async function loader({ request }: Route.LoaderArgs) {
412
416
// Get the session.
413
417
const session = await sessionStorage .getSession (request .headers .get (' Cookie' ))
414
418
415
- // Destroy the session and redirect to login .
419
+ // Destroy the session and redirect to any route of your choice .
416
420
return redirect (' /auth/login' , {
417
421
headers: {
418
422
' Set-Cookie' : await sessionStorage .destroySession (session ),
0 commit comments