forked from mtusi2026/mtusi2026lab4
21 lines
566 B
TypeScript
21 lines
566 B
TypeScript
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
|
|
import { FastifyRequest } from 'fastify';
|
|
import { User } from '../common/domain';
|
|
|
|
declare module 'fastify' {
|
|
interface FastifyRequest {
|
|
currentUser?: User;
|
|
sessionTokenHash?: string;
|
|
}
|
|
}
|
|
|
|
export const CurrentUser = createParamDecorator(
|
|
(_data: unknown, ctx: ExecutionContext): User => {
|
|
const request = ctx.switchToHttp().getRequest<FastifyRequest>();
|
|
if (!request.currentUser) {
|
|
throw new Error('CurrentUser used without AuthGuard');
|
|
}
|
|
return request.currentUser;
|
|
},
|
|
);
|