-
Notifications
You must be signed in to change notification settings - Fork 341
/
login.tsx
205 lines (187 loc) · 5.67 KB
/
login.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { getFormProps, getInputProps, useForm } from '@conform-to/react'
import { getZodConstraint, parseWithZod } from '@conform-to/zod'
import {
json,
type ActionFunctionArgs,
type LoaderFunctionArgs,
type MetaFunction,
} from '@remix-run/node'
import { Form, Link, useActionData, useSearchParams } from '@remix-run/react'
import { HoneypotInputs } from 'remix-utils/honeypot/react'
import { z } from 'zod'
import { GeneralErrorBoundary } from '#app/components/error-boundary.tsx'
import { CheckboxField, ErrorList, Field } from '#app/components/forms.tsx'
import { Spacer } from '#app/components/spacer.tsx'
import { StatusButton } from '#app/components/ui/status-button.tsx'
import { login, requireAnonymous } from '#app/utils/auth.server.ts'
import {
ProviderConnectionForm,
providerNames,
} from '#app/utils/connections.tsx'
import { checkHoneypot } from '#app/utils/honeypot.server.ts'
import { useIsPending } from '#app/utils/misc.tsx'
import { PasswordSchema, UsernameSchema } from '#app/utils/user-validation.ts'
import { handleNewSession } from './login.server.ts'
const LoginFormSchema = z.object({
username: UsernameSchema,
password: PasswordSchema,
redirectTo: z.string().optional(),
remember: z.boolean().optional(),
})
export async function loader({ request }: LoaderFunctionArgs) {
await requireAnonymous(request)
return json({})
}
export async function action({ request }: ActionFunctionArgs) {
await requireAnonymous(request)
const formData = await request.formData()
checkHoneypot(formData)
const submission = await parseWithZod(formData, {
schema: (intent) =>
LoginFormSchema.transform(async (data, ctx) => {
if (intent !== null) return { ...data, session: null }
const session = await login(data)
if (!session) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Invalid username or password',
})
return z.NEVER
}
return { ...data, session }
}),
async: true,
})
if (submission.status !== 'success' || !submission.value.session) {
return json(
{ result: submission.reply({ hideFields: ['password'] }) },
{ status: submission.status === 'error' ? 400 : 200 },
)
}
const { session, remember, redirectTo } = submission.value
return handleNewSession({
request,
session,
remember: remember ?? false,
redirectTo,
})
}
export default function LoginPage() {
const actionData = useActionData<typeof action>()
const isPending = useIsPending()
const [searchParams] = useSearchParams()
const redirectTo = searchParams.get('redirectTo')
const [form, fields] = useForm({
id: 'login-form',
constraint: getZodConstraint(LoginFormSchema),
defaultValue: { redirectTo },
lastResult: actionData?.result,
onValidate({ formData }) {
return parseWithZod(formData, { schema: LoginFormSchema })
},
shouldRevalidate: 'onBlur',
})
return (
<div className="flex min-h-full flex-col justify-center pb-32 pt-20">
<div className="mx-auto w-full max-w-md">
<div className="flex flex-col gap-3 text-center">
<h1 className="text-h1">Welcome back!</h1>
<p className="text-body-md text-muted-foreground">
Please enter your details.
</p>
</div>
<Spacer size="xs" />
<div>
<div className="mx-auto w-full max-w-md px-8">
<Form method="POST" {...getFormProps(form)}>
<HoneypotInputs />
<Field
labelProps={{ children: 'Username' }}
inputProps={{
...getInputProps(fields.username, { type: 'text' }),
autoFocus: true,
className: 'lowercase',
autoComplete: 'username',
}}
errors={fields.username.errors}
/>
<Field
labelProps={{ children: 'Password' }}
inputProps={{
...getInputProps(fields.password, {
type: 'password',
}),
autoComplete: 'current-password',
}}
errors={fields.password.errors}
/>
<div className="flex justify-between">
<CheckboxField
labelProps={{
htmlFor: fields.remember.id,
children: 'Remember me',
}}
buttonProps={getInputProps(fields.remember, {
type: 'checkbox',
})}
errors={fields.remember.errors}
/>
<div>
<Link
to="/forgot-password"
className="text-body-xs font-semibold"
>
Forgot password?
</Link>
</div>
</div>
<input
{...getInputProps(fields.redirectTo, { type: 'hidden' })}
/>
<ErrorList errors={form.errors} id={form.errorId} />
<div className="flex items-center justify-between gap-6 pt-3">
<StatusButton
className="w-full"
status={isPending ? 'pending' : form.status ?? 'idle'}
type="submit"
disabled={isPending}
>
Log in
</StatusButton>
</div>
</Form>
<ul className="mt-5 flex flex-col gap-5 border-b-2 border-t-2 border-border py-3">
{providerNames.map((providerName) => (
<li key={providerName}>
<ProviderConnectionForm
type="Login"
providerName={providerName}
redirectTo={redirectTo}
/>
</li>
))}
</ul>
<div className="flex items-center justify-center gap-2 pt-6">
<span className="text-muted-foreground">New here?</span>
<Link
to={
redirectTo
? `/signup?${encodeURIComponent(redirectTo)}`
: '/signup'
}
>
Create an account
</Link>
</div>
</div>
</div>
</div>
</div>
)
}
export const meta: MetaFunction = () => {
return [{ title: 'Login to Epic Notes' }]
}
export function ErrorBoundary() {
return <GeneralErrorBoundary />
}