You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm facing an issue with user authentication in a Next.js application using NextAuth. I have implemented a custom signIn callback for Google OAuth authentication, where I check if the user exists in the database and return it if found. However, I'm encountering unexpected behavior where the user object seems to be modified or overwritten.
Example in Database i have already have record name : ijk email : abc@gmail.com
when i use google login, name : abc email : abc@gmail.com
since the email already exists it should return {name: ikj, email: abc@gmail.com} instead it returns {name : abc, email : abc@gmail.com}, which is an unexpected behaviour. {name : abc, email : abc@gmail.com} this getting stored in authprovider and in backend returns the status code of 401
exportconstauthOptions: NextAuthOptions={providers: [CredentialsProvider({name: "crendentials",credentials: {},asyncauthorize(credentials,req): Promise<any>{const{ email, password }=credentialsas{email: string;password: string;};try{constuser=awaitUser.findOne({ email })if(!user){// Assuming you have access to the response object 'res'
return null}constcomparedpassword: boolean=awaitbcryptjs.compare(password,user.password)if(!comparedpassword){returnnull}returnuser;}catch(error: any){console.log("Error:",error)returnnull}}}),GoogleProvider({clientId: GOOGLE_CLIENT_ID!asstring,clientSecret: GOOGLE_CLIENT_SECRET!asstring})],session: {strategy: 'jwt'},callbacks: {asyncsignIn({ user, account }: {user: any;account: any}){if(account.provider==="google"&&user){try{const{ name, email }: {name: string; email: string}=user;constexistingUser=awaitUser.findOne({ email });if(existingUser){returnexistingUser;// Return existing user}constnewUser=newUser({name: name,email: email,isVerified: true});constsavedUser=awaitnewUser.save();returnsavedUser;// Return the newly created user}catch(error){console.error("Error in Google OAuth signIn callback:",error);returnnull;}}else{// not hiting the else blockconsole.log("whyisi'mgettingcalledhere")returnuser;}},asyncjwt({ token, user }){if(user){console.log(user,"this failed in every aspect of my life 0001")// i should existingUser object but i'm not getting ittoken.email=user.emailtoken.name=user.nametoken.id=user.id}if(!user)token.error="Invalid Credentials"token.errorStatus=401;// Set error status code}returntoken},asyncsession({session,token}: {session: any,token: any}){if(session.user){session.user.email=token.email;session.user.name=token.name;session.user.id=token.id;}returnsession}},
secret: process.env.NEXTAUTH_SECRET!,pages: {signIn: '/login'}}consthandler=NextAuth(authOptions)export{handlerasGET,handlerasPOST}`
How to reproduce
Currently i working in local repository
Expected behavior
Despite returning the existing user if found, the user object appears to be modified, causing inconsistencies in authentication. I've ensured that there are no explicit variable reassignments within the callback function.
I've tried debugging the issue, but I'm unable to identify the root cause. Could someone please help me understand why the user object is being modified unexpectedly and suggest how to resolve this issue?
Any insights or suggestions would be greatly appreciated. Thanks in advance!
The text was updated successfully, but these errors were encountered:
loki4514
added
bug
Something isn't working
triage
Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
labels
Apr 15, 2024
Environment
I'm facing an issue with user authentication in a Next.js application using NextAuth. I have implemented a custom signIn callback for Google OAuth authentication, where I check if the user exists in the database and return it if found. However, I'm encountering unexpected behavior where the user object seems to be modified or overwritten.
Example in Database i have already have record name : ijk email : abc@gmail.com
when i use google login, name : abc email : abc@gmail.com
since the email already exists it should return {name: ikj, email: abc@gmail.com} instead it returns {name : abc, email : abc@gmail.com}, which is an unexpected behaviour. {name : abc, email : abc@gmail.com} this getting stored in authprovider and in backend returns the status code of 401
async signIn is the main fucntion of google outh
Reproduction URL
https://github.com/loki4514/Blog-App
Describe the issue
How to reproduce
Currently i working in local repository
Expected behavior
Despite returning the existing user if found, the user object appears to be modified, causing inconsistencies in authentication. I've ensured that there are no explicit variable reassignments within the callback function.
I've tried debugging the issue, but I'm unable to identify the root cause. Could someone please help me understand why the user object is being modified unexpectedly and suggest how to resolve this issue?
Any insights or suggestions would be greatly appreciated. Thanks in advance!
here is stackoverflow question
Link
The text was updated successfully, but these errors were encountered: