SPB Git

spb/groupe-ka Public

Groupe KA — site du holding + KA ID (compte unique & SSO des 7 plateformes). Next.js 16, SQLite, Google & Apple login.

TypeScript 85.5% HTML 8.9% CSS 5.5%
2.1 KB · 77 lines typescript
Raw Blame History
1// Auteur : Simon-Pierre Boucher — contact@spboucher.ai2import { NextRequest, NextResponse } from "next/server";3import { db, findUserByEmail, ensureKaId, touchLastLogin } from "@/lib/db";4import { ROLES } from "@/lib/roles";5import {6  hashPassword,7  mintSessionToken,8  SESSION_COOKIE,9  sessionCookieOptions,10} from "@/lib/auth";1112const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;1314export async function POST(req: NextRequest) {15  let body: {16    email?: string;17    password?: string;18    name?: string;19    role?: string;20  };21  try {22    body = await req.json();23  } catch {24    return NextResponse.json({ error: "Requête invalide." }, { status: 400 });25  }26  const email = (body.email ?? "").trim().toLowerCase();27  const password = body.password ?? "";28  const name = (body.name ?? "").trim();2930  if (!EMAIL_RE.test(email))31    return NextResponse.json(32      { error: "Adresse courriel invalide." },33      { status: 400 },34    );35  if (password.length < 8)36    return NextResponse.json(37      { error: "Le mot de passe doit compter au moins 8 caractères." },38      { status: 400 },39    );40  if (name.length < 2)41    return NextResponse.json(42      { error: "Dites-nous au moins votre prénom." },43      { status: 400 },44    );45  const role = body.role ?? "";46  if (!ROLES[role])47    return NextResponse.json(48      { error: "Choisissez votre type de compte (utilisateur, fournisseur ou équipe)." },49      { status: 400 },50    );5152  if (findUserByEmail(email))53    return NextResponse.json(54      {55        error:56          "Cette adresse a déjà un KA ID — connectez-vous (ou utilisez « Continuer avec Google » si le compte vient de Google).",57      },58      { status: 409 },59    );6061  const info = db62    .prepare(63      "INSERT INTO users (email, name, password_hash, role) VALUES (?, ?, ?, ?)",64    )65    .run(email, name, hashPassword(password), role);66  ensureKaId(Number(info.lastInsertRowid));67  touchLastLogin(Number(info.lastInsertRowid));6869  const res = NextResponse.json({ ok: true });70  res.cookies.set(71    SESSION_COOKIE,72    await mintSessionToken(Number(info.lastInsertRowid)),73    sessionCookieOptions,74  );75  return res;76}77