Skip to content

Commit 8e4312a

Browse files
committed
docs: update README.md
1 parent ea92fbb commit 8e4312a

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

docs/README.md

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -303,38 +303,40 @@ export async function loader({ request }: Route.LoaderArgs) {
303303
const user = session.get('user')
304304

305305
// 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')
311307

308+
// Get token from the URL.
312309
const url = new URL(request.url)
313310
const token = url.searchParams.get('t')
314311

315312
// Authenticate the user via magic-link URL.
316313
if (token) {
317314
try {
318315
return await authenticator.authenticate('TOTP', request)
319-
} catch (error: unknown) {
316+
} catch (error) {
320317
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' }
323323
}
324324
}
325325

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')
328329

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+
}
332336
}
333337

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')
338340
}
339341

340342
/**
@@ -363,18 +365,20 @@ export async function action({ request }: Route.ActionArgs) {
363365

364366
export default function Verify() {
365367
const loaderData = useLoaderData<typeof loader>()
366-
367368
const [value, setValue] = useState('')
369+
370+
const authEmail = 'authEmail' in loaderData ? loaderData.authEmail : undefined
371+
const authError = 'authError' in loaderData ? loaderData.authError : null
372+
368373
const fetcher = useFetcher()
369374
const isSubmitting = fetcher.state !== 'idle' || fetcher.formData != null
370375

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
374378

375379
return (
376380
<div style={{ display: 'flex', flexDirection: 'column' }}>
377-
{/* Code Verification Form */}
381+
{/* Code Verification Form. */}
378382
<fetcher.Form method="POST">
379383
<input
380384
required
@@ -386,7 +390,7 @@ export default function Verify() {
386390
<button type="submit">Continue</button>
387391
</fetcher.Form>
388392

389-
{/* Renders the form that requests a new code. */}
393+
{/* Request New Code. */}
390394
{/* Email input is not required, it's already stored in Session. */}
391395
<fetcher.Form method="POST" action="/auth/login">
392396
<button type="submit">Request new Code</button>
@@ -412,7 +416,7 @@ export async function loader({ request }: Route.LoaderArgs) {
412416
// Get the session.
413417
const session = await sessionStorage.getSession(request.headers.get('Cookie'))
414418

415-
// Destroy the session and redirect to login.
419+
// Destroy the session and redirect to any route of your choice.
416420
return redirect('/auth/login', {
417421
headers: {
418422
'Set-Cookie': await sessionStorage.destroySession(session),

0 commit comments

Comments
 (0)