-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.js
39 lines (32 loc) · 1.12 KB
/
middleware.js
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
import { NextResponse } from 'next/server';
import { auth0 } from './lib/auth0';
import { jwtDecode } from 'jwt-decode';
export async function middleware(request) {
const authRes = await auth0.middleware(request);
// authentication routes are handled by auth0
if (request.nextUrl.pathname.startsWith('/auth')) {
return authRes;
}
const { origin } = new URL(request.url);
const session = await auth0.getSession();
// no user session — redirect to login
if (!session) {
return NextResponse.redirect(`${origin}/auth/login`);
}
// add userUuid to session so it can be accessed by the client
// note: userUuid is added to accessToken in an auth0 action
const { accessToken } = session.tokenSet;
const decodedAccessToken = jwtDecode(accessToken);
const userUuid = decodedAccessToken['urn:bbThingsApp/userUuid'];
await auth0.updateSession(request, authRes, {
...session,
user: {
...session.user,
userUuid
}
});
return authRes;
}
export const config = {
matcher: ['/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)']
};