{"size": 1848, "ext": "js", "max_stars_repo_path": "routes/authRouter.js", "max_stars_repo_name": "BloomTech-Labs/signlingo-be", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "routes/authRouter.js", "max_issues_repo_name": "BloomTech-Labs/signlingo-be", "max_issues_repo_issues_event_min_datetime": "2020-05-04T20:40:26.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-02T13:00:46.000Z", "max_forks_repo_path": "routes/authRouter.js", "max_forks_repo_name": "BloomTech-Labs/signlingo-be", "max_forks_repo_forks_event_min_datetime": "2020-05-07T21:47:01.000Z", "max_forks_repo_forks_event_max_datetime": "2021-01-15T02:42:15.000Z"}
{"max_stars_count": null, "max_issues_count": 6, "max_forks_count": 6, "avg_line_length": 33.0, "max_line_length": 102, "alphanum_fraction": 0.5730519481}
const router = require("express").Router();
const OktaJwtVerifier = require("@okta/jwt-verifier");
const User = require("../models/authModel");
const ISSUER = process.env.ISSUER;

// register and login end point --> receives okta accessToken, parses it, checks if user is already in
// database, if not, adds user to user table and adds levels to user account in user levels table
// if token is bad, end point will not find okta uid. You will need to make sure to have an env var
// that represents your issuer process.env.ISSUER

const oktaJwtVerifier = new OktaJwtVerifier({
  issuer: ISSUER,
});

router.post("/signup", (req, res) => {
  oktaJwtVerifier
    .verifyAccessToken(req.body.accessToken.accessToken, "api://default")
    .then((decodedToken) => {
      User.findByOktaUID(decodedToken.claims.uid)
        .then((foundit) => {
          if (foundit.length === 0) {
            User.addUser(decodedToken.claims.uid).then((addedUser) => {
              res.status(201).json({
                newUser: addedUser,
                addedThem: "We added a user!",
                okta_uid: decodedToken.claims.uid,
              });
            });
          } else {
            res.status(200).json({
              foundUser: true,
              userFound: "Found the user already in users table",
              foundit,
              okta_uid: decodedToken.claims.uid,
            });
          }
        })
        .catch((err) => {
          res.status(500).json({
            error: err.message,
            error2: "could not find okta uid",
            uid: decodedToken.claims.uid,
          });
        });
    })
    .catch((err) => {
      // a validation failed, inspect the error
      res.status(500).json({
        message: `Json verify didn't work`,
        err: err.message,
      });
    });
});

module.exports = router;
