Localhost redirected you too many times NextJs 13 and JWT verify middleware

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

Localhost redirected you too many times NextJs 13 and JWT verify middleware

问题

以下是您要翻译的内容:

"一切都运行得很完美,唯一的问题是,当我登录时,我被重定向到"/"页面,如果用户想要进入"/login"或"/register"页面,即使已经登录,他们也应该被重定向到"/"页面,我如何在这段代码中添加这个功能,现在,即使已登录,我仍然能够进入"/login"页面,但页面上显示localhost重定向了您太多次的错误。

中间件代码 middleware.js:

import { NextResponse } from "next/server";
import { verify } from "jsonwebtoken";

export function middleware(request) {
  const secret = process.env.JWT_SECRET;
  const jwt = request.cookies.get("jwt")?.value;
  const path = request.nextUrl.pathname;

  if (!jwt && !["/login", "/register"].includes(path)) {
    return NextResponse.redirect(new URL("/login", request.url));
  } else if (jwt && (path === "/login" || path === "/register")) {
    try {
      verify(jwt, secret);
      return NextResponse.redirect(new URL("/", request.url));
    } catch (error) {
      return NextResponse.redirect(new URL("/login", request.url));
    }
  }
  return NextResponse.next();
}

export const config = {
  matcher: ["/dashboard/:path*", "/login", "/register", "/"],
};

我正在尝试使用JWT验证和NextJs进行成功的登录和注册。

英文:

Everything works perfectly fine one problem is that when I am logged In I am redirected to "/" page and if a user wanna go to "/login" or "/register" page even after being logged In they should be redirected to "/" page how can I add that feature in this code right now I am able get to "/login" page even after logged in but the page says localhost redirected you too many times error in the web page.

The code middleware.js :

import { NextResponse } from "next/server";
import { verify } from "jsonwebtoken";

export function middleware(request) {
  const secret = process.env.JWT_SECRET;
  const jwt = request.cookies.get("jwt")?.value;
  const path = request.nextUrl.pathname;

  if (!jwt && !["/login", "/register"].includes(path)) {
    return NextResponse.redirect(new URL("/login", request.url));
  } else if (jwt && (path === "/login" || path === "/register")) {
    try {
      verify(jwt, secret);
      return NextResponse.redirect(new URL("/", request.url));
    } catch (error) {
      return NextResponse.redirect(new URL("/login", request.url));
    }
  }
  return NextResponse.next();
}

export const config = {
  matcher: ["/dashboard/:path*", "/login", "/register", "/"],
};

I was trying successful login and register using JWT verify and NextJs

答案1

得分: 0

我认为 verify(jwt, secret) 失败了,它抛出了错误。因此,你进入了 catch 块。在 catch 块中,你被重定向到了 "login" 页面。所以,你正在执行以下代码:

else if (jwt && (path === "/login" || path === "/register"))

因为 jwt 有效且 path === "/login"。由于 verify 抛出错误,你陷入了一个循环。

英文:

I think verify(jwt, secret) is failing. it is throwing error. so you are running into catch block. In the catch block you are redirected to the "login` again. So, you are hitting

else if (jwt && (path === "/login" || path === "/register"))

because jwt is valid and path==="/login". since verify is throwing error, you are hitting the catch block so you are kinda in a loop

huangapple
  • 本文由 发表于 2023年5月21日 16:39:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298994.html
匿名

发表评论

匿名网友

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

确定