xpeditis2.0/apps/frontend/app/login/page.tsx
2026-01-27 19:33:51 +01:00

465 lines
16 KiB
TypeScript

/**
* Login Page - Xpeditis
*
* Modern split-screen login page with:
* - Left side: Login form with social authentication
* - Right side: Brand features and visual elements
*/
'use client';
import { useState } from 'react';
import Link from 'next/link';
import Image from 'next/image';
import { useAuth } from '@/lib/context/auth-context';
interface FieldErrors {
email?: string;
password?: string;
}
// Map backend error messages to French user-friendly messages
function getErrorMessage(error: any): { message: string; field?: 'email' | 'password' | 'general' } {
const errorMessage = error?.message || error?.response?.message || '';
// Network or server errors
if (error?.name === 'TypeError' || errorMessage.includes('fetch')) {
return {
message: 'Impossible de se connecter au serveur. Vérifiez votre connexion internet.',
field: 'general'
};
}
// Backend error messages
if (errorMessage.includes('Invalid credentials') || errorMessage.includes('Identifiants')) {
return {
message: 'Email ou mot de passe incorrect',
field: 'general'
};
}
if (errorMessage.includes('inactive') || errorMessage.includes('désactivé')) {
return {
message: 'Votre compte a été désactivé. Contactez le support pour plus d\'informations.',
field: 'general'
};
}
if (errorMessage.includes('not found') || errorMessage.includes('introuvable')) {
return {
message: 'Aucun compte trouvé avec cet email',
field: 'email'
};
}
if (errorMessage.includes('password') || errorMessage.includes('mot de passe')) {
return {
message: 'Mot de passe incorrect',
field: 'password'
};
}
if (errorMessage.includes('Too many') || errorMessage.includes('rate limit')) {
return {
message: 'Trop de tentatives de connexion. Veuillez réessayer dans quelques minutes.',
field: 'general'
};
}
// Default error
return {
message: errorMessage || 'Une erreur est survenue. Veuillez réessayer.',
field: 'general'
};
}
export default function LoginPage() {
const { login } = useAuth();
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [rememberMe, setRememberMe] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState('');
const [fieldErrors, setFieldErrors] = useState<FieldErrors>({});
// Validate form fields
const validateForm = (): boolean => {
const errors: FieldErrors = {};
// Email validation
if (!email.trim()) {
errors.email = 'L\'adresse email est requise';
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
errors.email = 'L\'adresse email n\'est pas valide';
}
// Password validation
if (!password) {
errors.password = 'Le mot de passe est requis';
} else if (password.length < 6) {
errors.password = 'Le mot de passe doit contenir au moins 6 caractères';
}
setFieldErrors(errors);
return Object.keys(errors).length === 0;
};
// Handle input changes - keep errors visible until successful login
const handleEmailChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setEmail(e.target.value);
};
const handlePasswordChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setPassword(e.target.value);
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError('');
setFieldErrors({});
// Validate form before submission
if (!validateForm()) {
return;
}
setIsLoading(true);
try {
await login(email, password);
// Navigation is handled by the login function in auth context
} catch (err: any) {
const { message, field } = getErrorMessage(err);
if (field === 'email') {
setFieldErrors({ email: message });
} else if (field === 'password') {
setFieldErrors({ password: message });
} else {
setError(message);
}
} finally {
setIsLoading(false);
}
};
return (
<div className="min-h-screen flex">
{/* Left Side - Form */}
<div className="w-full lg:w-1/2 flex flex-col justify-center px-8 sm:px-12 lg:px-16 xl:px-24 bg-white">
<div className="max-w-md w-full mx-auto">
{/* Logo */}
<div className="mb-10">
<Link href="/">
<Image
src="/assets/logos/logo-black.svg"
alt="Xpeditis"
width={50}
height={60}
priority
className="h-auto"
/>
</Link>
</div>
{/* Header */}
<div className="mb-8">
<h1 className="text-h1 text-brand-navy mb-2">Connexion</h1>
<p className="text-body text-neutral-600">
Bienvenue ! Connectez-vous pour accéder à votre compte
</p>
</div>
{/* Error Message */}
{error && (
<div className="mb-6 p-4 bg-red-50 border border-red-200 rounded-lg flex items-start gap-3">
<svg
className="w-5 h-5 text-red-600 flex-shrink-0 mt-0.5"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
<p className="text-body-sm text-red-800">{error}</p>
</div>
)}
{/* Form */}
<form onSubmit={handleSubmit} className="space-y-6">
{/* Email */}
<div>
<label htmlFor="email" className={`label ${fieldErrors.email ? 'text-red-600' : ''}`}>
Adresse email
</label>
<input
id="email"
type="email"
value={email}
onChange={handleEmailChange}
className={`input w-full ${
fieldErrors.email
? 'border-red-500 focus:border-red-500 focus:ring-red-500 bg-red-50'
: ''
}`}
placeholder="votre.email@entreprise.com"
autoComplete="email"
disabled={isLoading}
aria-invalid={!!fieldErrors.email}
aria-describedby={fieldErrors.email ? 'email-error' : undefined}
/>
{fieldErrors.email && (
<p id="email-error" className="mt-1.5 text-body-sm text-red-600 flex items-center gap-1">
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
{fieldErrors.email}
</p>
)}
</div>
{/* Password */}
<div>
<label htmlFor="password" className={`label ${fieldErrors.password ? 'text-red-600' : ''}`}>
Mot de passe
</label>
<input
id="password"
type="password"
value={password}
onChange={handlePasswordChange}
className={`input w-full ${
fieldErrors.password
? 'border-red-500 focus:border-red-500 focus:ring-red-500 bg-red-50'
: ''
}`}
placeholder="••••••••••"
autoComplete="current-password"
disabled={isLoading}
aria-invalid={!!fieldErrors.password}
aria-describedby={fieldErrors.password ? 'password-error' : undefined}
/>
{fieldErrors.password && (
<p id="password-error" className="mt-1.5 text-body-sm text-red-600 flex items-center gap-1">
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
{fieldErrors.password}
</p>
)}
</div>
{/* Remember Me & Forgot Password */}
<div className="flex items-center justify-between">
<label className="flex items-center cursor-pointer">
<input
type="checkbox"
checked={rememberMe}
onChange={e => setRememberMe(e.target.checked)}
className="w-4 h-4 text-accent border-neutral-300 rounded focus:ring-accent focus:ring-2"
disabled={isLoading}
/>
<span className="ml-2 text-body-sm text-neutral-700">Se souvenir de moi</span>
</label>
<Link href="/forgot-password" className="text-body-sm link">
Mot de passe oublié ?
</Link>
</div>
{/* Submit Button */}
<button
type="submit"
disabled={isLoading}
className="btn-primary w-full text-lg disabled:opacity-50 disabled:cursor-not-allowed"
>
{isLoading ? 'Connexion en cours...' : 'Se connecter'}
</button>
</form>
{/* Sign Up Link */}
<div className="mt-8 text-center">
<p className="text-body text-neutral-600">
Vous n'avez pas de compte ?{' '}
<Link href="/register" className="link font-semibold">
Créer un compte
</Link>
</p>
</div>
{/* Footer Links */}
<div className="mt-8 pt-8 border-t border-neutral-200">
<div className="flex flex-wrap justify-center gap-6 text-body-sm text-neutral-500">
<Link href="/help" className="hover:text-accent transition-colors">
Centre d'aide
</Link>
<Link href="/contact" className="hover:text-accent transition-colors">
Contactez-nous
</Link>
<Link href="/privacy" className="hover:text-accent transition-colors">
Confidentialité
</Link>
<Link href="/terms" className="hover:text-accent transition-colors">
Conditions
</Link>
</div>
</div>
</div>
</div>
{/* Right Side - Brand Features */}
<div className="hidden lg:block lg:w-1/2 relative bg-brand-navy">
{/* Background Gradient */}
<div className="absolute inset-0 bg-gradient-to-br from-brand-navy to-neutral-800 opacity-95"></div>
{/* Content */}
<div className="absolute inset-0 flex flex-col justify-center px-16 xl:px-24 text-white">
<div className="max-w-xl">
<h2 className="text-display-sm mb-6 text-white">Simplifiez votre fret maritime</h2>
<p className="text-body-lg text-neutral-200 mb-12">
Accédez à des tarifs en temps réel de plus de 50 compagnies maritimes. Réservez,
suivez et gérez vos expéditions LCL en quelques clics.
</p>
{/* Features */}
<div className="space-y-6">
<div className="flex items-start">
<div className="flex-shrink-0 w-12 h-12 bg-brand-turquoise rounded-lg flex items-center justify-center">
<svg
className="w-6 h-6 text-white"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M13 10V3L4 14h7v7l9-11h-7z"
/>
</svg>
</div>
<div className="ml-4">
<h3 className="text-h5 mb-1 text-white">Tarifs instantanés</h3>
<p className="text-body-sm text-neutral-300">
Comparez les prix de toutes les compagnies en temps réel
</p>
</div>
</div>
<div className="flex items-start">
<div className="flex-shrink-0 w-12 h-12 bg-brand-turquoise rounded-lg flex items-center justify-center">
<svg
className="w-6 h-6 text-white"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
</div>
<div className="ml-4">
<h3 className="text-h5 mb-1 text-white">Réservation simplifiée</h3>
<p className="text-body-sm text-neutral-300">
Réservez vos conteneurs en moins de 5 minutes
</p>
</div>
</div>
<div className="flex items-start">
<div className="flex-shrink-0 w-12 h-12 bg-brand-turquoise rounded-lg flex items-center justify-center">
<svg
className="w-6 h-6 text-white"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M17 8h2a2 2 0 012 2v6a2 2 0 01-2 2h-2v4l-4-4H9a1.994 1.994 0 01-1.414-.586m0 0L11 14h4a2 2 0 002-2V6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2v4l.586-.586z"
/>
</svg>
</div>
<div className="ml-4">
<h3 className="text-h5 mb-1. text-white">Suivi en temps réel</h3>
<p className="text-body-sm text-neutral-300">
Suivez vos expéditions à chaque étape du voyage
</p>
</div>
</div>
</div>
{/* Stats */}
<div className="grid grid-cols-3 gap-8 mt-12 pt-12 border-t border-neutral-700">
<div>
<div className="text-display-sm text-brand-turquoise">50+</div>
<div className="text-body-sm text-neutral-300 mt-1">Compagnies</div>
</div>
<div>
<div className="text-display-sm text-brand-turquoise">10k+</div>
<div className="text-body-sm text-neutral-300 mt-1">Expéditions</div>
</div>
<div>
<div className="text-display-sm text-brand-turquoise">99.5%</div>
<div className="text-body-sm text-neutral-300 mt-1">Satisfaction</div>
</div>
</div>
</div>
</div>
{/* Decorative Elements */}
<div className="absolute bottom-0 right-0 opacity-10">
<svg width="400" height="400" viewBox="0 0 400 400" fill="none">
<circle
cx="200"
cy="200"
r="150"
stroke="currentColor"
strokeWidth="2"
className="text-white"
/>
<circle
cx="200"
cy="200"
r="100"
stroke="currentColor"
strokeWidth="2"
className="text-white"
/>
<circle
cx="200"
cy="200"
r="50"
stroke="currentColor"
strokeWidth="2"
className="text-white"
/>
</svg>
</div>
</div>
</div>
);
}