Files
opencode-manager/frontend/src/pages/Setup.tsx
T
Chris Scott 81a66d2ddb Add single-user authentication system with Better Auth integration (#76)
* Add Better Auth integration with session management

- Implement user authentication using Better Auth library
- Add login, register, and sign out functionality
- Support passkey/WebAuthn authentication
- Create database schema for users, sessions, accounts, passkeys
- Add AuthProvider context and ProtectedRoute components
- Add account settings page in settings dialog
- Protect all API routes with authentication middleware
- Update environment configuration for auth providers

* Add single-user authentication system with admin setup

- Add first-run Setup page for initial admin account creation
- Add environment-based admin configuration (ADMIN_EMAIL, ADMIN_PASSWORD)
- Add password reset capability via ADMIN_PASSWORD_RESET environment flag
- Migrate to data routers with auth loaders, remove ProtectedRoute
- Add change password functionality to account settings
- Enforce AUTH_SECRET requirement in production
- Auto-disable registration when admin is pre-configured via environment

* Track busy/idle session status across hooks

* Bump version to 0.7.5

* Update pnpm to 10.28.1
2026-01-24 17:45:44 -05:00

125 lines
4.4 KiB
TypeScript

import { useState } from 'react'
import { useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
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: zodResolver(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>
)
}