#Retrieve custom data from User table using NextAuth

7 messages · Page 1 of 1 (latest)

solar cape
#

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:

    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 creditsfield in my session, any ideas? Thanks in advance!

oak adderBOT
#

🔎 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)

empty peak
solar cape
#

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?

empty peak
solar cape
#

Already did and still not working

dapper oriole