|
1 | | -import { getToken } from "next-auth/jwt" |
2 | | -import { withAuth } from "next-auth/middleware" |
3 | | -import { NextResponse } from "next/server" |
4 | | - |
5 | | -export default withAuth( |
6 | | - async function middleware(req) { |
7 | | - const token = await getToken({ req }) |
8 | | - const isAuth = !!token |
9 | | - const isAuthPage = req.nextUrl.pathname === "/" |
10 | | - if (isAuthPage) { |
11 | | - if (isAuth) { |
12 | | - return NextResponse.redirect(new URL("/dashboard", req.url)) |
13 | | - } |
14 | | - return null |
15 | | - } |
16 | | - |
17 | | - if (!isAuth) { |
18 | | - let from = req.nextUrl.pathname |
19 | | - if (req.nextUrl.search) { |
20 | | - from += req.nextUrl.search |
21 | | - } |
22 | | - |
23 | | - return NextResponse.redirect( |
24 | | - new URL(`/?from=${encodeURIComponent(from)}`, req.url) |
25 | | - ) |
26 | | - } |
27 | | - }, |
28 | | - { |
29 | | - callbacks: { |
30 | | - async authorized() { |
31 | | - return true |
32 | | - }, |
33 | | - }, |
34 | | - } |
35 | | -) |
| 1 | +import { getToken } from "next-auth/jwt" |
| 2 | +import { withAuth } from "next-auth/middleware" |
| 3 | +import { NextResponse } from "next/server" |
| 4 | + |
| 5 | +export default withAuth( |
| 6 | + async function middleware(req) { |
| 7 | + const token = await getToken({ req }) |
| 8 | + const isAuth = !!token |
| 9 | + const isAuthPage = req.nextUrl.pathname === "/" |
| 10 | + if (isAuthPage) { |
| 11 | + if (isAuth) { |
| 12 | + return NextResponse.redirect(new URL("/dashboard", req.url)) |
| 13 | + } |
| 14 | + return null |
| 15 | + } |
| 16 | + |
| 17 | + if (!isAuth) { |
| 18 | + let from = req.nextUrl.pathname |
| 19 | + if (req.nextUrl.search) { |
| 20 | + from += req.nextUrl.search |
| 21 | + } |
| 22 | + |
| 23 | + return NextResponse.redirect( |
| 24 | + new URL(`/?from=${encodeURIComponent(from)}`, req.url) |
| 25 | + ) |
| 26 | + } |
| 27 | + }, |
| 28 | + { |
| 29 | + callbacks: { |
| 30 | + async authorized() { |
| 31 | + return true |
| 32 | + }, |
| 33 | + }, |
| 34 | + } |
| 35 | +) |
0 commit comments