0
passport.use("google",
  new GoogleStrategy(
    {
      clientID: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      callbackURL: "http://localhost:3000/auth/google/notes",
      userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo",
    },
    async (accessToken, refreshToken, profile, cb) => {
      try {
        // console.log("Google Profile:", profile);
        const result = await db.query("SELECT * FROM users WHERE email = $1", [profile.email]);

        if(result.rows.length === 0){
          //Inserting a new user to the users table
          const newUser = await db.query(
            "INSERT INTO users (email, password) VALUES ($1, $2)", [profile.email, "google"]
          );

          return cb(null, newUser.rows[0]);
        } else {
          return cb(null, result.rows[0]);
        }
      } catch (err) {
        console.error("Error in Google Strategy:", err);
        return cb(err);  
      }
    }
  )
);

In this code, I'm using Google OAuth to register users to my application, and I send the registration details to the users table. But at the same time, I want to send the email of the registered user to the notes table as well.

I tried using another "db.query()" right after the line where user gets registered to the users table. When I run the application, it keeps loading and nothing happens. But the user's data has been passed to the users table, but that email isn't sending to the notes table. How can I do that?

3
  • Please don't use images, and replace your image by plain text. Commented Dec 3, 2024 at 17:11
  • Without posting the actual code you are having a problem with nobody can help. I clicked on the link you provided and could only see a black rectangle with some tiny coloured dots in it on my screen. I recommend that you check the help on how to post code and edit your question. Commented Dec 3, 2024 at 17:13
  • I'm sorry for the confusion. I've updated the question. Could you please take a look? Commented Dec 3, 2024 at 17:19

1 Answer 1

0

After some debugging, I was able to find the solution to my own question that sending the user's email to both tables.

passport.use("google",
  new GoogleStrategy(
    {
      clientID: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      callbackURL: "http://localhost:3000/auth/google/notes",
      userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo",
    },
    async (accessToken, refreshToken, profile, cb) => {
      try {
        // console.log("Google Profile:", profile);
        const result = await db.query("SELECT * FROM users WHERE email = $1", [profile.email]);

        if(result.rows.length === 0){
          //Inserting a new user to the users table
          const newUser = await db.query(
            "INSERT INTO users (email, password) VALUES ($1, $2)", [profile.email, "google"]
          );

          // Inserting the user's email into the notes table
          await db.query(
            "INSERT INTO notes (email) VALUES ($1)", 
            [profile.email]
          );

          return cb(null, newUser.rows[0]);
        } else {
          return cb(null, result.rows[0]);
        }
      } catch (err) {
        console.error("Error in Google Strategy:", err);  
        return cb(err);  
      }
    }
  )
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.