Next-auth: Using Prisma Schema with UUIDs

Created on 30 Jul 2020  路  6Comments  路  Source: nextauthjs/next-auth

When using the prisma adapter with the example project, a user can sign up using social providers. When they sign out and sign back in again the following error occurs.

[next-auth][error][session_error] TypeError: Cannot read property 'name' of null

To Reproduce
I've included a reproduction repo link below.

https://github.com/ThePaulMcBride/next-auth-bug

Set up the repo by adding the required secrets (I used github and google). I've included the prisma schema for generating the database. It is identical to the one in the docs with the exception of the id fields being uuids. You can generate the database using prisma migrate. Once the project is up and running, sign up. This works fine. Now sign out and log back in with the same provider. You get sent back to the home page signed out.

After inspecting the database you'll see what has happened. If you look at the sessions table, you'll notice that the value in user_id column actually reference the id field on the accounts table and not the users table. I have no idea why this happens.

Expected behavior
I should be able to sign back in with the same account I signed up with.

Additional context
This only seems to happen when using uuids for the model instead of incrementing numbers.

All 6 comments

The documentation states:

_"You can add properties to the schema and map them to any database colum names you wish, but you should not change the base properties or types defined in the example schema."_

Using UUID for the ID fields is not supported; that would need to be a feature request.

Ahh you're totally right. I keep running into issues where I've used the schema that was recommended on V3 beta. I should just wipe mind and start over. Thanks for pointing me in the right direction!

@ThePaulMcBride No worries! I hope we will get better models (relational, easier to use UUIDs with SQL...) soon! I think we need to get some testing and types stuff nailed down first, so we can make a change like that with more confidence!

It would be really nice to be able to use UUID for user ids. it's considered best practice, right? I'm in the learning stage with NextAuth + Postgres.

It would be really nice to be able to use UUID for user ids. it's considered best practice, right? I'm in the learning stage with NextAuth + Postgres.

For MongoDB we use ObjectID's but for SQL databases the included adapters use integers, which is still the most common case for SQL databases and, is how almost all OAuth Providers (Google, Facebook, Twitter, etc) represent User IDs with OAuth (though we treat them as strings, because this is not the case for all providers).

For compatibility with different user needs and feature parity, the plan is to support the option to enable UUID's or integers for all databases in both included adapters (TypeORM and Prisma) - e.g. just by setting a boolean.

I'd very much like this feature personally, but might take a little while to get round to it if we want to handle it well and ensure feature parity and to keep using it simple.

Sounds great! I'll keep an eye out and try to help here I can 馃槉

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alephart picture alephart  路  3Comments

ryanbahan picture ryanbahan  路  3Comments

ghoshnirmalya picture ghoshnirmalya  路  3Comments

ghoshnirmalya picture ghoshnirmalya  路  3Comments

Xetera picture Xetera  路  3Comments