英文:
Next-Auth Middleware Disables Register Page for Non-authenticated users
问题
我实施了 Next-Auth,并添加了一个中间件,如果用户已经登录,则禁用/register和/login路由。但如果用户未经身份验证并想注册,他们无法访问/register路由,并将被重定向到/login。
import { getToken } from 'next-auth/jwt'
import { withAuth } from 'next-auth/middleware'
import { NextFetchEvent, NextRequest, NextResponse } from 'next/server'
export default async function middleware(
req: NextRequest,
event: NextFetchEvent
) {
const token = await getToken({ req })
const isAuthenticated = !!token
if (req.nextUrl.pathname.startsWith('/login') && isAuthenticated) {
return NextResponse.redirect(new URL('/profile', req.url))
}
const authMiddleware = await withAuth({
pages: {
signIn: `/login`,
},
})
// @ts-expect-error
return authMiddleware(req, event)
}
export const config = {
matcher: ['/login', '/register'],
}
尝试了一些if
条件的变化并调整了matcher
,但问题仍然存在。
英文:
So I implemented Next-Auth and added a middleware that disables the /register and /login routes if the user is authenticated. But if the user is not authenticated and wants to register, they can't access the /register route and get forwarded to /login.
import { getToken } from 'next-auth/jwt'
import { withAuth } from 'next-auth/middleware'
import { NextFetchEvent, NextRequest, NextResponse } from 'next/server'
export default async function middleware(
req: NextRequest,
event: NextFetchEvent
) {
const token = await getToken({ req })
const isAuthenticated = !!token
if (req.nextUrl.pathname.startsWith('/login') && isAuthenticated) {
return NextResponse.redirect(new URL('/profile', req.url))
}
const authMiddleware = await withAuth({
pages: {
signIn: `/login`,
},
})
// @ts-expect-error
return authMiddleware(req, event)
}
export const config = {
matcher: ['/login', '/register'],
}
Tried few variations of if
checks and played with the matcher
but the issue still persisted.
答案1
得分: 0
这解决了它。
import { getToken } from 'next-auth/jwt'
import { withAuth } from 'next-auth/middleware'
import { NextFetchEvent, NextRequest, NextResponse } from 'next/server'
export default async function middleware(
req: NextRequest,
event: NextFetchEvent
) {
const token = await getToken({ req })
const isAuthenticated = !!token
if (req.nextUrl.pathname.startsWith('/login') && isAuthenticated) {
return NextResponse.redirect(new URL('/profile', req.url))
}
if (req.nextUrl.pathname.startsWith('/register') && isAuthenticated) {
return NextResponse.redirect(new URL('/profile', req.url))
}
const authMiddleware = await withAuth({
pages: {
signIn: req.nextUrl.pathname,
},
})
// @ts-expect-error
return authMiddleware(req, event)
}
export const config = {
matcher: ['/login', '/register'],
}
英文:
This solved it.
import { getToken } from 'next-auth/jwt'
import { withAuth } from 'next-auth/middleware'
import { NextFetchEvent, NextRequest, NextResponse } from 'next/server'
export default async function middleware(
req: NextRequest,
event: NextFetchEvent
) {
const token = await getToken({ req })
const isAuthenticated = !!token
if (req.nextUrl.pathname.startsWith('/login') && isAuthenticated) {
return NextResponse.redirect(new URL('/profile', req.url))
}
if (req.nextUrl.pathname.startsWith('/register') && isAuthenticated) {
return NextResponse.redirect(new URL('/profile', req.url))
}
const authMiddleware = await withAuth({
pages: {
signIn: req.nextUrl.pathname,
},
})
// @ts-expect-error
return authMiddleware(req, event)
}
export const config = {
matcher: ['/login', '/register'],
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论