Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

57
Views
How do I send oauth token and user information from server back to client?

I am trying to implement google oauth in my app. I have a link to a google login page, which on successful completion of the form redirects to my server express route(see below). The controller for this route needs to do ONE of the following:

  • Send the generated token and user information back to the front end so it can be saved in session local storage -> follow up below

  • somehow save this information to the session and then redirect the user to home page

Route controller:

export const googleAuth = async (req, res) => {
  const googleUser = await getGoogleUser({ code: req.query.code });
  let user = await User.findOne({ email: String(googleUser.email) });

  if (!user) {
    user = new User();
  }

  try {
    const token = jwt.sign({ _id: user._id }, process.env.JWT_SECRET, {
      expiresIn: "1d",
    });
    user.password = undefined;
    res.json({ token, user });
  } catch (err) {
    console.log(err);
    return res.status(400).send("Error. Try again.");
  }
};

if the solution is to send the data from backend -> client, how do i receive the data? In my regular authentication method i have a handleSubmit function, but the client has no form submit for google oauth(it's just a link to the google form)

Thanks in advance and sorry if this is a duplicate, I couldn't find a question that answered all these questions together.

7 months ago ยท Juan Pablo Isaza
Answer question
Find remote jobs