-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathRegister.tsx
More file actions
156 lines (147 loc) · 5.81 KB
/
Register.tsx
File metadata and controls
156 lines (147 loc) · 5.81 KB
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
import { useState } from 'react'
import { Link, useLoaderData } from 'react-router-dom'
import { useForm } from 'react-hook-form'
import { standardSchemaResolver } from '@hookform/resolvers/standard-schema'
import { z } from 'zod'
import { useAuth } from '@/hooks/useAuth'
import { useTheme } from '@/hooks/useTheme'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Alert, AlertDescription } from '@/components/ui/alert'
import { Loader2, UserPlus, AlertCircle } from 'lucide-react'
import type { AuthConfig } from '@/lib/auth-loaders'
const registerSchema = z.object({
name: z.string().min(2, 'Name must be at least 2 characters'),
email: z.string().email('Invalid email address'),
password: z.string().min(8, 'Password must be at least 8 characters'),
confirmPassword: z.string(),
}).refine((data) => data.password === data.confirmPassword, {
message: "Passwords don't match",
path: ['confirmPassword'],
})
type RegisterFormData = z.infer<typeof registerSchema>
export function Register() {
const { signUpWithEmail } = useAuth()
const { config } = useLoaderData() as { config: AuthConfig }
const theme = useTheme()
const [error, setError] = useState<string | null>(null)
const [isSubmitting, setIsSubmitting] = useState(false)
const {
register,
handleSubmit,
formState: { errors },
} = useForm<RegisterFormData>({
resolver: standardSchemaResolver(registerSchema),
})
const onSubmit = async (data: RegisterFormData) => {
setError(null)
setIsSubmitting(true)
try {
const result = await signUpWithEmail(data.email, data.password, data.name)
if (result.error) {
setError(result.error)
}
} finally {
setIsSubmitting(false)
}
}
return (
<div className="h-dvh flex flex-col items-center justify-center bg-gradient-to-br from-background via-background to-background p-4">
<div className="w-full max-w-sm space-y-6">
<div className="flex flex-col items-center space-y-2">
<img
src={theme === 'light' ? "/opencode-wordmark-light.svg" : "/opencode-wordmark-dark.svg"}
alt="OpenCode"
className="h-8 w-auto"
/>
<p className="text-sm text-muted-foreground">
{config.isFirstUser
? 'Create the first admin account'
: 'Create your account'}
</p>
</div>
<div className="rounded-lg border border-border bg-card p-6 space-y-4">
{error && (
<Alert variant="destructive">
<AlertCircle className="h-4 w-4" />
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="name" className="text-sm text-muted-foreground">Name</Label>
<Input
id="name"
type="text"
placeholder="Your name"
className="bg-input border-border focus:border-primary"
{...register('name')}
aria-invalid={!!errors.name}
/>
{errors.name && (
<p className="text-sm text-destructive">{errors.name.message}</p>
)}
</div>
<div className="space-y-2">
<Label htmlFor="email" className="text-sm text-muted-foreground">Email</Label>
<Input
id="email"
type="email"
placeholder="you@example.com"
className="bg-input border-border focus:border-primary"
{...register('email')}
aria-invalid={!!errors.email}
/>
{errors.email && (
<p className="text-sm text-destructive">{errors.email.message}</p>
)}
</div>
<div className="space-y-2">
<Label htmlFor="password" className="text-sm text-muted-foreground">Password</Label>
<Input
id="password"
type="password"
placeholder="At least 8 characters"
className="bg-input border-border focus:border-primary"
{...register('password')}
aria-invalid={!!errors.password}
/>
{errors.password && (
<p className="text-sm text-destructive">{errors.password.message}</p>
)}
</div>
<div className="space-y-2">
<Label htmlFor="confirmPassword" className="text-sm text-muted-foreground">Confirm Password</Label>
<Input
id="confirmPassword"
type="password"
placeholder="Confirm your password"
className="bg-input border-border focus:border-primary"
{...register('confirmPassword')}
aria-invalid={!!errors.confirmPassword}
/>
{errors.confirmPassword && (
<p className="text-sm text-destructive">{errors.confirmPassword.message}</p>
)}
</div>
<Button type="submit" className="w-full" disabled={isSubmitting}>
{isSubmitting ? (
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
) : (
<UserPlus className="mr-2 h-4 w-4" />
)}
{config.isFirstUser ? 'Create Admin Account' : 'Create Account'}
</Button>
</form>
</div>
<p className="text-center text-sm text-muted-foreground">
Already have an account?{' '}
<Link to="/login" className="text-primary hover:underline transition-colors">
Sign in
</Link>
</p>
</div>
</div>
)
}