Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AdapterUser Type Cuts Off Database Information #10958

Closed
YTG2G3 opened this issue May 19, 2024 · 4 comments
Closed

AdapterUser Type Cuts Off Database Information #10958

YTG2G3 opened this issue May 19, 2024 · 4 comments
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@YTG2G3
Copy link

YTG2G3 commented May 19, 2024

Environment

  System:
    OS: Windows 11 10.0.22631
    CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
    Memory: 3.07 GB / 15.91 GB
  Binaries:
    Node: 20.10.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.21 - C:\Program Files\nodejs\yarn.CMD
    npm: 10.2.3 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.1.0 - C:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Chromium (123.0.2420.97)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    @auth/drizzle-adapter: ^1.0.1 => 1.0.1
    next: 14.2.3 => 14.2.3
    next-auth: 5.0.0-beta.17 => 5.0.0-beta.17
    react: ^18 => 18.3.1

Reproduction URL

https://github.com/YTG2G3/nextauth-demo

Describe the issue

The BETA version of NextAuth uses
{ session: { user: AdapterUser } & AdapterSession /** Available when {@link AuthConfig.session} is set tostrategy: "database". */ user: AdapterUser }
as session callback parameter, and the AdapterUser is an interface that extends User:
export interface AdapterUser extends User { id: string email: string emailVerified: Date | null }

However, when a custom column that is not defined in type is added to the users table (I used drizzle):
export const users = pgTable('user', { id: text('id') .primaryKey() .$defaultFn(() => crypto.randomUUID()), name: text('name'), email: text('email').notNull(), emailVerified: timestamp('emailVerified', { mode: 'date' }), image: text('image'), admin: boolean('admin').default(false) // custom column });
the column is not reflected on the callback:
callbacks: { session({ session, user }) { return { ...session, user: { ...session.user, admin: user.admin // this always results in undefined } }; } }

Thus, Role Based Access Control does not work

How to reproduce

Explained above

Expected behavior

Current version: {
id: 'UUID-UUID-UUID-UUUUUUUID',
name: 'John Doe',
email: 'john@doe.com',
emailVerified: null,
image: null
}

Expected: {
id: 'UUID-UUID-UUID-UUUUUUUID',
name: 'John Doe',
email: 'john@doe.com',
emailVerified: null,
image: null,
admin: true
}

@YTG2G3 YTG2G3 added bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels May 19, 2024
@k-taro56
Copy link
Contributor

k-taro56 commented May 19, 2024

Hi,

This is not a bug.

You forgot to set the profile of the providers as described in the URL you provided.

import Google, { type GoogleProfile } from 'next-auth/providers/google';

    Google({
      profile<GoogleProfile>(profile) {
        return { admin: false, ...profile }
      }
    })
  ],

If you need further help, don't hesitate to ask.
Thank you!

@YTG2G3
Copy link
Author

YTG2G3 commented May 19, 2024

Forgot to include that part in replication of the bug, but tested that it does not work in original codebase.

providers: [
    Google({
      profile(profile) {
        return { admin: false, ...profile };
      }
    })
  ],
  callbacks: {
    async session({ session, user }) {
      console.log(user);
    }

Output of console.log remains the same as above.

@k-taro56
Copy link
Contributor

k-taro56 commented May 19, 2024

Oh, sorry.
But you will need to pass your own schemas when you add additional columns.

https://authjs.dev/getting-started/adapters/drizzle#passing-your-own-schemas

And you will probably need to extend the defineTables function.

@YTG2G3
Copy link
Author

YTG2G3 commented May 21, 2024

working solution. thank you for your help! I'll close the issue now

@YTG2G3 YTG2G3 closed this as completed May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

2 participants