-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.ts
31 lines (29 loc) · 905 Bytes
/
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
import { getToken } from "next-auth/jwt";
import withAuth from "next-auth/middleware";
import { NextRequest, NextResponse } from "next/server";
export default withAuth (async function middleware (request:NextRequest) {
const pathname = request.nextUrl.pathname;
const isAuth = await getToken({req:request});
const protectedRoutes = ['/app/profile'];
const isAuthRoute = pathname.startsWith('/auth')
const isProtectedRoute = protectedRoutes.some((route) =>
pathname.startsWith(route)
);
if (!isAuth && isProtectedRoute) {
return NextResponse.redirect(new URL ('/signin',request.url))
}
if(isAuthRoute && isAuth) {
return NextResponse.redirect(new URL ('/profile',request.url))
}
}, {
callbacks : {
async authorized () {
return true;
}
}
});
export const config = {
matcher: [
'/profile/:path*','/auth/:path*'
]
};