What is the correct way to use next-auth with Google OAuth and users stored in the database? I'm new to this, confused, and need some help. #2634
-
Hi! I'm new to I have added the Google OAuth provider, and now when I run When the google authentication is complete, I need to be able to create a new user in my database, or retrieve the existing one if they have already signed up before (because I need to store all kinds of custom information about the user, not just their email). And I want to make user's information available on the When I was using a regular Express server and Passport, the code looked kinda like this: const googleAuth = new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "/api/v1/profiles/google/callback",
},
async (req, accessToken, refreshToken, profile, done) => {
const existingProfile = await Profile.findOne({ email: profile.emails[0].value })
/* Found user, return him and move along. */
if (existingProfile) return done(null, existingProfile)
/* Haven't found profile with this googleId, create a new one. */
const newProfile = await new Profile({
googleId: profile.id,
email: profile.emails[0].value,
})
newProfile.save()
done(null, newProfile)
}
) So I would still be creating the users in my database, and retrieving their information on log in, so that I could send it to the client. Where does this kind of code supposed to go when I'm using the serverless And a second, but kind of related question - what's that default user object that gets provided to me in the (I've done my best to look through the tutorials and examples, but couldn't find one that explains this clearly.) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Without defining an adapter, Check out one of our database adapters https://next-auth.js.org/adapters/overview or create your own! To control what gets sent to the client, check the |
Beta Was this translation helpful? Give feedback.
Without defining an adapter,
next-auth
defaults to store user data in a JWT in a session cookie.Check out one of our database adapters https://next-auth.js.org/adapters/overview or create your own!
To control what gets sent to the client, check the
session
callback: https://next-auth.js.org/configuration/callbacks#session-callback