{"size": 3452, "ext": "js", "max_stars_repo_path": "server.js", "max_stars_repo_name": "tru-ID/2fa-sim-swap-detection-vonage", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server.js", "max_issues_repo_name": "tru-ID/2fa-sim-swap-detection-vonage", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server.js", "max_forks_repo_name": "tru-ID/2fa-sim-swap-detection-vonage", "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": 23.8068965517, "max_line_length": 91, "alphanum_fraction": 0.6193511008}
require('dotenv').config();

const path = require('path');
const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser');
const app = express();
const Vonage = require('@vonage/server-sdk');
const { createAccessToken } = require('./helpers/createAccessToken');
const { performSimCheck } = require('./helpers/performSimCheck');
const VONAGE_API_KEY = process.env.VONAGE_API_KEY;
const VONAGE_API_SECRET = process.env.VONAGE_API_SECRET;
const VONAGE_BRAND_NAME = process.env.VONAGE_BRAND_NAME;

const vonage = new Vonage(
  {
    apiKey: VONAGE_API_KEY,
    apiSecret: VONAGE_API_SECRET,
  },
  {
    debug: true,
  }
);

let verifyRequestId = null;
let verifyRequestNumber = null;

app.use(express.static('public'));

app.use(
  session({
    secret: 'loadsofrandomstuff',
    resave: false,
    saveUninitialized: true,
  })
);

app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'pug');

// Add your routes here

app.get('/', (req, res) => {
  /*
    If there is a session for the user, the `index.html`
    page will display the number that was used to authenticate them.
    If not, it will prompt them to authenticate.
  */
  if (!req.session.user) {
    res.render('index', {
      brand: VONAGE_BRAND_NAME,
    });
  } else {
    res.render('index', {
      number: req.session.user.number,
      brand: VONAGE_BRAND_NAME,
    });
  }
});

app.get('/authenticate', (req, res) => {
  res.render('authenticate');
});

app.post('/verify', async (req, res) => {
  // Start the verification process
  verifyRequestNumber = req.body.number;
  //create access token
  const accessToken = await createAccessToken();
  // perform SIMCheck
  const { numberSupported, simChanged } = await performSimCheck(
    verifyRequestNumber,
    accessToken
  );
  console.log(verifyRequestNumber);
  if (simChanged === true) {
    return res.render('error', {
      error:
        'Verification Failed. SIM changed too recently. Please contact support.',
    });
  }
  if (numberSupported === false) {
    return res.render('error', {
      error:
        'Verification Failed. We do not support the phone number. Please contact support.',
    });
  }

  vonage.verify.request(
    {
      number: verifyRequestNumber,
      brand: VONAGE_BRAND_NAME,
    },
    (err, result) => {
      if (err) {
        console.error(err);
      } else {
        verifyRequestId = result.request_id;
        console.log(result);
        console.log(`request_id: ${verifyRequestId}`);
      }
    }
  );
  /* 
    Redirect to page where the user can 
    enter the code that they received
  */
  res.render('entercode');
});

app.post('/check-code', (req, res) => {
  // Check the code provided by the user
  vonage.verify.check(
    {
      request_id: verifyRequestId,
      code: req.body.code,
    },
    (err, result) => {
      if (err) {
        console.error(err);
      } else {
        if (result.status == 0) {
          /* User provided correct code,
             so create a session for that user
          */
          req.session.user = {
            number: verifyRequestNumber,
          };
        }
      }
      // Redirect to the home page
      res.redirect('/');
    }
  );
});

app.get('/cancel', (req, res) => {
  req.session.destroy();
  res.redirect('/');
});

const server = app.listen(3000, () => {
  console.log(`Server running on port ${server.address().port}`);
});
