-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
52 lines (43 loc) · 1.66 KB
/
middleware.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
import NextAuth from 'next-auth';
import authConfig from '@/auth.config';
import { DEFAULT_NOT_SIGNIN_REDIRECT, apiAuthPrefix, publicRoutes } from '@/routes';
const { auth } = NextAuth(authConfig);
/**
* Middleware function for authentication.
* Handles route authorization and redirection based on the user's authentication status.
*
* @param {object} req - The request object.
* @returns {void | Response} - Returns void if it doesn't need further actions, or returns
* a redirection Response if the user is not logged in and the route is not public.
*/
export default auth((req) => {
// Extract the nextUrl from the request
const { nextUrl } = req;
const isLoggedIn = !!req.auth;
const isApiAuthRoute = nextUrl.pathname.startsWith(apiAuthPrefix);
const isPublicRoute = publicRoutes.includes(nextUrl.pathname);
if (isApiAuthRoute) {
return;
}
// Redirect to the default sign-in redirect URL if the user is not signed in and
// trying to accessing private routes
if (!isLoggedIn && !isPublicRoute) {
return Response.redirect(new URL(DEFAULT_NOT_SIGNIN_REDIRECT, nextUrl));
}
return;
});
export const config = {
/**
* Routes that match any of the specified patterns will be intercepted by the middleware.
* This allows the middleware to handle specific routes or groups of routes.
*
* Patterns:
* - `/((?!.+\\.[\\w]+$|_next).*)`: Matches any route except those with file extensions
* or starting with `_next`.
* - `/`: Matches the root route.
* - `/(api|trpc)(.*)`: Matches routes starting with either "/api" or "/trpc".
*
* @type {string[]}
*/
matcher: ['/((?!.+\\.[\\w]+$|_next).*)', '/', '/(api|trpc)(.*)'],
};