Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const getJWTFromRequest = (req: any) => {
// Support JWT via cookies from the user session
const cookies = (req && req.cookies) || (req && req.headers && req.headers.cookie)
if (cookies && isString(cookies)) {
return cookies && parse(cookies).jwt
}
if (cookies && cookieParser.JSONCookies(cookies).jwt) {
return cookieParser.JSONCookies(cookies).jwt
}
// Support standard auth: "Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l"
const basicAuth = req.headers.Authorization || req.headers.authorization
if (basicAuth) {
if (basicAuth.includes("Basic ")) {
return basicAuth.split("Basic ")[1]
}
}
// No other auth routes
return undefined
}
public create({headers}: APIRequestContextSource) {
if (!headers.cookie) {
return {};
}
const {secrets, ...parseOptions} = this.opts;
const cookies = parseCookieString(headers.cookie, parseOptions);
// parse signed cookies
if (secrets.length !== 0) {
Object.assign(cookies, signedCookies(cookies, secrets));
}
// parse JSON cookies
return JSONCookies(cookies);
}
}