-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
43 lines (32 loc) · 1.27 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
import { NextResponse } from "next/server"
import { exact, startsWith } from "./utils/modules"
import { cookies } from "next/headers"
import app from "./utils/server/api"
const middleware = async (request) => {
let path = request.nextUrl.pathname
const token = (await cookies()).get("token")?.value
const { message, ok: isEmptyAuthRoute } = exact.safeTest(path, "/auth")
// if (!token && startsWith(path, '/dashboard')) {
// return NextResponse.redirect(new URL("/auth/login", request.url))
// }
if (token && startsWith(path, '/auth')) {
return NextResponse.redirect(new URL("/dashboard", request.url))
} else if (token && startsWith(path, '/login')) {
return NextResponse.redirect(new URL("/dashboard", request.url))
} else if (!token && startsWith(path, '/dashboard')) {
return NextResponse.redirect(new URL("/auth/login", request.url))
}
if (isEmptyAuthRoute) {
return NextResponse.redirect(new URL("/auth/login", request.url))
}
console.log(path)
if (startsWith(path, "/dashboard/signout")) {
const logout = async () => {
const res = await app.get("/auth/logout")
console.log(res)
return res.data
}
logout()
}
}
export default middleware