Files
mantisbase-admin/components/setup-form.tsx

190 lines
6.6 KiB
TypeScript

"use client"
import * as React from "react"
import { Eye, EyeOff, CheckCircle2 } from "lucide-react"
import { Logo } from "./logo"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Alert, AlertDescription } from "@/components/ui/alert"
import { ApiClient } from "@/lib/api"
import { useAppState } from "@/lib/app-state"
import { useToast } from "@/hooks/use-toast"
interface SetupFormProps {
token: string
onSetupComplete: () => void
}
export function SetupForm({ token, onSetupComplete }: SetupFormProps) {
const [email, setEmail] = React.useState("")
const [password, setPassword] = React.useState("")
const [confirmPassword, setConfirmPassword] = React.useState("")
const [showPassword, setShowPassword] = React.useState(false)
const [showConfirmPassword, setShowConfirmPassword] = React.useState(false)
const [isLoading, setIsLoading] = React.useState(false)
const [error, setError] = React.useState("")
const [success, setSuccess] = React.useState(false)
const { mode } = useAppState()
const { toast } = useToast()
const apiClient = React.useMemo(() => {
return new ApiClient(
() => {},
(error) => setError(error),
token,
)
}, [token])
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
setIsLoading(true)
setError("")
if (password !== confirmPassword) {
setError("Passwords do not match")
setIsLoading(false)
return
}
if (password.length < 8) {
setError("Password must be at least 8 characters long")
setIsLoading(false)
return
}
try {
// Create admin account using the setup token
const response: any = await apiClient.call("/api/v1/sys/admins/setup", {
method: "POST",
body: JSON.stringify({
email,
password,
}),
})
// Check if response has an error field (ApiClient may return error in catch block)
if (response?.error && response.error.length > 0) {
throw new Error(response.error)
}
// If we get here, the account was created successfully
setSuccess(true)
toast({
title: "Account Created",
description: "Your admin account has been created successfully. You can now sign in.",
})
// Wait a moment then redirect to login
setTimeout(() => {
onSetupComplete()
}, 2000)
} catch (err: any) {
// Handle API errors - ApiClient throws errors for failed requests
const errorMessage = err.message || "Failed to create admin account"
setError(errorMessage)
console.error("Setup error:", err)
} finally {
setIsLoading(false)
}
}
return (
<div className="min-h-screen flex items-center justify-center bg-background">
<Card className="w-full max-w-md">
<CardHeader className="text-center">
<div className="flex justify-center mb-4">
<Logo className="h-12 w-12 text-primary" size={48} />
</div>
<CardTitle className="text-2xl">Setup Admin Account</CardTitle>
<CardDescription>Create your first admin account to get started</CardDescription>
</CardHeader>
<CardContent>
{success ? (
<div className="text-center space-y-4 py-4">
<CheckCircle2 className="h-16 w-16 text-green-500 mx-auto" />
<p className="text-lg font-medium">Account created successfully!</p>
<p className="text-sm text-muted-foreground">Redirecting to login...</p>
</div>
) : (
<form onSubmit={handleSubmit} className="space-y-4">
{error && (
<Alert variant="destructive">
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
<div className="space-y-2">
<Label htmlFor="email">Email</Label>
<Input
id="email"
type="email"
placeholder="admin@example.com"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/>
</div>
<div className="space-y-2">
<Label htmlFor="password">Password</Label>
<div className="relative">
<Input
id="password"
type={showPassword ? "text" : "password"}
placeholder="Enter your password (min. 8 characters)"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
minLength={8}
/>
<Button
type="button"
variant="ghost"
size="sm"
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
onClick={() => setShowPassword(!showPassword)}
>
{showPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
</Button>
</div>
</div>
<div className="space-y-2">
<Label htmlFor="confirm-password">Confirm Password</Label>
<div className="relative">
<Input
id="confirm-password"
type={showConfirmPassword ? "text" : "password"}
placeholder="Confirm your password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
required
minLength={8}
/>
<Button
type="button"
variant="ghost"
size="sm"
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
onClick={() => setShowConfirmPassword(!showConfirmPassword)}
>
{showConfirmPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
</Button>
</div>
</div>
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? "Creating account..." : "Create Admin Account"}
</Button>
</form>
)}
</CardContent>
</Card>
</div>
)
}