-
Notifications
You must be signed in to change notification settings - Fork 396
/
with-middleware-auth-required.ts
103 lines (98 loc) · 2.96 KB
/
with-middleware-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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { NextMiddleware, NextResponse } from 'next/server';
import { SessionCache } from '../session';
/**
* Protect your pages with Next.js Middleware. For example:
*
* To protect all your routes:
*
* ```js
* // middleware.js
* import { withMiddlewareAuthRequired } from '@auth0/nextjs-auth0/edge';
*
* export default withMiddlewareAuthRequired();
* ```
*
* To protect specific routes:
*
* ```js
* // middleware.js
* import { withMiddlewareAuthRequired } from '@auth0/nextjs-auth0/edge';
*
* export default withMiddlewareAuthRequired();
*
* export const config = {
* matcher: '/about/:path*',
* };
* ```
* For more info see: https://nextjs.org/docs/advanced-features/middleware#matching-paths
*
* To run custom middleware for authenticated users:
*
* ```js
* // middleware.js
* import { withMiddlewareAuthRequired, getSession } from '@auth0/nextjs-auth0/edge';
*
* export default withMiddlewareAuthRequired(async function middleware(req) {
* const res = NextResponse.next();
* const user = await getSession(req, res);
* res.cookies.set('hl', user.language);
* return res;
* });
* ```
*
* @category Server
*/
export type WithMiddlewareAuthRequired = (middleware?: NextMiddleware) => NextMiddleware;
/**
* @ignore
*/
export default function withMiddlewareAuthRequiredFactory(
{ login, callback }: { login: string; callback: string },
getSessionCache: () => SessionCache
): WithMiddlewareAuthRequired {
return function withMiddlewareAuthRequired(middleware?): NextMiddleware {
return async function wrappedMiddleware(...args) {
const [req] = args;
const { pathname, origin, search } = req.nextUrl;
const ignorePaths = [login, callback, '/_next', '/favicon.ico'];
if (ignorePaths.some((p) => pathname.startsWith(p))) {
return;
}
const sessionCache = getSessionCache();
const authRes = NextResponse.next();
const session = await sessionCache.get(req, authRes);
if (!session?.user) {
if (pathname.startsWith('/api')) {
return NextResponse.json(
{
error: 'not_authenticated',
description: 'The user does not have an active session or is not authenticated'
},
{ status: 401 }
);
}
return NextResponse.redirect(
new URL(`${login}?returnTo=${encodeURIComponent(`${pathname}${search}`)}`, origin)
);
}
const res = await (middleware && middleware(...args));
if (res) {
const nextRes = new NextResponse(res.body, res);
let cookies = authRes.cookies.getAll();
if ('cookies' in res) {
for (const cookie of res.cookies.getAll()) {
nextRes.cookies.set(cookie);
}
}
for (const cookie of cookies) {
if (!nextRes.cookies.get(cookie.name)) {
nextRes.cookies.set(cookie);
}
}
return nextRes;
} else {
return authRes;
}
};
};
}