forked from wishonia/wishonia
-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.ts
53 lines (46 loc) · 1.29 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
53
import { NextResponse } from "next/server"
import { getToken } from "next-auth/jwt"
import { withAuth } from "next-auth/middleware"
import { getDomainConfig } from "@/lib/utils/domain-config"
export default withAuth(
async function middleware(req) {
const token = await getToken({ req })
const isAuth = !!token
const isAuthPage =
req.nextUrl.pathname.startsWith("/signin") ||
req.nextUrl.pathname.startsWith("/signup")
const hostname = req.headers.get("host")
const domainConfig = getDomainConfig(hostname)
// Check if we're on the root path
if (req.nextUrl.pathname === "/") {
// Only redirect if defaultHomepage is not root path
if (domainConfig.defaultHomepage !== "/") {
return NextResponse.redirect(
new URL(domainConfig.defaultHomepage, req.url)
)
}
return null
}
// Handle auth pages
if (isAuthPage) {
if (isAuth) {
return NextResponse.redirect(
new URL(domainConfig.afterLoginPath, req.url)
)
}
return null
}
return null
},
{
callbacks: {
async authorized() {
return true
},
},
}
)
// This indicates which routes use this middleware
export const config = {
matcher: ["/", "/dashboard/:path*", "/signin", "/signup"],
}