We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Bearer Tokens

Clients send authentication tokens to our API in the Authorization HTTP header:

Authorization: Bearer TOKEN_STRING

The word Bearer describes how the token is used: whoever has the token can use it to authenticate. In Chirpy, that token will be a JWT.

Before we can validate a JWT, we need to extract it from the header's value. We'll write a helper that accepts the full header string, strips off the Bearer prefix, and returns the token.

Assignment

  1. function extractBearerToken(authorization: string | undefined): string;
    

    It should:

    • Return only the token part from a header string like "Bearer TOKEN_STRING", removing the prefix and surrounding whitespace.
    • Throw a BadRequestError if the scheme isn't Bearer or the token is empty.

Run and submit the CLI tests.