Next-Auth中间件禁用未经身份验证用户的注册页面。

huangapple go评论75阅读模式
英文:

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'],
}

huangapple
  • 本文由 发表于 2023年5月25日 23:50:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定