Next.js Discord

Discord Forum

Retrieve custom data from User table using NextAuth

Unanswered
Giant Angora posted this in #help-forum
Open in Discord
Giant AngoraOP
Hello, I have an app built with NextAuth, DrizzleORM and Postgres

I added a field called credits to my user table, here is the full user table:

export const users = createTable('user', {
    id: varchar('id', { length: 255 }).notNull().primaryKey(),
    name: varchar('name', { length: 255 }),
    email: varchar('email', { length: 255 }).notNull(),
    emailVerified: timestamp('emailVerified', {
        mode: 'date',
    }).default(sql`CURRENT_TIMESTAMP`),
    image: varchar('image', { length: 255 }),
    credits: integer('credits').default(0),
});


I am trying to attach this creditsfield to the session user in NextAuth, this is what I did:

import { DrizzleAdapter } from '@auth/drizzle-adapter';
import {
    getServerSession,
    type DefaultUser,
    type DefaultSession,
    type NextAuthOptions,
} from 'next-auth';
import { type Adapter } from 'next-auth/adapters';
import GithubProvider from 'next-auth/providers/github';
import GoogleProvider from 'next-auth/providers/google';

import { env } from '@/env';
import { db } from '@/server/db';
import { createTable } from '@/server/db/schema';

declare module 'next-auth' {
    interface Session extends DefaultSession {
        user: {
            id: string;
            credits: number;
        } & DefaultSession['user'];
    }

    interface User extends DefaultUser {
        credits: number;
    }
}

export const authOptions: NextAuthOptions = {
    callbacks: {
        session: ({ session, user }) => {
            session.user.id = user.id;
            session.user.name = user.name;
            session.user.email = user.email;
            session.user.credits = user.credits;

            return session;
        },
    },
    
    // rest of the code
};

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


For some reason I am still not getting this creditsfield in my session, any ideas? Thanks in advance!

6 Replies

@Giant Angora Hello, I have an app built with NextAuth, DrizzleORM and Postgres I added a field called `credits` to my `user` table, here is the full user table: export const users = createTable('user', { id: varchar('id', { length: 255 }).notNull().primaryKey(), name: varchar('name', { length: 255 }), email: varchar('email', { length: 255 }).notNull(), emailVerified: timestamp('emailVerified', { mode: 'date', }).default(sql`CURRENT_TIMESTAMP`), image: varchar('image', { length: 255 }), credits: integer('credits').default(0), }); I am trying to attach this `credits`field to the session user in NextAuth, this is what I did: import { DrizzleAdapter } from '@auth/drizzle-adapter'; import { getServerSession, type DefaultUser, type DefaultSession, type NextAuthOptions, } from 'next-auth'; import { type Adapter } from 'next-auth/adapters'; import GithubProvider from 'next-auth/providers/github'; import GoogleProvider from 'next-auth/providers/google'; import { env } from '@/env'; import { db } from '@/server/db'; import { createTable } from '@/server/db/schema'; declare module 'next-auth' { interface Session extends DefaultSession { user: { id: string; credits: number; } & DefaultSession['user']; } interface User extends DefaultUser { credits: number; } } export const authOptions: NextAuthOptions = { callbacks: { session: ({ session, user }) => { session.user.id = user.id; session.user.name = user.name; session.user.email = user.email; session.user.credits = user.credits; return session; }, }, // rest of the code }; export const getServerAuthSession = () => getServerSession(authOptions); For some reason I am still not getting this `credits`field in my session, any ideas? Thanks in advance!
Please do the following steps to add it typesafe to your session:
1. Define a /types/next-auth.d.ts file in your root folder.
2. Paste the following code inside: https://paste.gg/p/B33fb0n3/829af5ac6ca4437f8672ef0e7161478a
3. Success: You can see that you will get all the defined values typesafe (see attached). In my case it was for groups
Giant AngoraOP
This is what I have in the auth.ts file, I am using t3 stack and they added it in there, I supposed it does the same as creating this new file? Or am I wrong?
@Giant Angora This is what I have in the `auth.ts` file, I am using t3 stack and they added it in there, I supposed it does the same as creating this new file? Or am I wrong?
hm they explicity tell, that we need to create a new file:
To extend/augment this type, create a types/next-auth.d.ts file in your project:
Giant AngoraOP
Already did and still not working
@Giant Angora Already did and still not working
Blue orchard bee
What's your setup within Next-Auth?
Giant AngoraOP
I am using T3 stack from https://create.t3.gg/