You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

访问未配置URL时出现/auth/auth/嵌套重定向问题求助

问题:未定义URL重定向时出现路径嵌套循环

访问routes.ts中未配置的URL(如点击指向/auth/reset的按钮)时,页面未正确重定向至localhost:3000/auth/login,而是生成多层嵌套的/auth/auth/.../login路径。

原因分析

在middleware.ts的重定向逻辑中,使用了相对路径'auth/login'作为重定向目标:

return Response.redirect(new URL('auth/login', nextUrl))

当当前路径为/auth/reset时,nextUrl的基准路径是/auth/reset,相对路径会在其基础上叠加,导致每次重定向都生成/auth/reset/auth/login;而该路径同样未被配置为公开或认证路由,又会触发新一轮重定向,最终形成路径嵌套循环。

解决方案

将重定向目标改为绝对路径'/auth/login',确保从根目录开始定位,避免路径叠加:

修改middleware.ts中的对应代码:

// 原代码
// return Response.redirect(new URL('auth/login', nextUrl))

// 修改后
return Response.redirect(new URL('/auth/login', nextUrl))

额外建议

如果/auth/reset是实际需要的重置密码页面,需将其添加到routes.ts的authRoutes数组中,避免触发未登录重定向:

export const authRoutes = [
    "/auth/login",
    "/auth/register",
    "/auth/error",
    "/auth/reset" // 添加此行
]

内容的提问来源于stack exchange,提问作者Stykgwar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 12:57:42