Is there a way to run some code just after user is created?
If yes then when and where? Is there a way to "listen" when new user or some new entry in table is created?
15 Replies
Depends on how you are creating users. Can you tell us more about your setup?
as a general question - can I somehow listen and react when row is inserted in a table?
as more specific question - I'm using convex auth. When user signs in with i.e github and new user is created in users table, I want to insert some rows in some tables (this is only for new users)
general question: There are no triggers, you can call helper functions from wherever you are performing the db write.
convex auth: You can customize user creation: https://labs.convex.dev/auth/advanced#controlling-user-creation-and-account-linking-behavior
Advanced: Details - Convex Auth
Authentication library for your Convex backend
I don't think I want to op-in to this 😬
When you provide this callback, the library doesn't create or update users at all. It is up to you to implement all the necessary logic for all providers you use.
Do you need account linking?
no, I need to seed some tables after user is created
I mean, if you don't need account linking at all (because you don't have 2 authentication methods that should allow the same user to log in, see the linked doc), then implementing
createOrUpdateUser
should be straightforward.but as I understand I should do all the insertions in auth tables myself?
ok so how can I do a row creation in another table inside that
createOrUpdateUser
function without messing with auth tables?Not all auth tables, just the
users
table. createOrUpdateUser
receives ctx
as an arg, so you can insert whatever you want from that function in other tables.
As long as you return the id of the user you should be fine.
Convex auth still handles updating all of the other auth tables, implementing this function doesn't change that.I want to keep the initial implementation that convex/auth is using for this:
can you explain me this better? I dont understand what this do
That's copy pasted from the doc Michal linked earlier
I'd have to look at how convex auth is getting
existingUserId
, but it would mean that you don't need to link anything because the existing user has already been identified. If there are other fields in the args that have changed, like eg., the user image from a GitHub login, you'll want to update that field on the users table yourself there.
It is a bit rough to have to opt out of the default handling just to make changes in another table on user creation. @Michal Srb an event hook here would be really nice.
I'm currently avoiding all of this by driving post user creation changes from the client, which should work fine. But it'd be nice to have it done transactionally on the backend.I have an open PR but will discuss it more internally to see if we can improve this area altogether. Will get back to you tomorrow.
https://github.com/get-convex/convex-auth/pull/48
GitHub
Callback for default user creation by xixixao · Pull Request #48 · ...
I think this is good to add for now. My hope is that we'll find the time to implement a type-safe version of the library, and in that one we wouldn't need either of these callbacks....
I'm surprised how quickly you are cooking features
Published in 0.0.48: https://labs.convex.dev/auth/advanced#writing-additional-data-during-authentication
Advanced: Details - Convex Auth
Authentication library for your Convex backend
I'll use for sure, thanks for this! The composable approach mentioned in the PR looks ideal too, looking forward to it.