How to modify user during create/link account?
See original GitHub issueYour question Hello! I am loving NextAuth for its simplicity to initially set up, but I’m also struggling with some customizations that don’t seem overly complicated. Currently, I have an app with Email, Google, and Twitter auths. I have no issues with users signing up using any of the 3, and even linking their Google and Twitter accounts after signing in with email.
However, I do not see any way to actually modify the built-in models data. For instance, I have created a firstName and lastName on the user model, and if I manually populate these fields on the DB, it’s returning the correct data. But I see no possible way to use the first and last name returned from Google, for instance, to update this data either on account creation or account link. Similar to image, if someone signs up with email, I see no way to update the image field from Google or Twitter when linking the account.
I see a ton of similar questions on here, but nothing definitively answered. I know of the callbacks and the events, however, I have concerns with using either one of those. For the callbacks, I have my GQL server on a different server due to a completely unrelated bug to NextAuth that prevents it from running in NextJS: https://github.com/vercel/next.js/discussions/12254
So, if I wanted to follow some of the other answers about updating the names and image in the signIn callback, I believe it would cause a large delay in actually signing in while it waits on the GQL mutation to update the data. If I wanted to use the events (createUser and linkAccount look like great options here), this looks like it would be non-blocking which would be a better solution, however unfortunately the profile data I need is not present there.
To me, the ideal would be if the model or repository was available either in the callback arguments or similar, so I can just update the model’s data directly using the existing connection to my DB (i.e. await userRepository.update(user.id, user);
). I don’t really want to bring in more of my database code to my NextJS project since I have to keep the majority of it separate anyway due to the above bug, and don’t want to duplicate this code more than absolutely necessary.
What are you trying to do Simply trying to fill out a user’s information using the auth services. If a user signs up with email, and then links google, their name, first name, last name and image should all be populated after linking Google.
Conversely, if a user signs up with Twitter, so we only have something like email and image, linking Google should then populate name, first name, and last name.
Regardless of the approach to this, I need to populate missing fields only when signing up/linking accounts, so we don’t populate something the user purposefully deleted either.
Reproduction I have my repo public here: https://github.com/bduff9/nfl-pool-fe/tree/develop
Specifically, my NextAuth config is here: https://github.com/bduff9/nfl-pool-fe/blob/develop/pages/api/auth/[...nextauth].ts
Feedback Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.
- Found the documentation helpful
- Found documentation but was incomplete
- Could not find relevant documentation
- Found the example project helpful
- Did not find the example project helpful
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:7
Just to update and close this, it looks like it is not possible to do what I needed with this library. Perhaps some future changes to oauth providers or the signIn callback could help, but as of current, there is no way I can see to add data to the user in signIn or access the existing db connection.
Just for completeness, to solve our requirements, I had to manually create a DB connection and use MySQL to run queries in signIn to handle my use case (populating data from the returned OAuth profile). This is still not ideal as I have to run multiple queries in signIn to get/verify data plus update data if needed. I also am creating another DB connection there so even though I am sure to close it in a finally block, I worry that under load it will stress the DB since the backend opens conns to the db, next-auth opens a conn to the DB, and now my custom code is also opening a conn to the DB. However, there was no other way I could see to do what we needed. Hope this helps someone else if they also are looking for something similar.
Thanks, I will take a look. I had not seen the profile callback before, so if anyone else finds this issue in the future, this is documented here: https://next-auth.js.org/configuration/providers#oauth-provider-options
For the record, all the data I need is in the signIn callback already, in the user and profile objects, there’s just no way to save it directly to the user object there (without calling something external). I’d prefer not to set up a full adapter as I have everything I need currently other than this one piece. So if the profile callback lets me return the shape of data to save, that might be the best place.
However, I will review the profile callback and the links you sent to see if I can find anything from there on a good way to accomplish this. Thanks again!