{"size": 1315, "ext": "js", "max_stars_repo_path": "routes/user/auth/google/index.js", "max_stars_repo_name": "bernatfogarasi/team-listener-server", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "routes/user/auth/google/index.js", "max_issues_repo_name": "bernatfogarasi/team-listener-server", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "routes/user/auth/google/index.js", "max_forks_repo_name": "bernatfogarasi/team-listener-server", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.8367346939, "max_line_length": 79, "alphanum_fraction": 0.6608365019}
require("dotenv").config();
const path = require("path");
const router = require("express").Router();
const { User } = require(path.resolve("models"));
const { validate } = require(path.resolve("middleware"));
const { OAuth2Client } = require("google-auth-library");

const clientId =
  "95200909886-co62p1sehhu134kdvoai53gomdkac4p6.apps.googleusercontent.com";

router.post("/", validate.authGoogle, async (request, response) => {
  const { tokenId } = request.body;

  const client = new OAuth2Client(clientId);
  const ticket = await client.verifyIdToken({
    idToken: tokenId,
    audience: process.env.CLIENT_ID,
  });
  const { email, email_verified, picture, given_name } = ticket.getPayload();

  const user = await User.findOne({ email });
  var userId;
  if (user) {
    userId = user._id;
  } else {
    const user = new User({
      username: given_name,
      email,
      emailConfirmed: email_verified,
      profilePicture: { url: picture },
    });

    var userSaved;
    try {
      userSaved = await user.save();
    } catch (error) {
      return response.status(400).send({ message: "cannot save user", error });
    }
    userId = userSaved._id;
  }

  request.session.isAuth = true;
  request.session.userId = userId;

  response.send({ message: "success" });
});

module.exports = router;
