143 lines
7.8 KiB
TypeScript
143 lines
7.8 KiB
TypeScript
import { Body, Controller, Get, Header, HttpCode, Post, Query } from '@nestjs/common';
|
||
import { IsEmail, MaxLength } from 'class-validator';
|
||
import { Throttle } from '@nestjs/throttler';
|
||
import { escapeHtml as e } from '../enrollment/pages';
|
||
import { PasswordSetupService, SetupOutcome } from './password-setup.service';
|
||
|
||
const MIN_LENGTH = 10;
|
||
|
||
class ForgotDto {
|
||
@IsEmail({}, { message: 'Adresse e-mail invalide' }) @MaxLength(200)
|
||
email!: string;
|
||
}
|
||
|
||
/**
|
||
* Page publique du lien reçu par e-mail : https://monespacepro.eu/api/public/mot-de-passe?token=…
|
||
* (le chemin /api/public est déjà transmis à l'API par nginx sur monespacepro.eu).
|
||
*/
|
||
@Controller('public/mot-de-passe')
|
||
export class PasswordSetupController {
|
||
constructor(private setup: PasswordSetupService) {}
|
||
|
||
@Get()
|
||
@Header('Content-Type', 'text/html; charset=utf-8')
|
||
@Header('Cache-Control', 'no-store')
|
||
@Header('Referrer-Policy', 'no-referrer')
|
||
async form(@Query('token') token = '') {
|
||
const outcome = await this.setup.check(token);
|
||
return outcome.ok ? formPage(token, outcome.staff) : problemPage(outcome.reason);
|
||
}
|
||
|
||
/** « Mot de passe oublié » (app, écran de connexion) : envoie le lien si le compte existe. */
|
||
@Post('oubli')
|
||
@HttpCode(200)
|
||
@Throttle({ default: { limit: 5, ttl: 15 * 60_000 } })
|
||
forgot(@Body() dto: ForgotDto) {
|
||
return this.setup.requestReset(dto.email);
|
||
}
|
||
|
||
@Post()
|
||
@HttpCode(200)
|
||
@Throttle({ default: { limit: 10, ttl: 10 * 60_000 } })
|
||
@Header('Content-Type', 'text/html; charset=utf-8')
|
||
@Header('Cache-Control', 'no-store')
|
||
@Header('Referrer-Policy', 'no-referrer')
|
||
async submit(@Body() body: Record<string, unknown>) {
|
||
const token = typeof body.token === 'string' ? body.token : '';
|
||
const password = typeof body.password === 'string' ? body.password : '';
|
||
const confirm = typeof body.confirm === 'string' ? body.confirm : '';
|
||
const check = await this.setup.check(token);
|
||
if (!check.ok) return problemPage(check.reason);
|
||
if (password.length < MIN_LENGTH || password.length > 128) return formPage(token, check.staff, `Le mot de passe doit contenir au moins ${MIN_LENGTH} caractères.`);
|
||
if (password !== confirm) return formPage(token, check.staff, 'Les deux mots de passe ne sont pas identiques.');
|
||
const outcome = await this.setup.setPassword(token, password);
|
||
return outcome.ok ? donePage(outcome.staff) : problemPage(outcome.reason);
|
||
}
|
||
}
|
||
|
||
// ── Pages ──────────────────────────────────────────────────────────────────────
|
||
function shell(title: string, body: string) {
|
||
return `<!doctype html>
|
||
<html lang="fr">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<meta name="robots" content="noindex">
|
||
<meta name="theme-color" content="#0E7A66">
|
||
<title>${e(title)} — MonEspacePro</title>
|
||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||
<link href="https://fonts.googleapis.com/css2?family=Figtree:wght@400;600;700;800&display=swap" rel="stylesheet">
|
||
<style>
|
||
*{box-sizing:border-box}
|
||
body{margin:0;min-height:100vh;background:#F6F6F3;color:#1C1C19;font:16px/1.5 Figtree,system-ui,sans-serif;display:flex;flex-direction:column;align-items:center;padding:32px 20px}
|
||
.brand{display:flex;align-items:center;gap:10px;font-weight:800;font-size:20px;margin-bottom:28px}
|
||
.brand svg{width:34px;height:34px}
|
||
.card{width:100%;max-width:440px;background:#fff;border:1px solid #DEDDD6;border-radius:22px;padding:28px}
|
||
h1{font-size:26px;line-height:1.15;margin:0 0 10px;letter-spacing:-.02em}
|
||
p{margin:0 0 14px;color:#5F5E56}
|
||
.who{background:#EAF6F2;border-radius:14px;padding:12px 14px;margin:6px 0 20px;font-size:15px;color:#1C1C19}
|
||
.who b{display:block}
|
||
label{display:block;font-weight:700;font-size:15px;margin:14px 0 6px}
|
||
input[type=password],input[type=text]{width:100%;height:52px;border:1px solid #DEDDD6;border-radius:14px;padding:0 14px;font:inherit;font-size:17px;background:#F6F6F3}
|
||
input:focus{outline:3px solid #F2B544;outline-offset:1px}
|
||
.hint{font-size:13px;color:#5F5E56;margin-top:6px}
|
||
.err{background:#FBEAE8;color:#B8322A;border-radius:12px;padding:10px 14px;font-weight:600;font-size:15px;margin-bottom:6px}
|
||
button{width:100%;height:54px;margin-top:22px;border:0;border-radius:14px;background:#0E7A66;color:#fff;font:inherit;font-weight:700;font-size:17px;cursor:pointer}
|
||
.show{display:flex;align-items:center;gap:8px;margin-top:10px;font-size:14px;color:#5F5E56}
|
||
.icon{width:64px;height:64px;border-radius:50%;display:grid;place-items:center;margin-bottom:16px}
|
||
.icon svg{width:34px;height:34px}
|
||
.ok{background:#EAF6F2;color:#0E7A66}.ko{background:#FBEAE8;color:#B8322A}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="brand"><svg viewBox="0.5 -2 64 64" aria-hidden="true"><path d="M12 54V32a9 9 0 0 1 18 0v22M30 32a9 9 0 0 1 18 0v22" fill="none" stroke="#0E7A66" stroke-width="8"/><circle cx="51" cy="12" r="6" fill="#F2B544"/></svg>MonEspacePro</div>
|
||
<main class="card">${body}</main>
|
||
</body>
|
||
</html>`;
|
||
}
|
||
|
||
function formPage(token: string, staff: { email: string; displayName: string; storeName: string }, error?: string) {
|
||
return shell(
|
||
'Choisir mon mot de passe',
|
||
`<h1>Choisissez votre mot de passe</h1>
|
||
<p>Bonjour ${e(staff.displayName)}, ce mot de passe vous servira à vous connecter à l’app MonEspacePro.</p>
|
||
<div class="who"><b>${e(staff.email)}</b>${e(staff.storeName)}</div>
|
||
${error ? `<div class="err" role="alert">${e(error)}</div>` : ''}
|
||
<form method="post" action="/api/public/mot-de-passe" autocomplete="on">
|
||
<input type="hidden" name="token" value="${e(token)}">
|
||
<input type="text" name="username" value="${e(staff.email)}" autocomplete="username" hidden>
|
||
<label for="p1">Nouveau mot de passe</label>
|
||
<input id="p1" type="password" name="password" required minlength="${MIN_LENGTH}" maxlength="128" autocomplete="new-password">
|
||
<div class="hint">${MIN_LENGTH} caractères minimum. Mélangez lettres, chiffres et symboles.</div>
|
||
<label for="p2">Confirmez le mot de passe</label>
|
||
<input id="p2" type="password" name="confirm" required minlength="${MIN_LENGTH}" maxlength="128" autocomplete="new-password">
|
||
<label class="show"><input type="checkbox" onclick="for(const i of document.querySelectorAll('input[name=password],input[name=confirm]'))i.type=this.checked?'text':'password'"> Afficher les mots de passe</label>
|
||
<button type="submit">Enregistrer mon mot de passe</button>
|
||
</form>`,
|
||
);
|
||
}
|
||
|
||
function donePage(staff: { email: string }) {
|
||
return shell(
|
||
'Mot de passe enregistré',
|
||
`<div class="icon ok"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12.5l4.5 4.5L19 7.5"/></svg></div>
|
||
<h1>Mot de passe enregistré</h1>
|
||
<p>Vous pouvez maintenant ouvrir l’app <b>MonEspacePro</b> et vous connecter avec :</p>
|
||
<div class="who"><b>${e(staff.email)}</b>et le mot de passe que vous venez de choisir.</div>
|
||
<p>Pas encore l’app ? Installez MonEspacePro depuis l’App Store ou Google Play.</p>`,
|
||
);
|
||
}
|
||
|
||
function problemPage(reason: Exclude<SetupOutcome, { ok: true }>['reason']) {
|
||
const text = {
|
||
expired: ['Lien expiré', 'Ce lien n’est plus valable (7 jours maximum). Demandez à votre dirigeant de vous en renvoyer un depuis l’app MonEspacePro.'],
|
||
used: ['Lien déjà utilisé', 'Votre mot de passe a déjà été enregistré avec ce lien. Connectez-vous à l’app MonEspacePro ; en cas d’oubli, demandez un nouveau lien à votre dirigeant.'],
|
||
invalid: ['Lien invalide', 'Ce lien n’est pas valide ou le compte n’existe plus. Demandez un nouveau lien à votre dirigeant.'],
|
||
}[reason];
|
||
return shell(
|
||
text[0],
|
||
`<div class="icon ko"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3.5l9.5 16.5h-19zM12 10v4.5M12 17.5h.01"/></svg></div>
|
||
<h1>${e(text[0])}</h1>
|
||
<p>${e(text[1])}</p>`,
|
||
);
|
||
}
|