express-typeorm-rest-boilerplate
Version:
Boilerplate code to get started with building RESTful API Services
25 lines (21 loc) • 746 B
text/typescript
import jwt from 'express-jwt';
import config from '../../config';
import { Request } from 'express';
const getTokenFromHeader = (req: Request) => {
if (
(req.headers.authorization &&
req.headers.authorization.split(' ')[0] === 'Token') ||
(req.headers.authorization &&
req.headers.authorization.split(' ')[0] === 'Bearer')
) {
return req.headers.authorization.split(' ')[1];
}
return null;
};
const isAuth = jwt({
secret: config.jwtSecret, // The _secret_ to sign the JWTs
requestProperty: 'token', // Use req.token to store the JWT
getToken: getTokenFromHeader, // How to extract the JWT from the request
algorithms: ['HS256'], // Use the HS256 (HMAC with SHA-256) algorithm
});
export default isAuth;