mirror of
https://github.com/shadcn-ui/taxonomy
synced 2026-05-24 09:48:32 +00:00
fix: middleware redirects for auth pages
This commit is contained in:
parent
a9310f58b8
commit
8706f45686
1 changed files with 35 additions and 9 deletions
|
|
@ -1,14 +1,40 @@
|
|||
import { NextResponse } from "next/server"
|
||||
import { withAuth } from "next-auth/middleware"
|
||||
|
||||
import { getSession } from "@/lib/session"
|
||||
|
||||
export default withAuth({
|
||||
callbacks: {
|
||||
async authorized({ req }) {
|
||||
const session = await getSession(req.headers.get("cookie"))
|
||||
return !!session
|
||||
},
|
||||
},
|
||||
})
|
||||
export default withAuth(
|
||||
async function middleware(req) {
|
||||
const session = await getSession(req.headers.get("cookie"))
|
||||
|
||||
export const config = { matcher: ["/dashboard/:path*", "/editor"] }
|
||||
const isAuthPage =
|
||||
req.nextUrl.pathname.startsWith("/login") ||
|
||||
req.nextUrl.pathname.startsWith("/register")
|
||||
|
||||
if (isAuthPage) {
|
||||
if (session) {
|
||||
return NextResponse.redirect(new URL("/dashboard", req.url))
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.redirect(new URL("/login", req.url))
|
||||
}
|
||||
},
|
||||
{
|
||||
callbacks: {
|
||||
async authorized() {
|
||||
// This is a work-around for handling redirect on auth pages.
|
||||
// We return true here so that the middleware function above
|
||||
// is always called.
|
||||
return true
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
export const config = {
|
||||
matcher: ["/dashboard/:path*", "/editor", "/login", "/register"],
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue