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

login.tsx

180 lines5.6 KB

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