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:
@ -21,7 +21,7 @@ export class AnalyticsResolver {
|
|||||||
createEvent(
|
createEvent(
|
||||||
@Args() createEventInput: CreateAnalyticsInput,
|
@Args() createEventInput: CreateAnalyticsInput,
|
||||||
@AuthWorkspace() workspace: Workspace | undefined,
|
@AuthWorkspace() workspace: Workspace | undefined,
|
||||||
@AuthUser() user: User | undefined,
|
@AuthUser({ allowUndefined: true }) user: User | undefined,
|
||||||
) {
|
) {
|
||||||
return this.analyticsService.create(createEventInput, user, workspace);
|
return this.analyticsService.create(createEventInput, user, workspace);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,23 @@
|
|||||||
import { ExecutionContext, createParamDecorator } from '@nestjs/common';
|
import {
|
||||||
|
ExecutionContext,
|
||||||
|
ForbiddenException,
|
||||||
|
createParamDecorator,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
|
||||||
import { getRequest } from 'src/utils/extract-request';
|
import { getRequest } from 'src/utils/extract-request';
|
||||||
|
|
||||||
|
interface DecoratorOptions {
|
||||||
|
allowUndefined?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export const AuthUser = createParamDecorator(
|
export const AuthUser = createParamDecorator(
|
||||||
(_: unknown, ctx: ExecutionContext) => {
|
(options: DecoratorOptions | undefined, ctx: ExecutionContext) => {
|
||||||
const request = getRequest(ctx);
|
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;
|
return request.user ? request.user.user : undefined;
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user