|
| 1 | +import { useState } from 'react' |
| 2 | +import { useNavigate } from 'react-router-dom' |
| 3 | +import { Database } from 'lucide-react' |
| 4 | +import { useLogin } from '@/hooks/useAuth' |
| 5 | + |
| 6 | +const inputCls = |
| 7 | + 'w-full rounded-md border px-3 py-2 text-sm outline-none transition-colors focus:border-[var(--accent)]' |
| 8 | +const inputSt = { |
| 9 | + backgroundColor: 'var(--bg-base)', |
| 10 | + borderColor: 'var(--border)', |
| 11 | + color: 'var(--text-primary)', |
| 12 | +} |
| 13 | + |
| 14 | +export function LoginPage() { |
| 15 | + const navigate = useNavigate() |
| 16 | + const login = useLogin() |
| 17 | + const [username, setUsername] = useState('') |
| 18 | + const [password, setPassword] = useState('') |
| 19 | + const [rememberMe, setRememberMe] = useState(false) |
| 20 | + const [error, setError] = useState('') |
| 21 | + |
| 22 | + function handleSubmit(e: React.FormEvent) { |
| 23 | + e.preventDefault() |
| 24 | + setError('') |
| 25 | + login.mutate({ username, password, rememberMe }, { |
| 26 | + onSuccess: () => { |
| 27 | + navigate('/', { replace: true }) |
| 28 | + }, |
| 29 | + onError: () => { |
| 30 | + setError('Invalid username or password') |
| 31 | + }, |
| 32 | + }) |
| 33 | + } |
| 34 | + |
| 35 | + return ( |
| 36 | + <div |
| 37 | + className="min-h-screen flex items-center justify-center p-4" |
| 38 | + style={{ backgroundColor: 'var(--bg-base)' }} |
| 39 | + > |
| 40 | + <div |
| 41 | + className="w-full max-w-sm rounded-lg border p-8" |
| 42 | + style={{ backgroundColor: 'var(--bg-surface)', borderColor: 'var(--border)' }} |
| 43 | + > |
| 44 | + {/* Logo */} |
| 45 | + <div className="flex items-center gap-2 mb-7"> |
| 46 | + <div |
| 47 | + className="w-7 h-7 rounded flex items-center justify-center flex-shrink-0" |
| 48 | + style={{ backgroundColor: 'var(--accent)' }} |
| 49 | + > |
| 50 | + <Database className="w-4 h-4 text-white" /> |
| 51 | + </div> |
| 52 | + <span className="text-sm font-semibold" style={{ color: 'var(--text-primary)' }}> |
| 53 | + Backup Manager |
| 54 | + </span> |
| 55 | + </div> |
| 56 | + |
| 57 | + <h1 className="text-base font-semibold mb-1" style={{ color: 'var(--text-primary)' }}> |
| 58 | + Sign in |
| 59 | + </h1> |
| 60 | + <p className="text-xs mb-6" style={{ color: 'var(--text-muted)' }}> |
| 61 | + Enter your credentials to continue |
| 62 | + </p> |
| 63 | + |
| 64 | + <form onSubmit={handleSubmit} className="space-y-4"> |
| 65 | + <div> |
| 66 | + <label className="block text-xs font-medium mb-1" style={{ color: 'var(--text-secondary)' }}> |
| 67 | + Username |
| 68 | + </label> |
| 69 | + <input |
| 70 | + type="text" |
| 71 | + required |
| 72 | + className={inputCls} |
| 73 | + style={inputSt} |
| 74 | + value={username} |
| 75 | + onChange={(e) => setUsername(e.target.value)} |
| 76 | + autoFocus |
| 77 | + autoComplete="username" |
| 78 | + /> |
| 79 | + </div> |
| 80 | + |
| 81 | + <div> |
| 82 | + <label className="block text-xs font-medium mb-1" style={{ color: 'var(--text-secondary)' }}> |
| 83 | + Password |
| 84 | + </label> |
| 85 | + <input |
| 86 | + type="password" |
| 87 | + required |
| 88 | + className={inputCls} |
| 89 | + style={inputSt} |
| 90 | + value={password} |
| 91 | + onChange={(e) => setPassword(e.target.value)} |
| 92 | + autoComplete="current-password" |
| 93 | + /> |
| 94 | + </div> |
| 95 | + |
| 96 | + <label className="flex items-center gap-2 cursor-pointer select-none"> |
| 97 | + <input |
| 98 | + type="checkbox" |
| 99 | + checked={rememberMe} |
| 100 | + onChange={(e) => setRememberMe(e.target.checked)} |
| 101 | + className="w-4 h-4 rounded flex-shrink-0" |
| 102 | + style={{ accentColor: 'var(--accent)' }} |
| 103 | + /> |
| 104 | + <span className="text-xs" style={{ color: 'var(--text-secondary)' }}> |
| 105 | + Remember me for 30 days |
| 106 | + </span> |
| 107 | + </label> |
| 108 | + |
| 109 | + {error && ( |
| 110 | + <p className="text-xs" style={{ color: 'var(--error)' }}> |
| 111 | + {error} |
| 112 | + </p> |
| 113 | + )} |
| 114 | + |
| 115 | + <button |
| 116 | + type="submit" |
| 117 | + disabled={login.isPending} |
| 118 | + className="w-full py-2 text-sm font-medium rounded-md transition-colors disabled:opacity-60" |
| 119 | + style={{ backgroundColor: 'var(--accent)', color: '#fff' }} |
| 120 | + > |
| 121 | + {login.isPending ? 'Signing in…' : 'Sign in'} |
| 122 | + </button> |
| 123 | + </form> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + ) |
| 127 | +} |
0 commit comments