-
Notifications
You must be signed in to change notification settings - Fork 369
/
with-api-auth-required.ts
44 lines (40 loc) · 1.32 KB
/
with-api-auth-required.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { NextApiResponse, NextApiRequest, NextApiHandler } from 'next';
import { SessionCache } from '../session';
import { assertReqRes } from '../utils/assert';
/**
* Wrap an API route to check that the user has a valid session. If they're not logged in the
* handler will return a 401 Unauthorized.
*
* ```js
* // pages/api/protected-route.js
* import { withApiAuthRequired, getSession } from '@auth0/nextjs-auth0';
*
* export default withApiAuthRequired(function ProtectedRoute(req, res) {
* const session = getSession(req, res);
* ...
* });
* ```
*
* If you visit `/api/protected-route` without a valid session cookie, you will get a 401 response.
*
* @category Server
*/
export type WithApiAuthRequired = (apiRoute: NextApiHandler) => NextApiHandler;
/**
* @ignore
*/
export default function withApiAuthFactory(sessionCache: SessionCache): WithApiAuthRequired {
return (apiRoute) =>
async (req: NextApiRequest, res: NextApiResponse): Promise<void> => {
assertReqRes(req, res);
const session = await sessionCache.get(req, res);
if (!session || !session.user) {
res.status(401).json({
error: 'not_authenticated',
description: 'The user does not have an active session or is not authenticated'
});
return;
}
await apiRoute(req, res);
};
}