Source: nodeServer/api/routes/biometricRoute.js

/**
 * @module Routes
 * @requires express
 */
const express = require('express');

const router = express.Router();
const mysql = require('mysql');

const pool = mysql.createPool({
  connectionLimit: 2,
  host: '167.172.165.119',
  user: 'simonlil',
  password: 'Lilleeng92',
  database: 'testTok',
  debug: false
});

const Bd = require('../dao/bioDao');

const bioDao = new Bd(pool);

/**
 * Retrieve the data for one user.
 * The server checks if the user is already in the table.
 * If it's not it inserts it.
 * @name Insert user to biometric table
 * @route {GET} /biometric/:expoToken
 * @routeparam {String} [tokenId] The users unique device expoToken.
 * @returns {json} biometric data of the user.
 */
router.get('/:tokenId', (req, res) => {
  const { tokenId } = req.params;
  let result;

  function sendData() {
    bioDao.getOne(tokenId, (status, data) => {
      res.status(status);
      res.json(data);
    });
  }

  async function callback(status, data) {
    result = await data[0].exist;
    if (result) {
      // Finnes!
      sendData();
    } else {
      // Finnes ikke!
      bioDao.insertToken(tokenId, sendData);
    }
  }

  console.log('Handling PUT request to /biometric/', tokenId);
  bioDao.checkExist(tokenId, callback);
});

/**
 * Stores the users answer if he wants to use biometric login in the future.
 * @name Set biometric settings for one user.
 * @route {PUT} /biometric/
 * @bodyparam {boolean} [option] If the user wants to use biometric login in the future.
 * @bodyparam {String} [tokenId] The users unique device expoToken.
 *
 */
router.put('/', (req, res) => {
  console.log('Handling PUT request to /biometric');
  bioDao.setBiometric(req.body, (status, data) => {
    res.status(status);
    res.json(data);
  });
});

/**
 * Sets the boolean value for the user after he gets the prompt message
 * regarding whether or not to use biometric login.
 * @name Set the users answer on the prompMessage on first login.
 * @route {PUT} /biometric/prompt
 * @bodyparam {boolean} [option] Whether or not the user has read the first prompt message.
 * @bodyparam {String} [tokenId] The users unique device expoToken
 */
router.put('/prompt', (req, res) => {
  console.log('Handling PUT request to /biometric/prompt');
  bioDao.setFirstPrompt(req.body, (status, data) => {
    res.status(status);
    res.json(data);
  });
});

module.exports = router;