✨ Refactor JWT utility functions for improved readability and consistency
This commit is contained in:
@@ -24,9 +24,9 @@ function createMockJWT() {
|
||||
return {
|
||||
sign: (payload, secret, options) => {
|
||||
// Mock JWT: base64(header).base64(payload).base64(signature)
|
||||
const header = Buffer.from(JSON.stringify({ alg: 'HS256', typ: 'JWT' })).toString(
|
||||
'base64'
|
||||
);
|
||||
const header = Buffer.from(
|
||||
JSON.stringify({ alg: 'HS256', typ: 'JWT' })
|
||||
).toString('base64');
|
||||
const body = Buffer.from(JSON.stringify(payload)).toString('base64');
|
||||
const signature = Buffer.from('mock-signature').toString('base64');
|
||||
return `${header}.${body}.${signature}`;
|
||||
@@ -36,18 +36,18 @@ function createMockJWT() {
|
||||
const parts = token.split('.');
|
||||
if (parts.length !== 3) throw new Error('Invalid token format');
|
||||
const payload = JSON.parse(Buffer.from(parts[1], 'base64').toString());
|
||||
|
||||
|
||||
// Check expiration
|
||||
if (payload.exp && Date.now() >= payload.exp * 1000) {
|
||||
throw new Error('Token expired');
|
||||
}
|
||||
|
||||
|
||||
return payload;
|
||||
} catch (err) {
|
||||
throw new Error(`Invalid token: ${err.message}`);
|
||||
}
|
||||
},
|
||||
decode: (token) => {
|
||||
decode: token => {
|
||||
const parts = token.split('.');
|
||||
if (parts.length !== 3) return null;
|
||||
return JSON.parse(Buffer.from(parts[1], 'base64').toString());
|
||||
@@ -64,7 +64,7 @@ function createMockJWT() {
|
||||
*/
|
||||
export function createToken(payload, secret, expiresIn = 7 * 24 * 60 * 60) {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
|
||||
|
||||
return jwt.sign(
|
||||
{
|
||||
...payload,
|
||||
|
||||
Reference in New Issue
Block a user