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.58k stars 396 forks source link

ACCESS DENIED ISSUE SOLVED in route.js, You can now sign in with Google and see result in MongoDB #60

Open frankoadeleye opened 11 months ago

frankoadeleye commented 11 months ago

Replace what you have in your route.js file in app/api/auth/[...nextauth] with the code below:

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,
    })
  ],
  callbacks: {
    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 }

The problem was the number of params in signIn callback. Please write in the order above and also use conditional ? as used in the try block of signIn asyn function above.

Let me know if this works for you too!

Cheers

frankoadeleye commented 11 months ago

A few other things to try:

Go to and under Security select Network Acess and click on edit under the current IP Address of your computer.

Then in the modal that pops up, select 'Update IP' Address. If you cannot find this button, then your IP Address is updated already.

Another thing you should do is this: (https://github.com/adrianhajdin/project_next_13_ai_prompt_sharing/issues/52#issue-1819330411)

AbdullahTayeh commented 11 months ago

hi, if I want to host it what shall I include in the google.cloud.clonsole, and do I need to change anything in my .env file

saiutkarsh33 commented 10 months ago

None of these work for me! Worked a while back but it does not work anymore!

santanaruben commented 10 months ago

In my case, the problems were some typos and also the restriction for the length of the "username". I made it work by changing from 8-20 to 6-20 in the match property of the username configuration object in the user.js file.

honxsinatra commented 8 months ago

for my side i just use "callback" instead of "callbacks" errorshot

honxsinatra commented 8 months ago

it works for me

karabirkan commented 6 months ago

The other thing you can do is check your apps MONGODB_URI when you type in your password remember to delete brackets for example : MONGODB_URI="mongodb+srv://birkan:@... MONGODB_URI="mongodb+srv://birkan:birkan@...