Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Add ID to auth token
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 3, 2020
1 parent 282fcf8 commit 86a456d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/modules/auth/auth.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Request as ExpressRequest } from 'express';

export interface AccessTokenClaims {
sub: string;
id: number;
scopes: string[];
}

Expand Down
1 change: 1 addition & 0 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
6 changes: 3 additions & 3 deletions src/modules/auth/jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -14,9 +15,8 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
}

async validate(payload: AccessTokenClaims): Promise<AccessTokenParsed> {
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 };
}
}

0 comments on commit 86a456d

Please sign in to comment.