#How to secure specific pages using NextAuth and Middleware after authentication

17 messages · Page 1 of 1 (latest)

twilit burrowBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

solemn trail
#

Try using the middleware this way:

onyx rain
# solemn trail https://github.com/nextauthjs/next-auth-example/blob/main/middleware.ts

Something like that: ```js
import { withAuth } from 'next-auth/middleware';

export default withAuth(
{
callbacks: {
authorized: ({ req, token }) => {
if (req.nextUrl.pathname === "/dashboard") {
return token?.role=== "ADMIN"
}
if (req.nextUrl.pathname === "/booking") {
return token?.role=== "USER"
}
return !!token;
},
},
}
);

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

solemn trail
onyx rain
solemn trail
#

In the condition?

#

Just console log to check if you are even getting the userRole passed correctly

#

Cause its gonna return false otherwise

onyx rain
# solemn trail Are you returning true?
import { withAuth } from 'next-auth/middleware';

export default withAuth(
    {
      callbacks: {
        authorized: ({ req, token }) => {
          if (req.nextUrl.pathname === "/dashboard" && token?.role !== "ADMIN") {
            console.log("req.nextUrl.pathname", req.nextUrl.pathname);
            console.log("token?.role", token?.role);
            //return to /booking if the role is not admin
            //return true will give access to /dashboard
            //return false will not give access to /dashboard nd send to callbackUrl "signin"
          }
          if (req.nextUrl.pathname === "/booking" && token?.role !== "USER") {
            console.log("req.nextUrl.pathname", req.nextUrl.pathname);
            console.log("token?.role", token?.role);
            //return to /dashboard if the role is not user
            //return true will give access to /booking
            //return false will not give access to /booking and send to callbackUrl "signin"
  
          }
          return !!token;
        },
      },
    }
  );
  
  export const config = {
    matcher: ["/dashboard/:path*", "/booking/:path*", "/settings/:path*"]
  };
``` I am getting the pathname and role in console: req.nextUrl.pathname /dashboard
token?.role USER
solemn trail
#

Wasnt it supposed to be token.userRole???

#

Thats why i am saying do console.log to check if you are receiving the token and its properties correctly

onyx rain
#

According to the documentation: "The middleware function will only be invoked if the authorized callback returns true."

#

#next-auth Any one who can help?