mirror of
https://github.com/twentyhq/twenty.git
synced 2024-11-23 22:12:24 +03:00
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:
parent
09a2a656e2
commit
3e8f4ec2c5
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
},
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user