Setting an `apiKey` additional field on users
I'm trying to add an
apiKey
field to my users, like so:
But I'm not sure how to generate an unique key for each user on signup. How can I do this?Solution:
Jump to solution
I'm going assume you're using the apiKey plugin.
But I'm not sure how to generate an unique key for each user on signup. How can I do this?Use database hooks: https://www.better-auth.com/docs/concepts/database#2-after-hook...

5 Replies
Solution
I'm going assume you're using the apiKey plugin.
But I'm not sure how to generate an unique key for each user on signup. How can I do this?Use database hooks: https://www.better-auth.com/docs/concepts/database#2-after-hook Use an
after
hook on user
create
. ^^
Then create an api key.
Here's an example:
got it, thank you!
One more question: I'm using Drizzle too and I noticed that I have to make the API Key column nullable to allow creating users. It works, but how do you deal with TypeScript complaining that
apiKey
could be undefined even though it definitely won't?//@ts-ignore
Not sure you can do much about this.
Cool, thanks again