Open tynoschuck opened 12 months ago
import NextAuth from 'next-auth'; import GoogleProvider from 'next-auth/providers/google';
import User from '@models/user'; import { connectToDB } from '@utils/database';
const handler = NextAuth({ providers: [ GoogleProvider({ clientId: process.env.GOOGLE_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, }) ], callback: { async session({ session }) {
const sessionUser = await User.findOne({ email: session.user.email });
session.user.id = sessionUser._id.toString();
return session
},
async signIn({ user, account, profile, email, credentials }) {
try {
await connectToDB();
const userExists = await User.findOne({ email: profile?.email });
// if not, create a new document and save user in MongoDB
if (userExists === null) {
await User.create({
email: profile?.email,
username: profile?.name.replace(/\s+/g, '').toLowerCase(),
image: user.image
})
}
return true
} catch (error) {
console.log(error);
return false
}
}
} })
export { handler as GET, handler as POST }
module.exports = nextConfig;
Thanks for the reply! Unfortunately changing callbacks to callback does not solve the problem, I do not receive errors but it does nopt connect to the database as it should.
Thanks for the reply! Unfortunately changing callbacks to callback does not solve the problem, I do not receive errors but it does nopt connect to the database as it should.
same here stuck at this issue for ages now. I have tried everything but no luck so far.
I fixed it following others topic.... Changing the regex at user.js (/models), to allow less than 8 characters... and removing the brackets on MONGODB_URI when changing the password at .env file
This issue is also caused by any Google account that has non-latin letters in the user name! The solution is to completely disable limitations on the user name at at user.js (/models). This will allow duplicate user names, but I have no problem with that.
I've spent 2 hours on this before solving it... Thanks a lot for the help, shuuwolf
I fixed it following others topic.... Changing the regex at user.js (/models), to allow less than 8 characters... and removing the brackets on MONGODB_URI when changing the password at .env file
Hello there! Nothing new here, same issue as already discussed, I tried everything already mentioned in the other posts, and I came to a dead end. Here's my user.js
route.js
and database
Hope that someone would be able to help. Thank in advance!