Authentication
This API uses JSON Web Tokens (JWT) for securing endpoints. It employs a two-token strategy: an Access Token and a Refresh Token.
Access Token
Usage: Required for accessing protected API endpoints.
Format: Standard JWT.
Transmission: Must be included in the
Authorizationheader of your request using theBearerscheme.
Authorization: Bearer <your_access_token>Lifetime: Access tokens have a predefined expiration time (configured on the server, e.g., 15 minutes). Once expired, you'll receive a
401 Unauthorizederror.Obtaining: Received upon successful registration (
/auth/register) or login (/auth/login). Can be renewed using a valid Refresh Token (/auth/refresh-token).
Refresh Token
Usage: Used to obtain a new Access Token when the current one expires, without requiring the user to log in again.
Format: Standard JWT.
Transmission: Automatically handled via an
HttpOnly,Secure(in production),SameSite=Strictcookie namedrefreshToken. You generally don't need to manage this token directly in your client-side code.Lifetime: Refresh tokens have a longer expiration time (configured on the server, e.g., 7 days).
Obtaining: Set automatically as a cookie upon successful registration or login.
Invalidation: Deleted from the server and the cookie is cleared upon logout (
/auth/logout).
Authentication Flow
Register or Login: Call
/auth/registeror/auth/loginwith valid credentials.Receive an
accessTokenin the response body.Receive a
refreshTokenset as an HttpOnly cookie.
Access Protected Resources: Make requests to other API endpoints, including the
accessTokenin theAuthorization: Bearer <token>header.Handle Expired Access Token: If a request returns a
401 Unauthorizederror indicating an expired access token:Call
POST /auth/refresh-token. This endpoint uses therefreshTokencookie automatically sent by the browser.Receive a new
accessTokenin the response.Retry the original request with the new
accessToken.
Handle Expired Refresh Token: If the
/auth/refresh-tokenendpoint returns a401 Unauthorizederror, the refresh token has expired or is invalid. The user must log in again via/auth/login.Logout: Call
POST /auth/logout. This requires both theaccessToken(in the header) and therefreshToken(in the cookie) to be valid. It invalidates the refresh token on the server and clears the cookie.
Last updated
Was this helpful?