-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
48 lines (42 loc) · 1.12 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
import { env } from "@/configs/env";
import { getToken } from "next-auth/jwt";
import { withAuth } from "next-auth/middleware";
import { NextResponse } from "next/server";
// biome-ignore lint/style/noDefaultExport: Next-Auth
export default withAuth(
async (req) => {
const url = req.nextUrl.clone();
const token = await getToken({ req });
if (token) {
if (url.pathname === "/") {
url.pathname = "/dashboard";
return NextResponse.redirect(url);
}
} else if (url.pathname !== "/") {
url.pathname = "/";
return NextResponse.redirect(url);
}
return NextResponse.next();
},
{
pages: {
signIn: "/"
},
callbacks: {
authorized: async ({ token }) => !!token
},
secret: env.NEXTAUTH_SECRET
}
);
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
"/((?!api|_next/static|_next/image|_next/webpack-hmr|favicon.ico).*)"
]
};