import jwt from 'jsonwebtoken'
const auth=async (req,res,next)=>{
try {
const token=req.headers.authorization.split(''[1]);
const isCustomAuth=token.length<500;
let decodedData;
if(token && isCustomAuth){
decodedData=jwt.verify(token,process.env.SECRET);
req.userId=decodedData?.id;
}
else{
decodedData=jwt.decode(token);
req.userId=decodedData?.sub;
}
next();
} catch (error) {
console.log(error);
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter