2025-11-04 09:14:29 +01:00
|
|
|
"use client"
|
|
|
|
|
|
|
|
|
|
import type React from "react"
|
|
|
|
|
|
2025-11-07 19:25:36 +01:00
|
|
|
import { useState, useEffect } 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"
|
2025-11-07 19:25:36 +01:00
|
|
|
import { Checkbox } from "./ui/checkbox"
|
2025-11-04 09:14:29 +01:00
|
|
|
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("")
|
2025-11-07 19:25:36 +01:00
|
|
|
const [rememberMe, setRememberMe] = useState(false)
|
2025-11-04 09:14:29 +01:00
|
|
|
const [error, setError] = useState("")
|
|
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
|
|
2025-11-07 19:25:36 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
const savedUsername = localStorage.getItem("proxmenux-saved-username")
|
|
|
|
|
const savedPassword = localStorage.getItem("proxmenux-saved-password")
|
|
|
|
|
|
|
|
|
|
if (savedUsername && savedPassword) {
|
|
|
|
|
setUsername(savedUsername)
|
|
|
|
|
setPassword(savedPassword)
|
|
|
|
|
setRememberMe(true)
|
|
|
|
|
|
|
|
|
|
// Auto-login si hay credenciales guardadas
|
|
|
|
|
handleAutoLogin(savedUsername, savedPassword)
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
const handleAutoLogin = async (user: string, pass: string) => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(getApiUrl("/api/auth/login"), {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: JSON.stringify({ username: user, password: pass, remember_me: true }),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const data = await response.json()
|
|
|
|
|
|
|
|
|
|
if (response.ok && data.token) {
|
|
|
|
|
localStorage.setItem("proxmenux-auth-token", data.token)
|
|
|
|
|
onLogin()
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log("Auto-login failed, showing login form")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-04 09:14:29 +01:00
|
|
|
const handleLogin = async (e: React.FormEvent) => {
|
|
|
|
|
e.preventDefault()
|
|
|
|
|
setError("")
|
|
|
|
|
|
|
|
|
|
if (!username || !password) {
|
2025-11-07 19:25:36 +01:00
|
|
|
setError("Por favor, introduce usuario y contraseña")
|
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 19:25:36 +01:00
|
|
|
body: JSON.stringify({ username, password, remember_me: rememberMe }),
|
2025-11-04 09:14:29 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const data = await response.json()
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
2025-11-07 19:25:36 +01:00
|
|
|
throw new Error(data.error || "Fallo en el login")
|
2025-11-04 09:14:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save token
|
|
|
|
|
localStorage.setItem("proxmenux-auth-token", data.token)
|
2025-11-07 19:25:36 +01:00
|
|
|
|
|
|
|
|
if (rememberMe) {
|
|
|
|
|
localStorage.setItem("proxmenux-saved-username", username)
|
|
|
|
|
localStorage.setItem("proxmenux-saved-password", password)
|
|
|
|
|
} else {
|
|
|
|
|
// Limpiar credenciales guardadas si no se marcó recordar
|
|
|
|
|
localStorage.removeItem("proxmenux-saved-username")
|
|
|
|
|
localStorage.removeItem("proxmenux-saved-password")
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-04 09:14:29 +01:00
|
|
|
onLogin()
|
|
|
|
|
} catch (err) {
|
2025-11-07 19:25:36 +01:00
|
|
|
setError(err instanceof Error ? err.message : "Fallo en el login")
|
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">
|
|
|
|
|
<Label htmlFor="login-username">Username</Label>
|
|
|
|
|
<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)}
|
|
|
|
|
className="pl-10"
|
|
|
|
|
disabled={loading}
|
|
|
|
|
autoComplete="username"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<Label htmlFor="login-password">Password</Label>
|
|
|
|
|
<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)}
|
|
|
|
|
className="pl-10"
|
|
|
|
|
disabled={loading}
|
|
|
|
|
autoComplete="current-password"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-11-07 19:25:36 +01:00
|
|
|
<div className="flex items-center space-x-2">
|
|
|
|
|
<Checkbox
|
|
|
|
|
id="remember-me"
|
|
|
|
|
checked={rememberMe}
|
|
|
|
|
onCheckedChange={(checked) => setRememberMe(checked as boolean)}
|
|
|
|
|
disabled={loading}
|
|
|
|
|
/>
|
|
|
|
|
<Label htmlFor="remember-me" className="text-sm font-normal cursor-pointer select-none">
|
|
|
|
|
Remember me
|
|
|
|
|
</Label>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-11-04 09:14:29 +01:00
|
|
|
<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>
|
|
|
|
|
)
|
|
|
|
|
}
|