-
Notifications
You must be signed in to change notification settings - Fork 76
/
middleware.js
52 lines (43 loc) · 1.79 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
40
41
42
43
44
45
46
47
48
49
50
51
52
import { NextResponse } from 'next/server';
import { checkAuth } from "@keystone/utils/checkAuth";
export async function middleware(request) {
const { authenticatedItem: isAuth, redirectToInit } = await checkAuth(request);
// Paths that don't require authentication
const publicPaths = ['/signin', '/signup', '/reset', '/init'];
const isPublicPath = publicPaths.some(path => request.nextUrl.pathname.startsWith(path));
// Check if sign-ups are allowed
const allowSignUp = process.env.ALLOW_EXTERNAL_SIGNUPS === 'true';
if (redirectToInit && !request.nextUrl.pathname.startsWith('/init')) {
return NextResponse.redirect(new URL('/init', request.url));
}
if (!redirectToInit && request.nextUrl.pathname.startsWith('/init')) {
return NextResponse.redirect(new URL('/', request.url));
}
if (isPublicPath) {
if (isAuth && !request.nextUrl.pathname.startsWith('/reset')) {
return NextResponse.redirect(new URL('/', request.url));
}
// Redirect to sign-in if sign-ups are not allowed and the user is trying to access the signup page
if (request.nextUrl.pathname.startsWith('/signup') && !allowSignUp) {
return NextResponse.redirect(new URL('/signin', request.url));
}
return NextResponse.next();
}
if (!isAuth) {
const from = request.nextUrl.pathname + request.nextUrl.search;
return NextResponse.redirect(new URL(`/signin?from=${encodeURIComponent(from)}`, request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api/graphql (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'/((?!api/graphql|_next/static|_next/image|favicon.ico).*)',
],
};