Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: jwt expiry #1907

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions interfaces/IBF-dashboard/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions interfaces/IBF-dashboard/src/app/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export class AuthService implements OnDestroy {
return null;
}

const isExpired: boolean = this.jwtService.checkExpiry(rawToken);
if (isExpired) {
return null;
}

const decodedToken = this.jwtService.decodeToken(rawToken);
const user: User = {
token: rawToken,
Expand Down
4 changes: 4 additions & 0 deletions interfaces/IBF-dashboard/src/app/services/jwt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ export class JwtService {
public decodeToken(rawToken: string): any {
return this.jwtHelper.decodeToken(rawToken);
}

public checkExpiry(rawToken: string): boolean {
return this.jwtHelper.isTokenExpired(rawToken);
}
}
9 changes: 8 additions & 1 deletion services/API-service/src/roles.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ export class RolesGuard implements CanActivate {
const authHeaders = req.headers.authorization;
if (authHeaders && (authHeaders as string).split(' ')[1]) {
const token = (authHeaders as string).split(' ')[1];
const decoded: User = jwt.verify(token, process.env.SECRET);

let decoded: User;
try {
decoded = jwt.verify(token, process.env.SECRET);
} catch {
return false;
}

const user = await this.userService.findById(decoded.userId);

// First check if logged in
Expand Down
Loading