WayrApp Backend & Ecosystem Documentation - v1.0.0
    Preparing search index...

    Function extractTokenFromHeader

    • Extract Bearer token from Authorization header

      Parses the Authorization header to extract JWT tokens following the Bearer token authentication scheme. Validates the header format and returns the token portion, or null if the header is missing or malformed.

      This utility function is used by authentication middleware to extract tokens from HTTP requests for verification. It handles the standard "Bearer " format and provides safe parsing with null returns for invalid formats.

      Parameters

      • OptionalauthHeader: string

        Authorization header value (optional)

      Returns null | string

      Extracted token string or null if header is invalid/missing

      // Extract token from request header
      const authHeader = req.headers.authorization; // 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
      const token = extractTokenFromHeader(authHeader);
      console.log(token); // 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
      // Handle missing or invalid headers
      extractTokenFromHeader(undefined); // returns null
      extractTokenFromHeader('Invalid header'); // returns null
      extractTokenFromHeader('Bearer'); // returns null
      extractTokenFromHeader('Basic dXNlcjpwYXNz'); // returns null (wrong scheme)