Skip to content

Commit

Permalink
WIP: Added functions to validate the token
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoud-Emad committed Jan 28, 2024
1 parent 3b85ba6 commit c02c7c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions client/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ export interface Country {
id: number
country: string
}

export interface JWTokenObject {
token_type: string
exp: number
iat: number
jti: string
user_id: number
}
14 changes: 14 additions & 0 deletions client/src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import moment from 'moment'

import type { JWTokenObject } from "@/types"

export async function resolve<T>(promise: Promise<T>): Promise<[T, any]> {
try {
return [await promise, null]
Expand Down Expand Up @@ -45,3 +47,15 @@ export function getStatusColor(status: string) {
return 'grey'
}
}
export function decodeAccessToken(token: string): JWTokenObject {
const base64Url = token.split('.')[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
}

export function isValidToken(token: string): boolean{
return Date.now() >= decodeAccessToken(token).exp * 1000
}

0 comments on commit c02c7c8

Please sign in to comment.