adrianhajdin / project_next_14_ai_prompt_sharing

Next.js recently became the official React framework as outlined in React docs. In this course, you'll learn the most important Next.js concepts and how they fit into the React ecosystem. Finally, you'll put your skills to the test by building a modern full-stack Next 14 application.
https://www.jsmastery.pro/ultimate-next-course
2.9k stars 427 forks source link

Acces Denied Issues #76

Open tynoschuck opened 12 months ago

tynoschuck commented 12 months ago

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 Screenshot 2023-12-03 at 14 10 49

route.js Screenshot 2023-12-03 at 14 11 17

and database Screenshot 2023-12-03 at 14 14 54

Hope that someone would be able to help. Thank in advance!

aabu960 commented 11 months ago
  1. 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 }

  1. in your next.config.js copy the code and paste below // next.config.js const nextConfig = { images: { domains: ['lh3.googleusercontent.com'], }, };

module.exports = nextConfig;

tynoschuck commented 11 months ago

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.

Pannu786 commented 11 months ago

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.

shuuwolf commented 11 months ago

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

nimroddanielmaayan commented 11 months ago

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