I’m trying to create a menu. If the user role is “1000”, i want to add “AddNewUser” item to menu, otherwise this section shouldn’t be appear. I can get session on my dashboard.jsx so normally session is correct, but i can’t get it in my api. How can i fix that?
Here is my session information coming from dashboard:

Here is terminal:
nextauth:
import NextAuth from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
import sql from "mssql";
import db from "../../../../../lib/db";
import bcrypt from 'bcryptjs';
const handler = NextAuth({
secret: process.env.NEXTAUTH_SECRET,
pages: {
signIn: '/auth/login-1',
},
providers: [
CredentialsProvider({
name: 'Credentials',
credentials: {
email: { label: 'Email', type: 'email', placeholder: '[email protected]' },
password: { label: 'Password', type: 'password' },
},
async authorize(credentials) {
try {
const pool = await sql.connect(db);
const sqlRequest = pool.request();
const userQuery = `
SELECT id, first_name, last_name, email, password, role_id, location_id FROM [inventory].[user] WHERE email = @Email
`;
sqlRequest.input('Email', sql.VarChar, credentials.email);
const result = await sqlRequest.query(userQuery);
const user = result.recordset[0];
if (user) {
const isValidPassword = bcrypt.compareSync(credentials.password, user.password);
if (isValidPassword) {
console.log("User Role Assigned:", user.role_id);
return {
id: user.id,
name: `${user.first_name} ${user.last_name}`,
email: user.email,
role: user.role_id,
location: user.location_id
};
}
}
return null;
} catch (error) {
console.error('An error occurred during authorization:', error);
return null;
}
},
}),
],
callbacks: {
async jwt({ token, user }) {
if (user) {
token.role = user.role;
token.location = user.location;
}
return token;
},
async session({ session, token }) {
if (token) {
session.user.role = token.role;
session.user.location = token.location;
}
return session;
},
},
});
export { handler, handler as GET, handler as POST };
api:
import { NextResponse } from "next/server";
import { getMenus } from "./menu-items";
import { getServerSession } from "next-auth/next"
export async function GET(req, { params }) {
const session = await getServerSession({ req });
if (!session || !session.user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const user = session.user;
console.log("role:", user.role);
const menus = await getMenus(params?.locale ?? "en-US", user.role);
return NextResponse.json(menus, { status: 200 });
}
menu-item:
import { getDictionary } from "@app/[lang]/dictionaries";
export async function getMenus(locale, userRole) {
const dictionary = await getDictionary(locale);
const { sidebar } = dictionary;
const baseMenu = [
{
path: `/${locale}/dashboards/dashboard`,
label: sidebar.menuItem.dashboard,
icon: "dashboard",
},
{
path: `/${locale}/dashboards/misc`,
label: sidebar.menuItem.addNewMachine,
icon: "editor",
},
{
path: `/${locale}/dashboards/addThirdPartyData`,
label: sidebar.menuItem.addData,
collapsible: true,
icon: "editor",
},
{
path: `/${locale}/dashboards/listing`,
label: sidebar.menuItem.machineList,
icon: "listing",
},
{
path: `/${locale}/modules/maps/clustering`,
label: sidebar.menu.maps,
icon: "map",
},
];
console.log(userRole);
if (userRole === 1000) {
console.log("Role check passed, adding addNewUser to menu");
baseMenu.push({
path: `/${locale}/dashboards/addNewUser`,
label: sidebar.menuItem.addNewUser,
icon: "user",
});
} else {
console.log("Role check failed, addNewUser not added to menu");
}
return [
{
label: sidebar.menu.home,
children: baseMenu,
},
];
}





