extract-auth-token.ts 718 B

1234567891011121314151617181920212223
  1. import { Request } from 'express';
  2. import { AuthOptions } from '../../config/vendure-config';
  3. /**
  4. * Get the session token from either the cookie or the Authorization header, depending
  5. * on the configured tokenMethod.
  6. */
  7. export function extractAuthToken(req: Request, tokenMethod: AuthOptions['tokenMethod']): string | undefined {
  8. if (tokenMethod === 'cookie') {
  9. if (req.session && req.session.token) {
  10. return req.session.token;
  11. }
  12. } else {
  13. const authHeader = req.get('Authorization');
  14. if (authHeader) {
  15. const matches = authHeader.match(/bearer\s+(.+)$/i);
  16. if (matches) {
  17. return matches[1];
  18. }
  19. }
  20. }
  21. }