Skip to content

Commit

Permalink
including necessary fields only from JWT token
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyJung23 committed Sep 19, 2024
1 parent 3db3e55 commit 939a9b8
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions api/src/services/jwt.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {injectable, /* inject, */ BindingScope, inject} from '@loopback/core';
import {injectable, BindingScope, inject} from '@loopback/core';
import {TokenService} from '@loopback/authentication';
import {UserProfile, securityId} from '@loopback/security';
import {promisify} from 'util';
Expand All @@ -18,7 +18,7 @@ export class JwtService implements TokenService{
throw new Error('securityUserProfile cannot be null');
}

let tokenBody = {
const tokenBody = {
"id": userProfile.id,
"role": userProfile.fsaeRole,
"activated": userProfile.activated
Expand All @@ -35,31 +35,33 @@ export class JwtService implements TokenService{
return Promise.resolve(token);
}

// Verify token and map it to securityUserProfile
verifyToken(token: string): Promise<any> {
if (!token) {
throw new HttpErrors.Unauthorized('Error verifying Token. Token cannot be null');
}

let securityUserProfile: UserProfile;

let temp
let decodedToken: any;
try {
// decode user profile from token
const decodedToken = verify(token, this.jwtSecret);
// don't copy over token field 'iat' and 'exp', nor 'aud', 'iss', 'sub'

// TODO: Correctly map out decoded Token to user profile
securityUserProfile = {
[securityId]: '',
name: 'Test',
};
temp = decodedToken

decodedToken = verify(token, this.jwtSecret);
} catch (error) {
throw new HttpErrors.Unauthorized(`Error verifying token: ${error.message}`);
}

return Promise.resolve(temp);
}
const {id, role, activated} = decodedToken;

if (!id || !role || !activated) {
throw new HttpErrors.Unauthorized('Token payload missing required fields');
}

const securityUserProfile: UserProfile = {
[securityId]: id,
id: id,
//name: 'Test',
fsaeRole: role,
activated: activated
};

return Promise.resolve(securityUserProfile);
}
}

0 comments on commit 939a9b8

Please sign in to comment.