32 lines
806 B
JavaScript
32 lines
806 B
JavaScript
import Debug from 'debug';
|
|
import jwt from 'jsonwebtoken';
|
|
const debug = Debug('twtkpr:authCheckJWT');
|
|
/**
|
|
* Checks for a valid JWT, and returns a boolean indicating the result
|
|
*
|
|
* @param req
|
|
* @returns
|
|
*/
|
|
export default async function authCheckJWT(req, config) {
|
|
debug('beginning');
|
|
const token = req.header('Authorization')?.split(' ')[1];
|
|
if (!token) {
|
|
debug('no token');
|
|
return false;
|
|
}
|
|
debug('token present');
|
|
try {
|
|
const decoded = jwt.verify(token, config.accessSecret);
|
|
debug({ decoded });
|
|
if (!decoded.id)
|
|
return false;
|
|
req.username = decoded.id;
|
|
}
|
|
catch {
|
|
debug('invalid token');
|
|
return false;
|
|
}
|
|
debug('token good');
|
|
return true;
|
|
}
|
|
//# sourceMappingURL=authCheckJWT.js.map
|