/*
 * Copyright (C) Sapphirecode - All Rights Reserved
 * This file is part of Auth-Server-Helper which is released under MIT.
 * See file 'LICENSE' for full license details.
 * Created by Timo Hocker <timo@scode.ovh>, March 2020
 */

/* eslint-disable no-magic-numbers */
// @ts-nocheck
'use strict';

const express = require ('express');
const auth = require ('./index');
const consts = require ('@scode/consts');
const crypto = require ('@scode/crypto-helper');
const password_helper = require ('@scode/password-helper');


/**
 * start the server
 */
async function start_server () {
  const app = express ();

  const name = 'testuser';
  const salt = crypto.create_salt ();
  const password = await password_helper.hash (
    crypto.hash_sha512 ('foo', salt)
  );
  const user = { name, salt, password };

  app.use (auth ((user_name) => {
    if (user.name === user_name)
      return user;

    return null;
  }, [
    /noauthreg/u,
    { method: 'POST', regex: /noauthobj/u }
  ]));

  app.use ((req, res) => {
    res.status (consts.http.status_ok)
      .end ('foo');
  });

  app.listen (3000);

  return app;
}

module.exports = { start_server };