admin管理员组文章数量:1419168
if (
adminconfig?.matcher.includes(pathname) ||
pathname.startsWith("/admin/")
) {
if (!isAuthenticated) {
return NextResponse.redirect(new URL("/", request.nextUrl));
} else if (isAdmin) {
return NextResponse.redirect(
new URL("/admin/main/users", request.nextUrl)
);
} else {
return NextResponse.redirect(new URL("/not-admin", request.nextUrl));
}
}
if user's role is admin, he/she can redirecting admin url. If not, redşrecting not-admin page.
not-admin redirecting is working. But if user is admin, admin/main/users or admin/path* not working. The error:
'This page isn’t working
localhost redirected you too many times.
- Try deleting your cookies.
ERR_TOO_MANY_REDIRECTS '
How can I config to middleware for admin?
if (
adminconfig?.matcher.includes(pathname) ||
pathname.startsWith("/admin/")
) {
if (!isAuthenticated) {
return NextResponse.redirect(new URL("/", request.nextUrl));
} else if (isAdmin) {
return NextResponse.redirect(
new URL("/admin/main/users", request.nextUrl)
);
} else {
return NextResponse.redirect(new URL("/not-admin", request.nextUrl));
}
}
if user's role is admin, he/she can redirecting admin url. If not, redşrecting not-admin page.
not-admin redirecting is working. But if user is admin, admin/main/users or admin/path* not working. The error:
'This page isn’t working
localhost redirected you too many times.
- Try deleting your cookies.
ERR_TOO_MANY_REDIRECTS '
How can I config to middleware for admin?
Share Improve this question edited Jan 29 at 8:11 DarkBee 15.5k8 gold badges72 silver badges118 bronze badges asked Jan 29 at 8:08 Gülbeyaz BAYRAM ÖZERGülbeyaz BAYRAM ÖZER 111 bronze badge1 Answer
Reset to default 0You have a logical flaw here - if the path starts with /amdin
and the user is authenticated as an admin, they're redirected to /admin/main/users
. The problem is that that path also starts with /admin
, so it's ridercted again and so on, until it fail with ERR_TOO_MANY_REDIRECTS
.
One approach to solve this is to not redirect if the URL is already /admin/main/users
:
if (
adminconfig?.matcher.includes(pathname) ||
pathname.startsWith("/admin/")
) {
if (!isAuthenticated) {
return NextResponse.redirect(new URL("/", request.nextUrl));
} else if (isAdmin &&
!pathname.startsWith("/admin/main/users")) { // Here!
return NextResponse.redirect(
new URL("/admin/main/users", request.nextUrl)
);
} else {
return NextResponse.redirect(new URL("/not-admin", request.nextUrl));
}
}
本文标签: nextjsMiddleware admin configurationStack Overflow
版权声明:本文标题:next.js - Middleware admin configuration - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745305379a2652613.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论