Files
payload/src/auth/getExtractJWT.ts
James Mikrut 7083225abd Feat/remove this bindings (#629)
* feat: removes this bindings for cleaner, more maintainable code

Co-authored-by: Elliot DeNolf <denolfe@users.noreply.github.com>
Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
2022-06-08 14:44:34 -04:00

29 lines
854 B
TypeScript

/* eslint-disable react/destructuring-assignment */
import { Request } from 'express';
import { SanitizedConfig } from '../config/types';
import parseCookies from '../utilities/parseCookies';
const getExtractJWT = (config: SanitizedConfig) => (req: Request): string | null => {
if (req && req.get) {
const jwtFromHeader = req.get('Authorization');
const origin = req.get('Origin');
if (jwtFromHeader && jwtFromHeader.indexOf('JWT ') === 0) {
return jwtFromHeader.replace('JWT ', '');
}
const cookies = parseCookies(req);
const tokenCookieName = `${config.cookiePrefix}-token`;
if (cookies && cookies[tokenCookieName]) {
if (!origin || config.csrf.length === 0 || config.csrf.indexOf(origin) > -1) {
return cookies[tokenCookieName];
}
}
}
return null;
};
export default getExtractJWT;