Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

login.tsx

149 lines4.6 KB

1import { useState } from 'react'
2import { createFileRoute, useRouter } from '@tanstack/react-router'
3​
4import { SiteFooter, SiteNav } from '#/components/site.tsx'
5import { authClient } from '#/lib/auth-client.ts'
6​
7export const Route = createFileRoute('/login')({
8 component: Login,
9})
10​
11function Login() {
12 const router = useRouter()
13 const { data: session, isPending } = authClient.useSession()
14 const [email, setEmail] = useState('')
15 const [otp, setOtp] = useState('')
16 const [step, setStep] = useState<'email' | 'otp'>('email')
17 const [busy, setBusy] = useState(false)
18 const [error, setError] = useState<string | null>(null)
19​
20 const sendCode = async (event: React.FormEvent) => {
21 event.preventDefault()
22 setBusy(true)
23 setError(null)
24 const { error: sendError } = await authClient.emailOtp.sendVerificationOtp({
25 email,
26 type: 'sign-in',
27 })
28 setBusy(false)
29 if (sendError) {
30 setError(sendError.message ?? 'Could not send the code.')
31 return
32 }
33 setStep('otp')
34 }
35​
36 const verifyCode = async (event: React.FormEvent) => {
37 event.preventDefault()
38 setBusy(true)
39 setError(null)
40 const { error: signInError } = await authClient.signIn.emailOtp({
41 email,
42 otp,
43 })
44 setBusy(false)
45 if (signInError) {
46 setError(signInError.message ?? 'Invalid code.')
47 return
48 }
49 await router.invalidate()
50 }
51​
52 const inputClasses =
53 'hairline w-full border bg-paper-raised px-3 py-2 font-mono text-sm text-ink outline-none transition-colors focus:border-ink'
54 const buttonClasses =
55 'w-full border border-ink bg-ink px-3 py-2 font-mono text-sm text-paper transition-colors hover:bg-press hover:border-press disabled:opacity-50'
56​
57 return (
58 <div className="min-h-screen text-ink">
59 <SiteNav active="sign in" />
60 <div className="mx-auto max-w-sm space-y-6 px-5 py-16">
61 <header className="space-y-2">
62 <h1 className="font-mono text-2xl font-semibold tracking-tight">
63 Sign in<span className="text-tok-blue">▌</span>
64 </h1>
65 <p className="text-sm text-ink-soft">
66 Manage your API keys. We email you a one-time code — no password.
67 </p>
68 </header>
69​
70 {isPending ? null : session ? (
71 <div className="space-y-3 text-sm">
72 <p>
73 Signed in as{' '}
74 <span className="font-medium">{session.user.email}</span>
75 </p>
76 <a className="press-link" href="/account">
77 Manage API keys →
78 </a>
79 <button
80 type="button"
81 className={buttonClasses}
82 onClick={() => {
83 void authClient.signOut().then(() => router.invalidate())
84 }}
85 >
86 Sign out
87 </button>
88 </div>
89 ) : step === 'email' ? (
90 <form
91 className="space-y-3"
92 onSubmit={(event) => {
93 void sendCode(event)
94 }}
95 >
96 <input
97 className={inputClasses}
98 type="email"
99 required
100 placeholder="you@example.com"
101 value={email}
102 onChange={(event) => setEmail(event.target.value)}
103 />
104 <button className={buttonClasses} disabled={busy} type="submit">
105 {busy ? 'Sending…' : 'Email me a code'}
106 </button>
107 </form>
108 ) : (
109 <form
110 className="space-y-3"
111 onSubmit={(event) => {
112 void verifyCode(event)
113 }}
114 >
115 <p className="text-sm text-ink-soft">
116 Enter the 6-digit code sent to{' '}
117 <span className="font-medium">{email}</span>.
118 </p>
119 <input
120 className={`${inputClasses} text-center text-lg tracking-[0.4em]`}
121 inputMode="numeric"
122 pattern="[0-9]{6}"
123 maxLength={6}
124 required
125 placeholder="••••••"
126 value={otp}
127 onChange={(event) => setOtp(event.target.value)}
128 />
129 <button className={buttonClasses} disabled={busy} type="submit">
130 {busy ? 'Verifying…' : 'Sign in'}
131 </button>
132 <button
133 type="button"
134 className="press-link w-full font-mono text-xs text-ink-faint"
135 onClick={() => setStep('email')}
136 >
137 Use a different email
138 </button>
139 </form>
140 )}
141​
142 {error ? (
143 <p className="font-mono text-sm text-tok-red">{error}</p>
144 ) : null}
145 </div>
146 <SiteFooter />
147 </div>
148 )