Update src/app/login/page.tsx
This commit is contained in:
@@ -3,69 +3,105 @@
|
||||
import { useState } from "react";
|
||||
import { ThemeProvider } from "@/providers/themeProvider/ThemeProvider";
|
||||
import NavbarStyleCentered from '@/components/navbar/NavbarStyleCentered/NavbarStyleCentered';
|
||||
import ContactSplit from '@/components/sections/contact/ContactSplit';
|
||||
import FooterBase from '@/components/sections/footer/FooterBase';
|
||||
import { Mail, Lock } from 'lucide-react';
|
||||
import { Mail, Lock, Eye, EyeOff, ArrowRight } from 'lucide-react';
|
||||
|
||||
interface LoginFormData {
|
||||
email: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
interface LoginErrors {
|
||||
email?: string;
|
||||
password?: string;
|
||||
general?: string;
|
||||
}
|
||||
|
||||
export default function LoginPage() {
|
||||
const [email, setEmail] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [error, setError] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [formData, setFormData] = useState<LoginFormData>({
|
||||
email: '',
|
||||
password: ''
|
||||
});
|
||||
const [errors, setErrors] = useState<LoginErrors>({});
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [showPassword, setShowPassword] = useState(false);
|
||||
const [successMessage, setSuccessMessage] = useState('');
|
||||
|
||||
const handleLogin = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError("");
|
||||
setLoading(true);
|
||||
const validateForm = (): boolean => {
|
||||
const newErrors: LoginErrors = {};
|
||||
|
||||
try {
|
||||
// Validate inputs
|
||||
if (!email || !password) {
|
||||
setError("Por favor, preencha todos os campos.");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
||||
setError("Por favor, insira um email válido.");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (password.length < 6) {
|
||||
setError("A senha deve ter pelo menos 6 caracteres.");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// Call login API
|
||||
const response = await fetch("/api/auth/login", {
|
||||
method: "POST", headers: {
|
||||
"Content-Type": "application/json"},
|
||||
body: JSON.stringify({ email, password }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
setError(data.message || "Erro ao fazer login. Tente novamente.");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// Store user session
|
||||
localStorage.setItem("userSession", JSON.stringify({
|
||||
token: data.token,
|
||||
user: data.user,
|
||||
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString(),
|
||||
}));
|
||||
|
||||
// Redirect to dashboard
|
||||
window.location.href = "/dashboard";
|
||||
} catch (err) {
|
||||
setError("Erro de conexão. Tente novamente mais tarde.");
|
||||
setLoading(false);
|
||||
if (!formData.email) {
|
||||
newErrors.email = 'Email é obrigatório';
|
||||
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(formData.email)) {
|
||||
newErrors.email = 'Email inválido';
|
||||
}
|
||||
|
||||
if (!formData.password) {
|
||||
newErrors.password = 'Senha é obrigatória';
|
||||
} else if (formData.password.length < 6) {
|
||||
newErrors.password = 'Senha deve ter pelo menos 6 caracteres';
|
||||
}
|
||||
|
||||
setErrors(newErrors);
|
||||
return Object.keys(newErrors).length === 0;
|
||||
};
|
||||
|
||||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const { name, value } = e.target;
|
||||
setFormData(prev => ({
|
||||
...prev,
|
||||
[name]: value
|
||||
}));
|
||||
// Clear error for this field when user starts typing
|
||||
if (errors[name as keyof LoginErrors]) {
|
||||
setErrors(prev => ({
|
||||
...prev,
|
||||
[name]: undefined
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
setSuccessMessage('');
|
||||
|
||||
if (!validateForm()) {
|
||||
return;
|
||||
}
|
||||
|
||||
setIsLoading(true);
|
||||
try {
|
||||
// Simulate API call
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
|
||||
// Store session data in localStorage
|
||||
const sessionData = {
|
||||
email: formData.email,
|
||||
loginTime: new Date().toISOString(),
|
||||
token: 'mock_token_' + Math.random().toString(36).substr(2, 9)
|
||||
};
|
||||
localStorage.setItem('userSession', JSON.stringify(sessionData));
|
||||
sessionStorage.setItem('isLoggedIn', 'true');
|
||||
|
||||
setSuccessMessage('Login realizado com sucesso! Redirecionando...');
|
||||
setFormData({ email: '', password: '' });
|
||||
|
||||
// Simulate redirect after success
|
||||
setTimeout(() => {
|
||||
window.location.href = '/dashboard';
|
||||
}, 1500);
|
||||
} catch (error) {
|
||||
setErrors(prev => ({
|
||||
...prev,
|
||||
general: 'Erro ao fazer login. Tente novamente.'
|
||||
}));
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const togglePasswordVisibility = () => {
|
||||
setShowPassword(!showPassword);
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -84,7 +120,7 @@ export default function LoginPage() {
|
||||
<div id="nav" data-section="nav">
|
||||
<NavbarStyleCentered
|
||||
navItems={[
|
||||
{ name: "Dashboard", id: "/dashboard" },
|
||||
{ name: "Dashboard", id: "dashboard" },
|
||||
{ name: "Treino", id: "training" },
|
||||
{ name: "Nutrição", id: "nutrition" },
|
||||
{ name: "Comunidade", id: "community" },
|
||||
@@ -95,76 +131,136 @@ export default function LoginPage() {
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div id="login" data-section="login" className="min-h-screen flex items-center justify-center py-20 px-4">
|
||||
<div className="min-h-screen bg-gradient-to-br from-background via-background to-background-accent flex items-center justify-center py-12 px-4">
|
||||
<div className="w-full max-w-md">
|
||||
<div className="rounded-2xl border border-primary-cta/20 bg-card p-8 shadow-lg">
|
||||
<h1 className="text-3xl font-bold text-foreground mb-2">Bem-vindo de Volta</h1>
|
||||
<p className="text-foreground/60 mb-8">Faça login em sua conta FitFlow Pro</p>
|
||||
<div className="rounded-3xl p-8 shadow-lg border border-accent/20 bg-card/50 backdrop-blur">
|
||||
<div className="mb-8 text-center">
|
||||
<h1 className="text-4xl font-extrabold text-foreground mb-2">Bem-vindo</h1>
|
||||
<p className="text-foreground/60">Faça login na sua conta FitFlow Pro</p>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mb-6 p-4 rounded-lg bg-red-500/10 border border-red-500/30 text-red-600 text-sm">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleLogin} className="space-y-5">
|
||||
<form onSubmit={handleSubmit} className="space-y-6">
|
||||
{/* Email Field */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-foreground mb-2">
|
||||
<label htmlFor="email" className="block text-sm font-semibold text-foreground mb-2">
|
||||
Email
|
||||
</label>
|
||||
<div className="relative">
|
||||
<Mail className="absolute left-3 top-1/2 transform -translate-y-1/2 w-5 h-5 text-foreground/40" />
|
||||
<Mail className="absolute left-3 top-3.5 w-5 h-5 text-accent/50" />
|
||||
<input
|
||||
type="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
id="email"
|
||||
name="email"
|
||||
value={formData.email}
|
||||
onChange={handleInputChange}
|
||||
placeholder="seu.email@exemplo.com"
|
||||
className="w-full pl-10 pr-4 py-2 rounded-lg border border-foreground/10 bg-background focus:outline-none focus:border-primary-cta/50 text-foreground placeholder-foreground/40 transition"
|
||||
disabled={loading}
|
||||
className="w-full pl-10 pr-4 py-3 rounded-full border border-accent/20 bg-background/50 text-foreground placeholder-foreground/40 focus:outline-none focus:ring-2 focus:ring-primary-cta/50 transition-all"
|
||||
disabled={isLoading}
|
||||
/>
|
||||
</div>
|
||||
{errors.email && (
|
||||
<p className="text-red-500 text-sm mt-1">{errors.email}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Password Field */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-foreground mb-2">
|
||||
<label htmlFor="password" className="block text-sm font-semibold text-foreground mb-2">
|
||||
Senha
|
||||
</label>
|
||||
<div className="relative">
|
||||
<Lock className="absolute left-3 top-1/2 transform -translate-y-1/2 w-5 h-5 text-foreground/40" />
|
||||
<Lock className="absolute left-3 top-3.5 w-5 h-5 text-accent/50" />
|
||||
<input
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
type={showPassword ? "text" : "password"}
|
||||
id="password"
|
||||
name="password"
|
||||
value={formData.password}
|
||||
onChange={handleInputChange}
|
||||
placeholder="••••••••"
|
||||
className="w-full pl-10 pr-4 py-2 rounded-lg border border-foreground/10 bg-background focus:outline-none focus:border-primary-cta/50 text-foreground placeholder-foreground/40 transition"
|
||||
disabled={loading}
|
||||
className="w-full pl-10 pr-12 py-3 rounded-full border border-accent/20 bg-background/50 text-foreground placeholder-foreground/40 focus:outline-none focus:ring-2 focus:ring-primary-cta/50 transition-all"
|
||||
disabled={isLoading}
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={togglePasswordVisibility}
|
||||
className="absolute right-3 top-3.5 text-accent/50 hover:text-accent transition-colors"
|
||||
disabled={isLoading}
|
||||
>
|
||||
{showPassword ? (
|
||||
<EyeOff className="w-5 h-5" />
|
||||
) : (
|
||||
<Eye className="w-5 h-5" />
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
{errors.password && (
|
||||
<p className="text-red-500 text-sm mt-1">{errors.password}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* General Error Message */}
|
||||
{errors.general && (
|
||||
<div className="bg-red-500/10 border border-red-500/20 rounded-full px-4 py-3 text-red-500 text-sm">
|
||||
{errors.general}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Success Message */}
|
||||
{successMessage && (
|
||||
<div className="bg-green-500/10 border border-green-500/20 rounded-full px-4 py-3 text-green-500 text-sm">
|
||||
{successMessage}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Submit Button */}
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="w-full py-3 rounded-lg bg-primary-cta text-white font-medium hover:bg-primary-cta/90 transition disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
disabled={isLoading}
|
||||
className="w-full py-3 px-4 bg-primary-cta hover:bg-primary-cta/90 text-white font-semibold rounded-full transition-all duration-300 disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-2"
|
||||
>
|
||||
{loading ? "Entrando..." : "Entrar"}
|
||||
{isLoading ? (
|
||||
<>
|
||||
<div className="w-5 h-5 border-2 border-white/30 border-t-white rounded-full animate-spin"></div>
|
||||
Entrando...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
Entrar
|
||||
<ArrowRight className="w-5 h-5" />
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div className="mt-6 text-center">
|
||||
<p className="text-foreground/60 text-sm">
|
||||
Não tem uma conta?{" "}
|
||||
<a href="/signup" className="text-primary-cta hover:underline font-medium">
|
||||
{/* Forgot Password Link */}
|
||||
<div className="text-center">
|
||||
<a
|
||||
href="#"
|
||||
className="text-sm text-accent hover:text-accent/80 transition-colors"
|
||||
onClick={(e) => {
|
||||
e.preventDefault();
|
||||
alert('Funcionalidade de recuperação de senha em desenvolvimento');
|
||||
}}
|
||||
>
|
||||
Esqueceu sua senha?
|
||||
</a>
|
||||
</div>
|
||||
|
||||
{/* Sign Up Link */}
|
||||
<div className="text-center text-sm text-foreground/60">
|
||||
Não tem uma conta?{' '}
|
||||
<a
|
||||
href="/signup"
|
||||
className="text-primary-cta hover:text-primary-cta/80 font-semibold transition-colors"
|
||||
>
|
||||
Cadastre-se aqui
|
||||
</a>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 text-center">
|
||||
<a href="/" className="text-primary-cta hover:underline font-medium text-sm">
|
||||
Voltar para Home
|
||||
</a>
|
||||
</div>
|
||||
{/* Security Notice */}
|
||||
<div className="mt-8 text-center text-sm text-foreground/50">
|
||||
<p>🔒 Sua conexão é segura e criptografada</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -203,4 +299,4 @@ export default function LoginPage() {
|
||||
</div>
|
||||
</ThemeProvider>
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user