Skip to content

Commit 9b01316

Browse files
committed
feat(middleware): add support for next 13.1 middleware responses
The api page workaround felt as a hack. With next 13.1 we can directly return a response with headers, which is enough to show the 401 page.
1 parent cb3489f commit 9b01316

File tree

6 files changed

+347
-356
lines changed

6 files changed

+347
-356
lines changed

Diff for: README.md

-9
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ You can use the `createNextAuthMiddleware` function to create a default middlewa
3737
}
3838
```
3939

40-
Next create the API page that returns the `401` response:
41-
42-
```js
43-
// pages/api/auth.ts
44-
import { createApiPage } from 'nextjs-basic-auth-middleware'
45-
46-
export default createApiPage()
47-
```
48-
4940
**Optional**
5041

5142
You can also use the `nextBasicAuthMiddleware` function to check basic auth in a bigger middleware function:

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"dependencies": {},
2222
"peerDependencies": {
23-
"next": ">=12"
23+
"next": ">=13.1"
2424
},
2525
"prettier": {
2626
"printWidth": 80,
@@ -42,7 +42,7 @@
4242
"@types/node": "14.14.8",
4343
"husky": "4.3.0",
4444
"jest": "28.1.3",
45-
"next": "12.2.5",
45+
"next": "13.2.3",
4646
"node-mocks-http": "1.9.0",
4747
"tslib": "2.4.0",
4848
"tsup": "6.2.2",

Diff for: src/index.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
export {
2-
createApiPage,
3-
createNextAuthMiddleware,
4-
nextBasicAuthMiddleware,
5-
} from './middleware'
1+
export { createNextAuthMiddleware, nextBasicAuthMiddleware } from './middleware'
62
export type { MiddlewareOptions } from './types'

Diff for: src/middleware.ts

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { NextApiRequest, NextApiResponse } from 'next'
21
import type { NextRequest } from 'next/server'
32
import { NextResponse } from 'next/server'
43
import { basicAuthentication } from './lib/auth'
@@ -16,12 +15,22 @@ import { MiddlewareOptions } from './types'
1615
* @returns Either a 401 error or goes to the next page
1716
*/
1817
export const createNextAuthMiddleware =
19-
({ pathname = '/api/auth', users = [] }: MiddlewareOptions = {}) =>
18+
({
19+
pathname = '/api/auth',
20+
users = [],
21+
message = 'Authentication failed',
22+
realm = 'protected',
23+
}: MiddlewareOptions = {}) =>
2024
(req: NextRequest) =>
21-
nextBasicAuthMiddleware({ pathname, users }, req)
25+
nextBasicAuthMiddleware({ pathname, users, message, realm }, req)
2226

2327
export const nextBasicAuthMiddleware = (
24-
{ pathname = '/api/auth', users = [] }: MiddlewareOptions = {},
28+
{
29+
pathname = '/api/auth',
30+
users = [],
31+
message = 'Authentication failed',
32+
realm = 'protected',
33+
}: MiddlewareOptions = {},
2534
req: NextRequest
2635
) => {
2736
// Check if credentials are set up
@@ -49,19 +58,8 @@ export const nextBasicAuthMiddleware = (
4958

5059
url.pathname = pathname
5160

52-
return NextResponse.rewrite(url)
61+
return new NextResponse(message, {
62+
status: 401,
63+
headers: { 'WWW-Authenticate': `Basic realm="${realm}"` },
64+
})
5365
}
54-
55-
/**
56-
* Create an API page that handles returning a 401 authentication failed message
57-
* @param realm The protection space
58-
* @param message Message you want to show to the users
59-
* @returns Next API page
60-
*/
61-
export const createApiPage =
62-
(realm = 'protected', message = 'Authentication failed') =>
63-
(_: NextApiRequest, res: NextApiResponse) => {
64-
res.setHeader('WWW-Authenticate', `Basic realm="${realm}"`)
65-
res.statusCode = 401
66-
res.end(message)
67-
}

Diff for: src/types.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ import { AuthCredentials } from './lib/credentials'
33
export type MiddlewareOptions = {
44
pathname?: string
55
users?: AuthCredentials
6+
message?: string
7+
realm?: string
68
}

0 commit comments

Comments
 (0)