|
|
@@ -8,19 +8,36 @@ import { AuthOptions } from '../../config/vendure-config';
|
|
|
*/
|
|
|
export function extractSessionToken(
|
|
|
req: Request,
|
|
|
- tokenMethod: AuthOptions['tokenMethod'],
|
|
|
+ tokenMethod: Exclude<AuthOptions['tokenMethod'], undefined>,
|
|
|
): string | undefined {
|
|
|
+ const tokenFromCookie = getFromCookie(req);
|
|
|
+ const tokenFromHeader = getFromHeader(req);
|
|
|
+
|
|
|
if (tokenMethod === 'cookie') {
|
|
|
- if (req.session && req.session.token) {
|
|
|
- return req.session.token;
|
|
|
- }
|
|
|
- } else {
|
|
|
- const authHeader = req.get('Authorization');
|
|
|
- if (authHeader) {
|
|
|
- const matches = authHeader.match(/bearer\s+(.+)$/i);
|
|
|
- if (matches) {
|
|
|
- return matches[1];
|
|
|
- }
|
|
|
+ return tokenFromCookie;
|
|
|
+ } else if (tokenMethod === 'bearer') {
|
|
|
+ return tokenFromHeader;
|
|
|
+ }
|
|
|
+ if (tokenMethod.includes('cookie') && tokenFromCookie) {
|
|
|
+ return tokenFromCookie;
|
|
|
+ }
|
|
|
+ if (tokenMethod.includes('bearer') && tokenFromHeader) {
|
|
|
+ return tokenFromHeader;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function getFromCookie(req: Request): string | undefined {
|
|
|
+ if (req.session && req.session.token) {
|
|
|
+ return req.session.token;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function getFromHeader(req: Request): string | undefined {
|
|
|
+ const authHeader = req.get('Authorization');
|
|
|
+ if (authHeader) {
|
|
|
+ const matches = authHeader.match(/bearer\s+(.+)$/i);
|
|
|
+ if (matches) {
|
|
|
+ return matches[1];
|
|
|
}
|
|
|
}
|
|
|
}
|