Next.js Discord

Discord Forum

Help with custom user object

Unanswered
Spot-billed Duck posted this in #help-forum
Open in Discord
Spot-billed DuckOP
So there is my basic nextauth config, with a simple custom attribute "isStaff", stored in session
    interface Session extends DefaultSession {
        user: {
            id: string;

            // ...other properties
            // role: UserRole;
            isStaff: boolean;
        } & DefaultSession["user"];
    }

    interface User {
        isStaff: boolean
    }
}

/**
 * Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
 *
 * @see https://next-auth.js.org/configuration/options
 */
export const authOptions: NextAuthOptions = {
    callbacks: {
        session: ({session, user}) => ({
            ...session,
            user: {
                ...session.user,
                id: user.id,
                isStaff: user.isStaff
            },
        }),
    },


but when I get my user, I optain this:
{
  name: 'wailroth',
  email: 'w.......gmail.com',
  image: 'https://cdn.discordapp.com/avatars/1045051959138320606/d25ee0d686f456a67d6c923404bcdadc.png',
  id: '807d9272-9ac7-4daa-a9dc-72bbb54d0516',
  isStaff: undefined
}


But in my postgre, "isStaff" is defined

4 Replies

Spot-billed DuckOP
I tried by using a custom type:
import type {DefaultSession, DefaultUser} from "next-auth"

declare module "next-auth" {

    interface Session {
        user: {
            isStaff: boolean
            postal_code: string,
        } & DefaultSession["user"]
    }

    interface User extends DefaultUser {
        isStaff: boolean
        postal_code: string
    }

}
for example
and everything is undefined
Spot-billed DuckOP
there is my full config
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,
        }),

        /**
         * ...add more providers here.
         *
         * Most other providers require a bit more work than the Discord provider. For example, the
         * GitHub provider requires you to add the `refresh_token_expires_in` field to the Account
         * model. Refer to the NextAuth.js docs for the provider you want to use. Example:
         *
         * @see https://next-auth.js.org/providers/github
         */


    ],
};

 */
export const getServerAuthSession = () => getServerSession(authOptions);