Files
ProxMenux/AppImage/components/login.tsx

146 lines
4.9 KiB
TypeScript
Raw Normal View History

2025-11-04 09:14:29 +01:00
"use client"
import type React from "react"
2025-11-07 20:05:29 +01:00
import { useState } from "react"
2025-11-04 09:14:29 +01:00
import { Button } from "./ui/button"
import { Input } from "./ui/input"
import { Label } from "./ui/label"
import { Lock, User, AlertCircle, Server } from "lucide-react"
import { getApiUrl } from "../lib/api-config"
import Image from "next/image"
interface LoginProps {
onLogin: () => void
}
export function Login({ onLogin }: LoginProps) {
const [username, setUsername] = useState("")
const [password, setPassword] = useState("")
const [error, setError] = useState("")
const [loading, setLoading] = useState(false)
const handleLogin = async (e: React.FormEvent) => {
e.preventDefault()
setError("")
if (!username || !password) {
2025-11-07 20:05:29 +01:00
setError("Please enter username and password")
2025-11-04 09:14:29 +01:00
return
}
setLoading(true)
try {
const response = await fetch(getApiUrl("/api/auth/login"), {
method: "POST",
headers: { "Content-Type": "application/json" },
2025-11-07 20:05:29 +01:00
body: JSON.stringify({ username, password }),
2025-11-04 09:14:29 +01:00
})
const data = await response.json()
if (!response.ok) {
2025-11-07 20:05:29 +01:00
throw new Error(data.error || "Login failed")
2025-11-04 09:14:29 +01:00
}
// Save token
localStorage.setItem("proxmenux-auth-token", data.token)
onLogin()
} catch (err) {
2025-11-07 20:05:29 +01:00
setError(err instanceof Error ? err.message : "Login failed")
2025-11-04 09:14:29 +01:00
} finally {
setLoading(false)
}
}
return (
<div className="min-h-screen bg-background flex items-center justify-center p-4">
<div className="w-full max-w-md space-y-8">
<div className="text-center space-y-4">
<div className="flex justify-center">
<div className="w-20 h-20 relative flex items-center justify-center bg-primary/10 rounded-lg">
<Image
src="/images/proxmenux-logo.png"
alt="ProxMenux Logo"
width={80}
height={80}
className="object-contain"
priority
onError={(e) => {
const target = e.target as HTMLImageElement
target.style.display = "none"
const fallback = target.parentElement?.querySelector(".fallback-icon")
if (fallback) {
fallback.classList.remove("hidden")
}
}}
/>
<Server className="h-12 w-12 text-primary absolute fallback-icon hidden" />
</div>
</div>
<div>
<h1 className="text-3xl font-bold">ProxMenux Monitor</h1>
<p className="text-muted-foreground mt-2">Sign in to access your dashboard</p>
</div>
</div>
<div className="bg-card border border-border rounded-lg p-6 shadow-lg">
<form onSubmit={handleLogin} className="space-y-4">
{error && (
<div className="bg-red-500/10 border border-red-500/20 rounded-lg p-3 flex items-start gap-2">
<AlertCircle className="h-5 w-5 text-red-500 flex-shrink-0 mt-0.5" />
<p className="text-sm text-red-500">{error}</p>
</div>
)}
<div className="space-y-2">
2025-11-07 20:19:55 +01:00
<Label htmlFor="login-username" className="text-sm">
Username
</Label>
2025-11-04 09:14:29 +01:00
<div className="relative">
<User className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
<Input
id="login-username"
type="text"
placeholder="Enter your username"
value={username}
onChange={(e) => setUsername(e.target.value)}
2025-11-07 20:19:55 +01:00
className="pl-10 text-base"
2025-11-04 09:14:29 +01:00
disabled={loading}
autoComplete="username"
/>
</div>
</div>
<div className="space-y-2">
2025-11-07 20:19:55 +01:00
<Label htmlFor="login-password" className="text-sm">
Password
</Label>
2025-11-04 09:14:29 +01:00
<div className="relative">
<Lock className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
<Input
id="login-password"
type="password"
placeholder="Enter your password"
value={password}
onChange={(e) => setPassword(e.target.value)}
2025-11-07 20:19:55 +01:00
className="pl-10 text-base"
2025-11-04 09:14:29 +01:00
disabled={loading}
autoComplete="current-password"
/>
</div>
</div>
<Button type="submit" className="w-full bg-blue-500 hover:bg-blue-600" disabled={loading}>
{loading ? "Signing in..." : "Sign In"}
</Button>
</form>
</div>
2025-11-04 11:34:46 +01:00
<p className="text-center text-sm text-muted-foreground">ProxMenux Monitor v1.0.1</p>
2025-11-04 09:14:29 +01:00
</div>
</div>
)
}