middleware | custom redirect #4882
-
/I want the user to get redirect to their dashboard(/dashboard) Instead of getting them redirectet to the login form, if they try to access the premiumarea. Is that possible somehow? `import { withAuth } from "next-auth/middleware"; // More on how NextAuth.js middleware works: https://next-auth.js.org/configuration/nextjs#middleware
}, export const config = { matcher: ["/admin", "/me", "/premiumarea"] };` |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
Unfortunately, this isn't possible atm. In middleware, we only allow redirecting to the sign-in page. See: |
Beta Was this translation helpful? Give feedback.
-
Hi @KoesterJannik, I've been trying to do similar but redirecting them to the homepage instead of login and seemed to have achieved this with the following middleware.ts file import { getToken } from "next-auth/jwt";
import { NextRequest, NextResponse } from "next/server";
export { default } from "next-auth/middleware";
export const config = { matcher: ["/admin/:path*"] };
export async function middleware(request: NextRequest) {
const token = await getToken({ req: request });
if (!token && process.env.NEXTAUTH_URL) {
return NextResponse.redirect(process.env.NEXTAUTH_URL);
}
} This is working great on my local environment although haven't tested in production yet. Wanted to add this comment though for anyone else trying to achieve similar |
Beta Was this translation helpful? Give feedback.
-
Actually this can be done while keeping NextAuth login page redirect. Instead of trying to reroute the user in the import { withAuth } from 'next-auth/middleware';
import { NextResponse } from 'next/server';
export default withAuth(req => {
if ( req.nextUrl.pathname.startsWith('/admin') ) {
if (req.nextauth.token.userRole !== 'Admin') {
return NextResponse.redirect(new URL('/dashboard', req.url));
}
}
if ( req.nextUrl.pathname.startsWith('/premium') ) {
if (req.nextauth.token.userRole !== 'Premium') {
return NextResponse.redirect(new URL('/dashboard', req.url));
}
}
});
export const config = {
matcher: ['/admin/:path*', '/premium/:path*']
}; This is the flow you can expect for users:
|
Beta Was this translation helpful? Give feedback.
-
In case anyone is seeing this comment in 2025 this was my way: import { getToken } from "next-auth/jwt";
import { NextRequest, NextResponse } from "next/server";
export { default } from "next-auth/middleware";
export const config = {
matcher: ["/auth/:path*", "/dashboard/:path*"],
};
const SIGNIN_ROUTE= "/signin"
const SIGNUP_ROUTE= "/signup"
const AUTH_ROUTES = [SIGNIN_ROUTE, SIGNUP_ROUTE]
const DASHBOARD_ROUTE = "/dashboard"
export async function middleware(request: NextRequest) {
const urlPath = request.nextUrl.pathname;
const token = await getToken({ req: request });
if (DASHBOARD_ROUTE.includes(urlPath) && !token) {
return NextResponse.redirect(new URL(SIGNIN_ROUTE, request.url));
}
if (AUTH_ROUTES.includes(urlPath) && token) {
return NextResponse.redirect(new URL(DASHBOARD_ROUTE, request.url));
}
return NextResponse.next();
} |
Beta Was this translation helpful? Give feedback.
Unfortunately, this isn't possible atm. In middleware, we only allow redirecting to the sign-in page. See:
next-auth/packages/next-auth/src/next/middleware.ts
Line 140 in 8853000