import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { RequestUser } from './request-user';

export const CurrentUser = createParamDecorator((_data: unknown, ctx: ExecutionContext): RequestUser => {
  const request = ctx.switchToHttp().getRequest<{ user?: RequestUser }>();
  if (!request.user) {
    throw new Error('CurrentUser was requested on an unauthenticated route');
  }
  return request.user;
});
