project_next_14_ai_prompt_sharing
project_next_14_ai_prompt_sharing copied to clipboard
Can't Log-in with other accounts
I'm having an issue; it doesn't allow me to log in with different users, and it doesn't save them in the database with callbacks. When I use callbacks, it allows me to log in with different accounts but doesn't let me save new prompts.
- copy the code below and past in your [...nextauth] router.js
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 }
- in your next.config.js copy the code and paste below // next.config.js const nextConfig = { images: { domains: ['lh3.googleusercontent.com'], }, };
module.exports = nextConfig;