-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathSetup.tsx
More file actions
124 lines (116 loc) · 4.44 KB
/
Setup.tsx
File metadata and controls
124 lines (116 loc) · 4.44 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
import { useState } from 'react'
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'
const setupSchema = 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'),
})
type SetupFormData = z.infer<typeof setupSchema>
export function Setup() {
const { signUpWithEmail } = useAuth()
const theme = useTheme()
const [error, setError] = useState<string | null>(null)
const [isSubmitting, setIsSubmitting] = useState(false)
const {
register,
handleSubmit,
formState: { errors },
} = useForm<SetupFormData>({
resolver: standardSchemaResolver(setupSchema),
})
const onSubmit = async (data: SetupFormData) => {
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">Create Admin 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="admin@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>
<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" />
)}
Create Admin Account
</Button>
</form>
</div>
</div>
</div>
)
}