chore: created and added new errors in jwt.strategy.ts file

This commit is contained in:
Balu Babu
2023-01-11 18:41:06 +05:30
parent d98e7b9416
commit d3a43cb65f
2 changed files with 20 additions and 3 deletions

View File

@@ -10,6 +10,11 @@ import { UserService } from 'src/user/user.service';
import { AuthService } from '../auth.service'; import { AuthService } from '../auth.service';
import { Request } from 'express'; import { Request } from 'express';
import * as O from 'fp-ts/Option'; import * as O from 'fp-ts/Option';
import {
COOKIES_NOT_FOUND,
INVALID_ACCESS_TOKEN,
USER_NOT_FOUND,
} from 'src/errors';
@Injectable() @Injectable()
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') { export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
@@ -22,7 +27,7 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
(request: Request) => { (request: Request) => {
const ATCookie = request.cookies['access_token']; const ATCookie = request.cookies['access_token'];
if (!ATCookie) { if (!ATCookie) {
throw new ForbiddenException('No cookies present'); throw new ForbiddenException(COOKIES_NOT_FOUND);
} }
return ATCookie; return ATCookie;
}, },
@@ -32,12 +37,12 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
} }
async validate(payload: AccessTokenPayload) { async validate(payload: AccessTokenPayload) {
if (!payload) throw new ForbiddenException('Access token malformed'); if (!payload) throw new ForbiddenException(INVALID_ACCESS_TOKEN);
const user = await this.usersService.findUserById(payload.sub); const user = await this.usersService.findUserById(payload.sub);
if (O.isNone(user)) { if (O.isNone(user)) {
throw new UnauthorizedException('User not found'); throw new UnauthorizedException(USER_NOT_FOUND);
} }
const profile = { const profile = {

View File

@@ -234,3 +234,15 @@ export const TOKEN_EXPIRED = 'auth/token_expired' as const;
* (AuthService) * (AuthService)
*/ */
export const MAGIC_LINK_EXPIRED = 'auth/magic_link_expired' as const; export const MAGIC_LINK_EXPIRED = 'auth/magic_link_expired' as const;
/**
* No cookies were found in the auth request
* (AuthService)
*/
export const COOKIES_NOT_FOUND = 'auth/cookies_not_found' as const;
/**
* Access Token is malformed or invalid
* (AuthService)
*/
export const INVALID_ACCESS_TOKEN = 'auth/invalid_access_token' as const;