Creating Distinct OAuth Routes and Exploring Logic Through Python Games: A Day in My Coding Journey

Day 111 of #365DaysOfCode & Day 11 of #100DaysOfPython

So, today, my main task was focused on writing the backend code for the OAuth sign-up and login routes for two types of users. The important decision I made was to separate these two routes. In the old setup, the system automatically created an account for a user if they didn't already exist in my database. However, this approach felt like it lacked a certain amount of user consent.

The new backend code is set up as follows: for both types of users - 'freelancers' and 'clients', there are now two separate GoogleStrategy instances under Passport.js, one for login and one for sign-up. Each one does something slightly different.

In the login flow, I look up the user based on their Google profile ID. If I don't find a match, I send back a response with a message, "No account found. Please sign up."

In the sign-up flow, I still look up the user based on their Google profile ID. However, if I don't find a match, I create a new user and save it to the database, using the profile ID and email from their Google account.

For each user type, 'freelancer' or 'client', the system works the same way. The callback URLs are different to accommodate the different user types and actions (sign-up or login).

Now, about the Python part of the day - I spent my time building small games to practice loops and 'if' and 'else' statements. It's been a fun process since it's mostly about logic and you can make it as complicated as you want. The current project on my desk is creating a Hangman game. It's turning out to be an exciting task!

And that's about it for today's coding journey! It's been another day of new challenges and fun problem-solving.

// Google OAuth for login (freelancer)
passport.use(
  "google-freelancer-login",
  new GoogleStrategy(
    {
      clientID: process.env.CLIENT_ID,
      clientSecret: process.env.CLIENT_SECRET,
      callbackURL:
        "http://localhost:8001/auth/google/freelancer/login/callback",
      passReqToCallback: true,
    },
    async (req, accessToken, refreshToken, profile, done) => {
      try {
        const user = await User.findOne({ googleId: profile.id });

        if (!user) {
          return done(null, false, {
            message: "No account found. Please sign up.",
          });
        }

        done(null, user);
      } catch (error) {
        done(error, null);
      }
    }
  )
);

// Google OAuth for signup (freelancer)
passport.use(
  "google-freelancer-signup",
  new GoogleStrategy(
    {
      clientID: process.env.CLIENT_ID,
      clientSecret: process.env.CLIENT_SECRET,
      callbackURL:
        "http://localhost:8001/auth/google/freelancer/signup/callback",
      passReqToCallback: true,
    },
    async (req, accessToken, refreshToken, profile, done) => {
      try {
        let user = await User.findOne({ googleId: profile.id });

        if (!user) {
          user = new User({
            googleId: profile.id,
            email: profile.emails[0].value,
          });
          await user.save();
        }

        done(null, user);
      } catch (error) {
        done(error, null);
      }
    }
  )
);

// client login GoogleStrategy

passport.use(
  "google-client-login",
  new GoogleStrategy(
    {
      clientID: process.env.CLIENT_ID,
      clientSecret: process.env.CLIENT_SECRET,
      callbackURL: "http://localhost:8001/auth/google/client/login/callback",
      passReqToCallback: true,
    },
    async (req, accessToken, refreshToken, profile, done) => {
      try {
        const user = await Client.findOne({ googleId: profile.id });

        if (!user) {
          return done(null, false, {
            message: "No account found. Please sign up.",
          });
        }

        done(null, user);
      } catch (error) {
        done(error, null);
      }
    }
  )
);

// Google OAuth for signup (client)
passport.use(
  "google-client-signup",
  new GoogleStrategy(
    {
      clientID: process.env.CLIENT_ID,
      clientSecret: process.env.CLIENT_SECRET,
      callbackURL: "http://localhost:8001/auth/google/client/signup/callback",
      passReqToCallback: true,
    },
    async (req, accessToken, refreshToken, profile, done) => {
      try {
        let user = await Client.findOne({ googleId: profile.id });

        if (!user) {
          user = new Client({
            googleId: profile.id,
            email: profile.emails[0].value,
          });
          await user.save();
        }

        done(null, user);
      } catch (error) {
        done(error, null);
      }
    }
  )
);