NextAuth custom properties for session

Hi guys, there is my nextauth config using drizzle
import {DrizzleAdapter} from "@auth/drizzle-adapter";
import {
DefaultSession,
getServerSession,
type NextAuthOptions,
} from "next-auth";
import DiscordProvider from "next-auth/providers/discord";

import {env} from "@/env.mjs";
import {db} from "@/server/db";
import {pgTable, sessions} from "@/server/db/schema";

/**
* Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
*
* @see https://next-auth.js.org/configuration/options
*/

declare module "next-auth" {
interface Session extends DefaultSession {
user: {
id: string;
isStaff: boolean
} & DefaultSession["user"];
}

interface User {
// ...other properties
isStaff: boolean
}
}

export const authOptions: NextAuthOptions = {
callbacks: {
session: ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
isStaff: user.isStaff
},
}),
},
pages: {
//signOut: '/api/auth/signout',
newUser: "/api/auth/register"

},
adapter: DrizzleAdapter(db, pgTable),
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),

*/
export const getServerAuthSession = () => getServerSession(authOptions);
import {DrizzleAdapter} from "@auth/drizzle-adapter";
import {
DefaultSession,
getServerSession,
type NextAuthOptions,
} from "next-auth";
import DiscordProvider from "next-auth/providers/discord";

import {env} from "@/env.mjs";
import {db} from "@/server/db";
import {pgTable, sessions} from "@/server/db/schema";

/**
* Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
*
* @see https://next-auth.js.org/configuration/options
*/

declare module "next-auth" {
interface Session extends DefaultSession {
user: {
id: string;
isStaff: boolean
} & DefaultSession["user"];
}

interface User {
// ...other properties
isStaff: boolean
}
}

export const authOptions: NextAuthOptions = {
callbacks: {
session: ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
isStaff: user.isStaff
},
}),
},
pages: {
//signOut: '/api/auth/signout',
newUser: "/api/auth/register"

},
adapter: DrizzleAdapter(db, pgTable),
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),

*/
export const getServerAuthSession = () => getServerSession(authOptions);
as you can see, I have normally custom properties like isStaff. But, when I log my session, isStaff or any other propertie is always null
1 Reply
Shoodey
Shoodey14mo ago
a quick console.log of user inside your session callback will show you it is actually null. You need to defined where that property is defined and set it from there, wither a database or an api
Want results from more Discord servers?
Add your server