From 86a456d1825287d4a90b3c1610059f6e9ce12c43 Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Tue, 3 Nov 2020 19:07:01 +0530 Subject: [PATCH] :recycle: Add ID to auth token --- src/modules/auth/auth.interface.ts | 1 + src/modules/auth/auth.service.ts | 1 + src/modules/auth/jwt.strategy.ts | 6 +++--- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/modules/auth/auth.interface.ts b/src/modules/auth/auth.interface.ts index ea465f27c..41c259d63 100644 --- a/src/modules/auth/auth.interface.ts +++ b/src/modules/auth/auth.interface.ts @@ -4,6 +4,7 @@ import { Request as ExpressRequest } from 'express'; export interface AccessTokenClaims { sub: string; + id: number; scopes: string[]; } diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 9cea79b35..9fe40f6d7 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -482,6 +482,7 @@ export class AuthService { const scopes = await this.getScopes(userId); const payload: AccessTokenClaims = { sub: LOGIN_ACCESS_TOKEN, + id: userId, scopes, }; return this.jwtService.sign(payload, { diff --git a/src/modules/auth/jwt.strategy.ts b/src/modules/auth/jwt.strategy.ts index 6fe604fb5..1f8685e7f 100644 --- a/src/modules/auth/jwt.strategy.ts +++ b/src/modules/auth/jwt.strategy.ts @@ -2,6 +2,7 @@ import { ExtractJwt, Strategy } from 'passport-jwt'; import { PassportStrategy } from '@nestjs/passport'; import { Injectable, UnauthorizedException } from '@nestjs/common'; import { AccessTokenClaims, AccessTokenParsed } from './auth.interface'; +import { LOGIN_ACCESS_TOKEN } from '../tokens/tokens.constants'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { @@ -14,9 +15,8 @@ export class JwtStrategy extends PassportStrategy(Strategy) { } async validate(payload: AccessTokenClaims): Promise { - const { sub, scopes } = payload; - const id = Number(sub.replace('user', '')); - if (isNaN(id)) throw new UnauthorizedException(); + const { sub, id, scopes } = payload; + if (sub !== LOGIN_ACCESS_TOKEN) throw new UnauthorizedException(); return { id, scopes }; } }