Next.js Discord

Discord Forum

Middleware is not redirecting

Unanswered
Nashville Warbler posted this in #help-forum
Open in Discord
Avatar
Nashville WarblerOP
I'm trying to redirect users to a path '/not-allowed' for all users who are not logged in or users who are logged in but their email is not included in the environment variable ADMIN_EMAIL_LIST. This is not redirecting as I expect. I am testing on the home page "/" and I am not logged in, but it does not redirect me. Also, I am trying to console.log in the middleware file but I don't see any of the logs in the console where my server is running. Should the logs appear? Here is the code I have in my middleware:

import { auth } from "@/lib/auth"
import { NextResponse } from "next/server";
export default auth((req) => {
    const isUserAdmin = process.env.ADMIN_EMAIL_LIST?.split(",").some((email) => {
        return email === req.auth?.user?.email;
    })

    console.log('isUserAdmin', isUserAdmin);
    if (!isUserAdmin) {
        return NextResponse.rewrite(new URL('/not-allowed', req.url))
    }
})

export const config = {
    matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};

10 Replies

Avatar
Nashville WarblerOP
This is all I see in the terminal tab that is running the server
Image
Avatar
Nashville WarblerOP
Bump 🙏🏻
Avatar
Siberian
are you sure this is running at all? What does auth do?
I think you might be able to export default, but try exporting a named function middleware
Avatar
Nashville WarblerOP
Hmm I’m not sure if the middleware is running because I wasn’t able to see any console logs output. I restarted the server a couple of times but still nothing. Thanks, I’ll try exporting a named function middleware.
Avatar
Siberian
also check what auth does
Avatar
Nashville WarblerOP
auth is a method exported from NextAuth, I'm using the latest version of next-auth / auth.js. I have updated my files slightly according to the Next.js "Learn" tutorial (https://nextjs.org/learn/dashboard-app/adding-authentication), but I am still not having any luck getting the expected redirect and not seeing the console.log.
Here is what my files look like now...
// auth.ts
import NextAuth, { NextAuthConfig } from 'next-auth';
import GitHub from "@auth/core/providers/github";

export const config = {
    callbacks: {
        authorized({ auth, request: { nextUrl } }) {
            const isUserAdmin = process.env.ADMIN_EMAIL_LIST?.split(",").some((email) => {
                return email === auth?.user?.email;
            })

            console.log('isUserAdmin', isUserAdmin);

            if (!isUserAdmin) {
                return Response.redirect(new URL('/not-allowed', nextUrl))
            }

            return true;
        },
    },
    providers: [
        GitHub
    ]
} satisfies NextAuthConfig

export const { handlers, auth, signIn, signOut } = NextAuth(config);
// middleware.ts
import { auth } from '@/lib/auth';

export default auth;

export const config = {
    matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};