Source: mHelpDesk/authentication.js

/**
 * Authentication
 * @module mHelpDesk/authentication
 */

const mysql = require('promise-mysql')

const config = require('../config')

exports.getPromiseOfAccessToken = () => {
  let connection
  return mysql.createConnection(config.database)
    .then(connection_ => {
      connection = connection_
      const sql = `
        SELECT access_token_str
        FROM projectchill2_refresh_token
        WHERE token_status = 1
        ORDER BY token_id DESC
        LIMIT 1;
      `
      return connection.query(sql)
    })
    .then(rows => {
      connection.end()
      if (!rows[0] || !rows[0].access_token_str) {
        throw new Error('No valid stored access token.')
      }
      return rows[0].access_token_str
    })
}