|
| 1 | +import { Package } from 'lucide-react' |
| 2 | +import { useState } from 'react' |
| 3 | +import { useNavigate } from 'react-router-dom' |
| 4 | + |
| 5 | +import { Button } from '@/shared/components/ui/Button' |
| 6 | +import { Input } from '@/shared/components/ui/Input' |
| 7 | +import { setToken } from '@/shared/utils/auth' |
| 8 | +import { generateToken } from '@/shared/utils/jwt' |
| 9 | + |
| 10 | +const VALID_USERNAME = import.meta.env.VITE_LOGIN_USERNAME ?? '' |
| 11 | +const VALID_PASSWORD = import.meta.env.VITE_LOGIN_PASSWORD ?? '' |
| 12 | + |
| 13 | +export default function LoginPage() { |
| 14 | + const navigate = useNavigate() |
| 15 | + const [username, setUsername] = useState('') |
| 16 | + const [password, setPassword] = useState('') |
| 17 | + const [error, setError] = useState<string | null>(null) |
| 18 | + const [loading, setLoading] = useState(false) |
| 19 | + |
| 20 | + const handleSubmit = async (e: React.FormEvent) => { |
| 21 | + e.preventDefault() |
| 22 | + setError(null) |
| 23 | + |
| 24 | + if (username !== VALID_USERNAME || password !== VALID_PASSWORD) { |
| 25 | + setError('Usuário ou senha inválidos.') |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + setLoading(true) |
| 30 | + try { |
| 31 | + const token = await generateToken() |
| 32 | + setToken(token) |
| 33 | + navigate('/', { replace: true }) |
| 34 | + } catch { |
| 35 | + setError('Erro ao gerar sessão. Tente novamente.') |
| 36 | + } finally { |
| 37 | + setLoading(false) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return ( |
| 42 | + <div className="min-h-screen bg-estoquei-bg flex items-center justify-center p-4"> |
| 43 | + <div className="w-full max-w-sm"> |
| 44 | + <div className="flex flex-col items-center gap-2 mb-8"> |
| 45 | + <div className="w-10 h-10 bg-estoquei-accent rounded-lg flex items-center justify-center"> |
| 46 | + <Package size={22} aria-hidden /> |
| 47 | + </div> |
| 48 | + <h1 className="text-estoquei-text text-xl font-semibold">estoquei</h1> |
| 49 | + <p className="text-estoquei-text3 text-sm">Faça login para continuar</p> |
| 50 | + </div> |
| 51 | + |
| 52 | + <form |
| 53 | + onSubmit={handleSubmit} |
| 54 | + className="bg-estoquei-bg2 border border-estoquei-border rounded-lg p-6 flex flex-col gap-4" |
| 55 | + > |
| 56 | + {error && ( |
| 57 | + <p role="alert" className="text-estoquei-danger text-sm"> |
| 58 | + {error} |
| 59 | + </p> |
| 60 | + )} |
| 61 | + |
| 62 | + <div className="flex flex-col gap-1"> |
| 63 | + <label htmlFor="username" className="text-estoquei-text3 text-xs font-medium uppercase tracking-[.08em]"> |
| 64 | + Usuário |
| 65 | + </label> |
| 66 | + <Input |
| 67 | + id="username" |
| 68 | + type="text" |
| 69 | + placeholder="Digite seu usuário" |
| 70 | + autoComplete="username" |
| 71 | + value={username} |
| 72 | + onChange={(e) => setUsername(e.target.value)} |
| 73 | + /> |
| 74 | + </div> |
| 75 | + |
| 76 | + <div className="flex flex-col gap-1"> |
| 77 | + <label htmlFor="password" className="text-estoquei-text3 text-xs font-medium uppercase tracking-[.08em]"> |
| 78 | + Senha |
| 79 | + </label> |
| 80 | + <Input |
| 81 | + id="password" |
| 82 | + type="password" |
| 83 | + placeholder="Digite sua senha" |
| 84 | + autoComplete="current-password" |
| 85 | + value={password} |
| 86 | + onChange={(e) => setPassword(e.target.value)} |
| 87 | + /> |
| 88 | + </div> |
| 89 | + |
| 90 | + <Button |
| 91 | + type="submit" |
| 92 | + variant="accent" |
| 93 | + className="w-full mt-2" |
| 94 | + loading={loading} |
| 95 | + > |
| 96 | + Entrar |
| 97 | + </Button> |
| 98 | + </form> |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + ) |
| 102 | +} |
0 commit comments