---
url: https://haihv.github.io/valifetch/api/auth/functions/basicAuth.md
---

# Function: basicAuth()

> **basicAuth**(`user`, `pass`): [`BeforeRequestHook`](../../index/type-aliases/BeforeRequestHook.md)

Defined in: [src/auth.ts:113](https://github.com/haihv/valifetch/blob/285835d38a6676b98d2ee1a93e23166c6c834cb2/src/auth.ts#L113)

Returns a `beforeRequest` hook that adds an `Authorization: Basic <credentials>`
header before every request. Credentials are encoded once at creation time.

The `user:pass` string is UTF-8 encoded before base64 (not `btoa` directly,
which only supports Latin-1), so non-ASCII usernames/passwords are safe.

## Parameters

### user

`string`

The username (must not contain a colon).

### pass

`string`

The password.

## Returns

[`BeforeRequestHook`](../../index/type-aliases/BeforeRequestHook.md)

A `beforeRequest` hook that sets the `Authorization` header.

## Example

```ts
import valifetch from 'valifetch';
import { basicAuth } from 'valifetch/auth';

const api = valifetch.create({
  hooks: { beforeRequest: [basicAuth('admin', 's3cr3t')] },
});
```
