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

I solved error about Access Denied #58

Open YuyaHirano1994 opened 11 months ago

YuyaHirano1994 commented 11 months ago

I follow the #52 It works.

no change other files.

route.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 }) {
      // store the user id from MongoDB to session
      const sessionUser = await User.findOne({ email: session.user.email });
      session.user.id = sessionUser._id.toString();

      return session;
    },
    async signIn({ account, profile, user, credentials }) {
      try {
        await connectToDB();

        // check if user already exists
        const userExists = await User.findOne({ email: profile.email });

        // if not, create a new document and save user in MongoDB
        if (!userExists) {
          await User.create({
            email: profile.email,
            username: profile.name.replace(" ", "").toLowerCase(),
            image: profile.picture,
          });
        }

        return true;
      } catch (error) {
        console.log("Error checking if user exists: ", error.message);
        return false;
      }
    },
  },
});

export { handler as GET, handler as POST };
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

YuyaHirano1994 commented 10 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

When you deploy it, You should change auth URL on console.google(watch youtube) but If you still develop local, you do not change.

marcosriani commented 10 months ago

Thank you very much, this is really helpful. It worked for me.

saiutkarsh33 commented 10 months ago

Still didnt work for me :(

marcosriani commented 10 months ago

Hi there, you can check out my code here in the link below, try to clear your browser cookies as well, and make sure the .even file is in the root directory, and it should have the following keys with your credentials

GOOGLE_ID=yourcredentialshere GOOGLE_CLIENT_SECRET=yourcredentialshere NEXTAUTH_URL=http://localhost:3000 NEXTAUTH_URL_INTERNAL=http://localhost:3000 NEXTAUTH_SECRET=yourcredentialshere

Link to my project: https://github.com/marcosriani/tromptopia/tree/main

andrehugofernandes commented 10 months ago

when I try to authorize a domain under "localhost:3000" or http://localhost3000/,

(like js mastery does here https://www.youtube.com/watch?v=wm5gMKuwSYk @ 1:21:30)

I'm getting the same error but I can't continue.

Any idea to solve this one?

JoseVitorOliveira commented 9 months ago

Thank you