feat(auth): add simulated current-user middleware
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
import { Request, Response, NextFunction } from 'express';
|
||||||
|
import { AppError } from '../errors/app-error';
|
||||||
|
import { findByEmail } from '../repositories/utilisateur.repository';
|
||||||
|
import { AuthenticatedUser } from '../types/authenticated-user';
|
||||||
|
|
||||||
|
/* Auth simulée (temporaire) : en attendant Azure AD, l'utilisateur courant est
|
||||||
|
résolu depuis l'en-tête `x-user-email`. À remplacer par la validation du JWT. */
|
||||||
|
export async function currentUser(req: Request, _res: Response, next: NextFunction): Promise<void> {
|
||||||
|
const email = req.header('x-user-email');
|
||||||
|
if (!email) {
|
||||||
|
throw new AppError(401, 'En-tete x-user-email requis (auth simulee)');
|
||||||
|
}
|
||||||
|
|
||||||
|
const utilisateur = await findByEmail(email);
|
||||||
|
if (!utilisateur || !utilisateur.actif) {
|
||||||
|
throw new AppError(401, 'Utilisateur non reconnu');
|
||||||
|
}
|
||||||
|
|
||||||
|
const authenticated: AuthenticatedUser = {
|
||||||
|
id: utilisateur.id,
|
||||||
|
email: utilisateur.email,
|
||||||
|
roleCode: utilisateur.role.code,
|
||||||
|
campusId: utilisateur.campusId,
|
||||||
|
};
|
||||||
|
|
||||||
|
req.user = authenticated;
|
||||||
|
next();
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import { Prisma } from '@prisma/client';
|
||||||
|
import { prisma } from '../db/prisma';
|
||||||
|
|
||||||
|
export type UtilisateurAvecRole = Prisma.UtilisateurGetPayload<{ include: { role: true } }>;
|
||||||
|
|
||||||
|
export function findByEmail(email: string): Promise<UtilisateurAvecRole | null> {
|
||||||
|
return prisma.utilisateur.findUnique({
|
||||||
|
where: { email },
|
||||||
|
include: { role: true },
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
export interface AuthenticatedUser {
|
||||||
|
id: number;
|
||||||
|
email: string;
|
||||||
|
roleCode: string;
|
||||||
|
campusId: number;
|
||||||
|
}
|
||||||
Vendored
+9
@@ -0,0 +1,9 @@
|
|||||||
|
import { AuthenticatedUser } from './authenticated-user';
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
namespace Express {
|
||||||
|
interface Request {
|
||||||
|
user?: AuthenticatedUser;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user