export default async (req: Request, res: any, next: any) => {
try {
const bearerHeader = req.headers['authorization'];
const token = bearerHeader && bearerHeader.split(" ")[1];
if (!token) return res.sendStatus(401);
try {
const public_key = `-----BEGIN PUBLIC KEY-----\n${process.env.PUBLIC_KEY}\n-----END PUBLIC KEY-----`
const decocedToken = jwtmod.verify(token ?? "", public_key, {
algorithms: ["RS256"]
}) as IUser;
const user: IUser = { sub: '', ...(typeof decocedToken === 'object' ? decocedToken : {}) };
// req.user = email;
req.user = user;
if (req.user.sub === '' || !req.user.sub){
return res.status(401).send("Invalid token");
}
next();
} catch (error) {
console.log(error);
return res.sendStatus(403);
}
} catch (error) {
console.log(error)
return res.sendStatus(401);
}
}