feat: refactor folder structure (#4498)
* feat: wip refactor folder structure * Fix * fix position --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
40
packages/twenty-server/src/engine/guards/jwt.auth.guard.ts
Normal file
40
packages/twenty-server/src/engine/guards/jwt.auth.guard.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import {
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
import { JsonWebTokenError } from 'jsonwebtoken';
|
||||
|
||||
import { assert } from 'src/utils/assert';
|
||||
import { getRequest } from 'src/utils/extract-request';
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard(['jwt']) {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
getRequest(context: ExecutionContext) {
|
||||
return getRequest(context);
|
||||
}
|
||||
|
||||
handleRequest(err: any, user: any, info: any) {
|
||||
assert(user, '', UnauthorizedException);
|
||||
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (info && info instanceof Error) {
|
||||
if (info instanceof JsonWebTokenError) {
|
||||
info = String(info);
|
||||
}
|
||||
|
||||
throw new UnauthorizedException(info);
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
import { ExecutionContext, Injectable } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
import { getRequest } from 'src/utils/extract-request';
|
||||
|
||||
@Injectable()
|
||||
export class OptionalJwtAuthGuard extends AuthGuard(['jwt']) {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
getRequest(context: ExecutionContext) {
|
||||
const request = getRequest(context);
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
handleRequest(err, user, info) {
|
||||
if (err || info) return null;
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user