fix: auth user decorator cannot destruct property of undefined (#3394)

* fix: auth user decorator cannot destruct property of undefined

* fix: change naming
This commit is contained in:
Jérémy M 2024-01-12 12:12:33 +01:00 committed by GitHub
parent 09a2a656e2
commit 3e8f4ec2c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View File

@ -21,7 +21,7 @@ export class AnalyticsResolver {
createEvent(
@Args() createEventInput: CreateAnalyticsInput,
@AuthWorkspace() workspace: Workspace | undefined,
@AuthUser() user: User | undefined,
@AuthUser({ allowUndefined: true }) user: User | undefined,
) {
return this.analyticsService.create(createEventInput, user, workspace);
}

View File

@ -1,11 +1,23 @@
import { ExecutionContext, createParamDecorator } from '@nestjs/common';
import {
ExecutionContext,
ForbiddenException,
createParamDecorator,
} from '@nestjs/common';
import { getRequest } from 'src/utils/extract-request';
interface DecoratorOptions {
allowUndefined?: boolean;
}
export const AuthUser = createParamDecorator(
(_: unknown, ctx: ExecutionContext) => {
(options: DecoratorOptions | undefined, ctx: ExecutionContext) => {
const request = getRequest(ctx);
if (!options?.allowUndefined && (!request.user || !request.user.user)) {
throw new ForbiddenException("You're not authorized to do this");
}
return request.user ? request.user.user : undefined;
},
);