xpeditis2.0/apps/frontend/app/register/page.tsx
David 317de48765 feat: fix auth flows — reset password, 2-step register, remember me, SIRET
- Backend: add forgot-password and reset-password endpoints with token-based
  flow (1h expiry, secure random token, email via existing EmailPort)
- Backend: add PasswordResetTokenOrmEntity + migration
- Backend: add siret field to RegisterOrganizationDto and pass it to org creation
- Backend: remove MinLength(12) from LoginDto.password (wrong for login use case)
- Backend: add rememberMe to LoginDto (optional, informational)
- Frontend: register page rewritten as 2-step flow (account info → org info)
  with SIRET field, Suspense wrapper, "Centre d'aide" link removed
- Frontend: forgot-password page rewritten in French, matching login style
- Frontend: reset-password page rewritten in French, matching login style, with Suspense
- Frontend: remember me now works — localStorage when checked, sessionStorage otherwise
- Frontend: login page removes "Centre d'aide" link, connects rememberMe to login
- Frontend: auth-context and api/auth updated to pass rememberMe through full chain

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-02 13:10:08 +02:00

606 lines
24 KiB
TypeScript

'use client';
import { useState, useEffect, Suspense } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import Link from 'next/link';
import Image from 'next/image';
import { register } from '@/lib/api';
import { verifyInvitation, type InvitationResponse } from '@/lib/api/invitations';
import type { OrganizationType } from '@/types/api';
function RegisterPageContent() {
const router = useRouter();
const searchParams = useSearchParams();
// Step management
const [step, setStep] = useState<1 | 2>(1);
// Step 1 — Personal info
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
// Step 2 — Organization
const [organizationName, setOrganizationName] = useState('');
const [organizationType, setOrganizationType] = useState<OrganizationType>('FREIGHT_FORWARDER');
const [siren, setSiren] = useState('');
const [siret, setSiret] = useState('');
const [street, setStreet] = useState('');
const [city, setCity] = useState('');
const [state, setState] = useState('');
const [postalCode, setPostalCode] = useState('');
const [country, setCountry] = useState('FR');
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState('');
// Invitation state
const [invitationToken, setInvitationToken] = useState<string | null>(null);
const [invitation, setInvitation] = useState<InvitationResponse | null>(null);
const [isVerifyingInvitation, setIsVerifyingInvitation] = useState(false);
useEffect(() => {
const token = searchParams.get('token');
if (token) {
setIsVerifyingInvitation(true);
verifyInvitation(token)
.then(invitationData => {
setInvitation(invitationData);
setInvitationToken(token);
setEmail(invitationData.email);
setFirstName(invitationData.firstName);
setLastName(invitationData.lastName);
})
.catch(() => {
setError("Le lien d'invitation est invalide ou expiré.");
})
.finally(() => {
setIsVerifyingInvitation(false);
});
}
}, [searchParams]);
// ---- Step 1 validation ----
const validateStep1 = (): string | null => {
if (!firstName.trim() || firstName.trim().length < 2) return 'Le prénom doit contenir au moins 2 caractères';
if (!lastName.trim() || lastName.trim().length < 2) return 'Le nom doit contenir au moins 2 caractères';
if (!email.trim() || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) return "L'adresse email n'est pas valide";
if (password.length < 12) return 'Le mot de passe doit contenir au moins 12 caractères';
if (password !== confirmPassword) return 'Les mots de passe ne correspondent pas';
return null;
};
const handleStep1 = (e: React.FormEvent) => {
e.preventDefault();
setError('');
const err = validateStep1();
if (err) {
setError(err);
return;
}
// If invitation — submit directly (no org step)
if (invitationToken) {
handleFinalSubmit();
} else {
setStep(2);
}
};
// ---- Step 2 validation ----
const validateStep2 = (): string | null => {
if (!organizationName.trim()) return "Le nom de l'organisation est requis";
if (!/^[0-9]{9}$/.test(siren)) return 'Le numéro SIREN est requis (9 chiffres)';
if (siret && !/^[0-9]{14}$/.test(siret)) return 'Le numéro SIRET doit contenir 14 chiffres';
if (!street.trim() || !city.trim() || !postalCode.trim() || !country.trim()) {
return "Tous les champs d'adresse sont requis";
}
return null;
};
const handleStep2 = (e: React.FormEvent) => {
e.preventDefault();
setError('');
const err = validateStep2();
if (err) {
setError(err);
return;
}
handleFinalSubmit();
};
// ---- Final submit ----
const handleFinalSubmit = async () => {
setIsLoading(true);
setError('');
try {
await register({
email,
password,
firstName,
lastName,
...(invitationToken
? { invitationToken }
: {
organization: {
name: organizationName,
type: organizationType,
siren,
siret: siret || undefined,
street,
city,
state: state || undefined,
postalCode,
country: country.toUpperCase(),
},
}),
});
router.push('/dashboard');
} catch (err: any) {
setError(err.message || 'Erreur lors de la création du compte');
// On error at step 2, stay on step 2; at invitation (step 1), stay on step 1
} finally {
setIsLoading(false);
}
};
// ---- Right panel content ----
const rightPanel = (
<div className="hidden lg:block lg:w-1/2 relative bg-brand-navy">
<div className="absolute inset-0 bg-gradient-to-br from-brand-navy to-neutral-800 opacity-95"></div>
<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">
{invitation ? 'Rejoignez votre équipe' : 'Rejoignez des milliers d\'entreprises'}
</h2>
<p className="text-body-lg text-neutral-200 mb-12">
Simplifiez votre logistique maritime et gagnez du temps sur chaque expédition.
</p>
<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="M5 13l4 4L19 7" />
</svg>
</div>
<div className="ml-4">
<h3 className="text-h5 mb-1 text-white">Essai gratuit de 30 jours</h3>
<p className="text-body-sm text-neutral-300">Testez toutes les fonctionnalités sans engagement</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="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z" />
</svg>
</div>
<div className="ml-4">
<h3 className="text-h5 mb-1 text-white">Sécurité maximale</h3>
<p className="text-body-sm text-neutral-300">Vos données sont protégées et chiffrées</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="M18.364 5.636l-3.536 3.536m0 5.656l3.536 3.536M9.172 9.172L5.636 5.636m3.536 9.192l-3.536 3.536M21 12a9 9 0 11-18 0 9 9 0 0118 0zm-5 0a4 4 0 11-8 0 4 4 0 018 0z" />
</svg>
</div>
<div className="ml-4">
<h3 className="text-h5 mb-1 text-white">Support 24/7</h3>
<p className="text-body-sm text-neutral-300">Notre équipe est pour vous accompagner</p>
</div>
</div>
</div>
<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">2k+</div>
<div className="text-body-sm text-neutral-300 mt-1">Entreprises</div>
</div>
<div>
<div className="text-display-sm text-brand-turquoise">150+</div>
<div className="text-body-sm text-neutral-300 mt-1">Pays couverts</div>
</div>
<div>
<div className="text-display-sm text-brand-turquoise">24/7</div>
<div className="text-body-sm text-neutral-300 mt-1">Support</div>
</div>
</div>
</div>
</div>
<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>
);
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-8">
<Link href="/">
<Image
src="/assets/logos/logo-black.svg"
alt="Xpeditis"
width={50}
height={60}
priority
className="h-auto"
/>
</Link>
</div>
{/* Progress indicator (only for self-registration, 2 steps) */}
{!invitation && (
<div className="mb-8">
<div className="flex items-center gap-3">
<div className="flex items-center gap-2">
<div className={`w-8 h-8 rounded-full flex items-center justify-center text-sm font-semibold transition-colors ${
step >= 1 ? 'bg-brand-navy text-white' : 'bg-neutral-100 text-neutral-400'
}`}>
{step > 1 ? (
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2.5} d="M5 13l4 4L19 7" />
</svg>
) : '1'}
</div>
<span className={`text-body-sm font-medium ${step >= 1 ? 'text-brand-navy' : 'text-neutral-400'}`}>
Votre compte
</span>
</div>
<div className={`flex-1 h-0.5 transition-colors ${step >= 2 ? 'bg-brand-navy' : 'bg-neutral-200'}`} />
<div className="flex items-center gap-2">
<div className={`w-8 h-8 rounded-full flex items-center justify-center text-sm font-semibold transition-colors ${
step >= 2 ? 'bg-brand-navy text-white' : 'bg-neutral-100 text-neutral-400'
}`}>
2
</div>
<span className={`text-body-sm font-medium ${step >= 2 ? 'text-brand-navy' : 'text-neutral-400'}`}>
Votre organisation
</span>
</div>
</div>
</div>
)}
{/* Header */}
<div className="mb-6">
{isVerifyingInvitation ? (
<p className="text-body text-neutral-600">Vérification de l'invitation...</p>
) : invitation ? (
<>
<h1 className="text-h1 text-brand-navy mb-2">Accepter l'invitation</h1>
<div className="p-3 bg-green-50 border border-green-200 rounded-lg">
<p className="text-body-sm text-green-800">
Invitation valide créez votre mot de passe pour rejoindre l'organisation.
</p>
</div>
</>
) : step === 1 ? (
<>
<h1 className="text-h1 text-brand-navy mb-2">Créer un compte</h1>
<p className="text-body text-neutral-600">Commencez votre essai gratuit dès aujourd'hui</p>
</>
) : (
<>
<h1 className="text-h1 text-brand-navy mb-2">Votre organisation</h1>
<p className="text-body text-neutral-600">Renseignez les informations de votre entreprise</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>
)}
{/* ---- STEP 1: Personal info ---- */}
{(step === 1 || invitation) && !isVerifyingInvitation && (
<form onSubmit={handleStep1} className="space-y-5">
<div className="grid grid-cols-2 gap-4">
<div>
<label htmlFor="firstName" className="label">Prénom</label>
<input
id="firstName"
type="text"
required
value={firstName}
onChange={e => setFirstName(e.target.value)}
className="input w-full"
placeholder="Jean"
disabled={isLoading || !!invitation}
/>
</div>
<div>
<label htmlFor="lastName" className="label">Nom</label>
<input
id="lastName"
type="text"
required
value={lastName}
onChange={e => setLastName(e.target.value)}
className="input w-full"
placeholder="Dupont"
disabled={isLoading || !!invitation}
/>
</div>
</div>
<div>
<label htmlFor="email" className="label">Adresse email</label>
<input
id="email"
type="email"
required
value={email}
onChange={e => setEmail(e.target.value)}
className="input w-full"
placeholder="jean.dupont@entreprise.com"
autoComplete="email"
disabled={isLoading || !!invitation}
/>
</div>
<div>
<label htmlFor="password" className="label">Mot de passe</label>
<input
id="password"
type="password"
required
value={password}
onChange={e => setPassword(e.target.value)}
className="input w-full"
placeholder="••••••••••••"
autoComplete="new-password"
disabled={isLoading}
/>
<p className="mt-1.5 text-body-xs text-neutral-500">Au moins 12 caractères</p>
</div>
<div>
<label htmlFor="confirmPassword" className="label">Confirmer le mot de passe</label>
<input
id="confirmPassword"
type="password"
required
value={confirmPassword}
onChange={e => setConfirmPassword(e.target.value)}
className="input w-full"
placeholder="••••••••••••"
autoComplete="new-password"
disabled={isLoading}
/>
</div>
<button
type="submit"
disabled={isLoading}
className="btn-primary w-full text-lg disabled:opacity-50 disabled:cursor-not-allowed mt-2"
>
{isLoading
? 'Création du compte...'
: invitation
? 'Créer mon compte'
: 'Continuer'}
</button>
<p className="text-body-xs text-center text-neutral-500">
En créant un compte, vous acceptez nos{' '}
<Link href="/terms" className="link">Conditions d'utilisation</Link>{' '}
et notre{' '}
<Link href="/privacy" className="link">Politique de confidentialité</Link>
</p>
</form>
)}
{/* ---- STEP 2: Organization info ---- */}
{step === 2 && !invitation && (
<form onSubmit={handleStep2} className="space-y-5">
<div>
<label htmlFor="organizationName" className="label">Nom de l'organisation *</label>
<input
id="organizationName"
type="text"
required
value={organizationName}
onChange={e => setOrganizationName(e.target.value)}
className="input w-full"
placeholder="Acme Logistics"
disabled={isLoading}
/>
</div>
<div>
<label htmlFor="organizationType" className="label">Type d'organisation *</label>
<select
id="organizationType"
value={organizationType}
onChange={e => setOrganizationType(e.target.value as OrganizationType)}
className="input w-full"
disabled={isLoading}
>
<option value="FREIGHT_FORWARDER">Transitaire</option>
<option value="SHIPPER">Expéditeur</option>
<option value="CARRIER">Transporteur</option>
</select>
</div>
<div className="grid grid-cols-2 gap-4">
<div>
<label htmlFor="siren" className="label">SIREN *</label>
<input
id="siren"
type="text"
required
value={siren}
onChange={e => setSiren(e.target.value.replace(/\D/g, '').slice(0, 9))}
className="input w-full"
placeholder="123456789"
maxLength={9}
disabled={isLoading}
/>
<p className="mt-1 text-body-xs text-neutral-500">9 chiffres</p>
</div>
<div>
<label htmlFor="siret" className="label">SIRET <span className="text-neutral-400 font-normal">(optionnel)</span></label>
<input
id="siret"
type="text"
value={siret}
onChange={e => setSiret(e.target.value.replace(/\D/g, '').slice(0, 14))}
className="input w-full"
placeholder="12345678900014"
maxLength={14}
disabled={isLoading}
/>
<p className="mt-1 text-body-xs text-neutral-500">14 chiffres</p>
</div>
</div>
<div>
<label htmlFor="street" className="label">Adresse *</label>
<input
id="street"
type="text"
required
value={street}
onChange={e => setStreet(e.target.value)}
className="input w-full"
placeholder="123 Rue de la République"
disabled={isLoading}
/>
</div>
<div className="grid grid-cols-2 gap-4">
<div>
<label htmlFor="city" className="label">Ville *</label>
<input
id="city"
type="text"
required
value={city}
onChange={e => setCity(e.target.value)}
className="input w-full"
placeholder="Paris"
disabled={isLoading}
/>
</div>
<div>
<label htmlFor="postalCode" className="label">Code postal *</label>
<input
id="postalCode"
type="text"
required
value={postalCode}
onChange={e => setPostalCode(e.target.value)}
className="input w-full"
placeholder="75001"
disabled={isLoading}
/>
</div>
</div>
<div className="grid grid-cols-2 gap-4">
<div>
<label htmlFor="state" className="label">
Région <span className="text-neutral-400 font-normal">(optionnel)</span>
</label>
<input
id="state"
type="text"
value={state}
onChange={e => setState(e.target.value)}
className="input w-full"
placeholder="Île-de-France"
disabled={isLoading}
/>
</div>
<div>
<label htmlFor="country" className="label">Pays *</label>
<input
id="country"
type="text"
required
value={country}
onChange={e => setCountry(e.target.value.toUpperCase().slice(0, 2))}
className="input w-full"
placeholder="FR"
maxLength={2}
disabled={isLoading}
/>
<p className="mt-1 text-body-xs text-neutral-500">Code ISO 2 lettres</p>
</div>
</div>
<div className="flex gap-3 pt-2">
<button
type="button"
onClick={() => { setStep(1); setError(''); }}
disabled={isLoading}
className="btn-secondary flex-1 text-lg disabled:opacity-50"
>
Retour
</button>
<button
type="submit"
disabled={isLoading}
className="btn-primary flex-2 flex-1 text-lg disabled:opacity-50 disabled:cursor-not-allowed"
>
{isLoading ? 'Création...' : 'Créer mon compte'}
</button>
</div>
<p className="text-body-xs text-center text-neutral-500">
En créant un compte, vous acceptez nos{' '}
<Link href="/terms" className="link">Conditions d'utilisation</Link>{' '}
et notre{' '}
<Link href="/privacy" className="link">Politique de confidentialité</Link>
</p>
</form>
)}
{/* Sign In Link */}
<div className="mt-8 text-center">
<p className="text-body text-neutral-600">
Vous avez déjà un compte ?{' '}
<Link href="/login" className="link font-semibold">Se connecter</Link>
</p>
</div>
{/* Footer Links */}
<div className="mt-6 pt-6 border-t border-neutral-200">
<div className="flex flex-wrap justify-center gap-6 text-body-sm text-neutral-500">
<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>
{rightPanel}
</div>
);
}
export default function RegisterPage() {
return (
<Suspense>
<RegisterPageContent />
</Suspense>
);
}