Skip to content

Commit 90eae6f

Browse files
committed
docs(readme): add documentation for api page rewrite
1 parent 9533c19 commit 90eae6f

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,32 @@ yarn add nextjs-basic-auth-middleware
2121
### Next.js Middleware
2222
The Next.js middleware functionality allows you to add basic auth in front of all your requests, see the [Next.js Middleware documentation](https://nextjs.org/docs/advanced-features/middleware) for more information.
2323

24+
It consists of 2 parts, `createNextAuthMiddleware` for checking and redirecting and `createApiPage` to create the API page that sends a 401 message.
25+
2426
You can use the `createNextAuthMiddleware` function to create a default middleware function that sends a `NextResponse.next()` when the auth passes:
2527

2628
```js
29+
// middleware.ts
2730
import { createNextAuthMiddleware } from 'nextjs-basic-auth-middleware'
2831

2932
export const middleware = createNextAuthMiddleware(options)
33+
34+
export const config = {
35+
matcher: ['/(.*)'], // Replace this with your own matcher logic
36+
}
3037
```
3138

39+
Next create the API page that returns the `401` response:
40+
41+
```js
42+
// pages/api/auth.ts
43+
import { createApiPage } from 'nextjs-basic-auth-middleware'
44+
45+
export default createApiPage()
46+
```
47+
48+
**Optional**
49+
3250
You can also use the `nextBasicAuthMiddleware` function to check basic auth in a bigger middleware function:
3351

3452
```js
@@ -37,7 +55,7 @@ You can also use the `nextBasicAuthMiddleware` function to check basic auth in a
3755
export const middleware = (req) => {
3856
nextBasicAuthMiddleware(options, req)
3957

40-
// Your other middleware functions
58+
// Your other middleware functions here
4159

4260
return NextResponse.next()
4361
}

0 commit comments

Comments
 (0)