mirror of
https://github.com/allankoechke/mantisbase-admin.git
synced 2026-11-30 10:19:37 +00:00
Add state management for TEST/PROD modes and update API client for real calls. #VERCEL_SKIP Co-authored-by: Allan K. Koech <44490960+allankoechke@users.noreply.github.com>
111 lines
3.8 KiB
TypeScript
111 lines
3.8 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import { Eye, EyeOff, Shield } from "lucide-react"
|
|
|
|
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 { loginWithPassword } from "@/lib/api"
|
|
import { useAppState } from "@/lib/app-state"
|
|
|
|
interface LoginFormProps {
|
|
onLogin: (token: string) => void
|
|
}
|
|
|
|
export function LoginForm({ onLogin }: LoginFormProps) {
|
|
const [email, setEmail] = React.useState("admin@example.com")
|
|
const [password, setPassword] = React.useState("password")
|
|
const [showPassword, setShowPassword] = React.useState(false)
|
|
const [isLoading, setIsLoading] = React.useState(false)
|
|
const [error, setError] = React.useState("")
|
|
const { mode } = useAppState()
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault()
|
|
setIsLoading(true)
|
|
setError("")
|
|
|
|
try {
|
|
const data = await loginWithPassword(email, password, mode)
|
|
localStorage.setItem("admin_token", data.token)
|
|
onLogin(data.token)
|
|
} catch (err: any) {
|
|
setError(err.message || "Invalid email or password")
|
|
} finally {
|
|
setIsLoading(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center bg-gray-50">
|
|
<Card className="w-full max-w-md">
|
|
<CardHeader className="text-center">
|
|
<div className="flex justify-center mb-4">
|
|
<Shield className="h-12 w-12 text-primary" />
|
|
</div>
|
|
<CardTitle className="text-2xl">Mantis Admin</CardTitle>
|
|
<CardDescription>Sign in to access the admin dashboard</CardDescription>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<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"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
required
|
|
/>
|
|
<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>
|
|
|
|
<Button type="submit" className="w-full" disabled={isLoading}>
|
|
{isLoading ? "Signing in..." : "Sign In"}
|
|
</Button>
|
|
|
|
<div className="text-sm text-muted-foreground text-center">
|
|
<p>Demo credentials:</p>
|
|
<p>Email: admin@example.com</p>
|
|
<p>Password: password</p>
|
|
<p className="mt-2 text-xs">Mode: {mode}</p>
|
|
</div>
|
|
</form>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
)
|
|
}
|