Extend next-auth session with drizzle adapter
I followed the steps to extend the next-auth session object with a custom userRole property. I extended the Session and User interface, I have the userRole property in my Users DB table and I include the role in the session callback in authOptions. The only type error that I am getting is from the adapter (DrizzleAdapter). But when I check the session object the role property is missing.
I've searched everywhere but I cannot figure it out or find a solution. Does anyone have the same problem or know a fix?
51 Replies
I have never done this myself but reading the docs. Did you add the profile callback in the providers array? This is what the docs says for adding roles to users: The profile() callback's return value is used to create users in the database. That's it! Your newly created users will now have an assigned role.
If you also want to use the role on the client, you can expose it via the session callback.
The link if you want to read the rest btw https://authjs.dev/guides/basics/role-based-access-control
Role-based access control | Auth.js
There are two ways to add role-based access control (RBAC) to your application, based on the session strategy you choose. Let's see an example for each of these.
Actually now that I’m thinking. Did you set a default value for the role in your schema? Let’s say you create a new user in your db and you don’t set the role manually. Don’t have a default value set in the schema either and try to access the role through user.role in the session. The user.role will be empty and thus the role field will never be passed along right?
Or am I thinking wrong
Thanks for the feedback, and yes I have done all those things. I figured it out. It is actually a bug. The next-auth Drizzle Adapter only fetches the defaults from the db
I created an issue but sadly it keeps getting closed because of the reproduction 😦
GitHub
DrizzleAdapter overwrites custom tables with defaults · Issue #9296...
Adapter type @auth/drizzle-adapter Environment System: OS: Linux 5.15 Ubuntu 20.04.4 LTS (Focal Fossa) CPU: (32) x64 13th Gen Intel(R) Core(TM) i9-13900K Memory: 11.79 GB / 15.49 GB Container: Yes ...
+1 to this bug
Hello Steve Melons, did you find a solution to extend the User data that is inserted on create ?
what i found is that, it just doesnt work using database as session strategy. its work when you are using jwt as the session strategie, then additionnal fiels are stored with in the jwt token, and you retrieve if from there to add to your session
this works fine in my repo
GitHub
GitHub - GentikSolm/t3-app-dir: t3 app dir boilerplate
t3 app dir boilerplate. Contribute to GentikSolm/t3-app-dir development by creating an account on GitHub.
I'm about to massively overhaul that repo, bit it should show you how to extend it
it not really a bug as much as it is an adapter issue. they can't do much from their end because they have drizzle orm files inhouse for the user table
so their user data is actually different from yours in the drizzle adapter
this works with extra columns, it just won't grab them unless you explicitly override them which is what I did, and honestly it's not much worth at all and even gives you a better idea of what's going on under the hood
Thanks for you responses !
Yeah, due to research i also ended up making my own adapter, but i am struggling with the correct Typeing.
`
`
just look at the repo I linked and check out how I do it
why is this working out of the box with Prisma but not with drizzle? they need to fix this. I wasted 2 days finding solutions
this is not a paid product, it is open source. if it want something fixed that badly do it yourself. these people are working completely for free on these amazing projects. just because it doesn't exactly fit your needs gives you no right to be angry about it
I mean i did it also like you did but im using MySQL and i cant return on insert. So i do a select return but this could be null and the create callback dosent allow that:
1. you shouldnt be mixing .then with await
2. since we just inserted the user, we can be very sure the user exists in the db, so you can safely tell typescript that it will for sure not be null
I found the solution for my project, I hope it will be a solution for you too.
I found a solution to this a while ago by julius:
https://github.com/nextauthjs/next-auth/pull/8561
GitHub
feat(adapter-drizzle): add option to pass in schema by juliusmarmin...
☕️ Reasoning
In other adapters, the adapter returns the entire table so if you have additional fields they will also be returned when the adapter queries the table. A good example here is if your ...
basically overrides a function of the drizzle adaptor. Still need to augment the interface and add the field to the db, but now the adaptor will actually fetch it
You need to also make sure you augment the right module
https://github.com/nextauthjs/next-auth/pull/9462
GitHub
fix(docs): updated typescript module imports by Huijiro · Pull Requ...
☕️ Reasoning
As discussed in #8948, #9253, and #9377, some augmentation of types in the documentation is wrong, this pull request aims to solve those issues by updating the documentation.
I also ad...
hey @Steve Melons (or anyone on this thread) did you by any chance also came across the typeError gave by the drizzle-adapter? I just created a new t3-app and I'm getting this
Does it build when you @ts-ignore ?
I think I had the same issue
it does but the sign-in/up flows on my prod website and local are completely broken
I'm getting this error with credentials I've used before
I literally just updated dependencies and started getting a lot of errors from nextauth and the drizzle adapter
import type { Adapter } from "next-auth/adapters";
Needs a different import I think
I have no type Adapter imported here 😦
my whole file
You may need to augment the type and include the user id
@Liltripple_reid
According to this you should just cast it for now. https://github.com/nextauthjs/next-auth/issues/9493
GitHub
Types of property 'createUser' are incompatible · Issue #9493 · nex...
Environment System: OS: Windows 11 10.0.22000 CPU: (32) x64 AMD Ryzen 9 3950X 16-Core Processor Memory: 88.88 GB / 127.94 GB Binaries: Node: 20.6.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.19 ...
I’ll try that will update soon
Update: the ts errors are out, but the sign in errors are still there
Double check your env variables?
100 times
same error "Client not found"
is your repo public ?
wait
do discord's or google's client_secret keys expire?
no
huh
I'll deploy and test this out again
In an hour or so I mihgt have some time to do a test. You are uing the pirsma adapter and stock t3 with the @ latest ?
drizzle adapter
Ok sounds good. Can you send your package.json ?
you can check out the repo and deployed url here https://github.com/statusunknown418/chronosecrets
GitHub
GitHub - statusunknown418/chronosecrets: A (rather different?) new ...
A (rather different?) new social media to schedule and share encrypted messages - GitHub - statusunknown418/chronosecrets: A (rather different?) new social media to schedule and share encrypted mes...
was planning on doing a kinda fancy product launch but started getting this auth stuff
looks nice! Will clone when i get a chance and will get back to you
thanks man, lmk if you can find anything 🙏
I remeber having this issue then i moved to atuhjs5 beta and it was gone
well it could be a good upgrade, thing is that it's still on "beta"
Yeah the docs need love but it can run on the edge runtime since you are using Drizzle if you deploy on vercel which is a plus.
Well if I can’t find a solution I think I’ll just upgrade
Is it too much code to change?
Hey sorry I got really busy last night. I'm gonna take a look today and get back to you.
It is a good amount, those docs are ok though (the upgrading ones).
shesh
no worries but I think I found a fix
for some reason upgrading running
pnpm up
again solved the thing
found this relatedGitHub
Google provider not working in @auth/core 0.19.1 · Issue #9558 · ne...
Provider type Google Environment System: OS: Windows 11 10.0.22621 CPU: (12) x64 AMD Ryzen 5 3600 6-Core Processor Memory: 5.83 GB / 15.91 GB Binaries: Node: 18.18.2 - C:\Program Files\nodejs\node....
So is it all fixed now?
looks like so