Skip to content

Function: bearerAuth() ​

bearerAuth(getToken): BeforeRequestHook

Defined in: src/auth.ts:69

Returns a beforeRequest hook that adds an Authorization: Bearer <token> header before every request.

If getToken() returns null or undefined the header is left unset, allowing unauthenticated requests to pass through.

Parameters ​

getToken ​

() => string | null | undefined

Synchronous getter for the current access token.

Returns ​

BeforeRequestHook

A beforeRequest hook that sets the Authorization header.

Example ​

ts
import valifetch from 'valifetch';
import { bearerAuth } from 'valifetch/auth';

const api = valifetch.create({
  hooks: { beforeRequest: [bearerAuth(() => localStorage.getItem('token'))] },
});

Released under the MIT License. Built with Claude Code.