Skip to content

Commit

Permalink
jwt token changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sarithapillai8 committed Nov 6, 2024
1 parent 370b701 commit da9f337
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/service/jwtToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

import { Request, Response, NextFunction } from 'express';
import { jwtVerify } from 'jose';

const JWT_SECRET = process.env.secret_key as string;

export async function authenticateToken(req: Request, res: Response, next: NextFunction) {
const authHeader = req.headers['authorization'];
if (!authHeader) {
return res.status(401).json({ error: '401.missing-token' });
}

const token = authHeader.split(' ')[1];
if (!token) {
return res.status(401).json({ error: '401.missing-token' });
}

try {
const secretKey = new TextEncoder().encode(JWT_SECRET);
const {payload}: any = await jwtVerify(token, secretKey)

if (typeof payload !== 'object' || !payload) {
return res.status(403).json({ error: '403.invalid-token' });
}
// const pld = jwtDecrypt(payload)
// const { access_key, secret_key } = payload as { access_key: string, secret_key: string };

// Set the Authorization header in the format expected by _createAuthSubject
// req.headers['authorization'] = `Basic ${Buffer.from(`${access_key}:${secret_key}`).toString('base64')}`;
next();
} catch(err){
// return res.status(403).json({ error: '403.invalid-token2' });
console.log(err)
}
}

0 comments on commit da9f337

Please sign in to comment.