auth-server-helper/mock_server.js

63 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-03-06 12:06:10 +01:00
/* eslint-disable no-magic-numbers */
// @ts-nocheck
'use strict';
const express = require ('express');
const cookie_parser = require ('cookie-parser');
const auth = require ('./index');
const knex = require ('knex');
const path = require ('path');
2020-03-06 18:02:08 +01:00
const consts = require ('@scode/consts');
const crypto = require ('@scode/crypto-helper');
2020-03-06 12:06:10 +01:00
const fs = require ('fs-extra');
/**
* start the server
*/
async function start_server () {
const db_path = path.join (__dirname, 'db.sqlite');
if (await fs.exists (db_path))
await fs.unlink (db_path);
const db = knex ({
client: 'sqlite',
connection: { filename: db_path },
useNullAsDefault: true
});
await db.schema.createTable ('users', (table) => {
table.increments ('id');
table.string ('name');
table.string ('email');
table.string ('password');
table.string ('salt');
table.boolean ('deleted')
.default (false);
});
const app = express ();
const authentication = auth (db);
const salt = crypto.create_salt ();
const hash = crypto.hash_sha512 ('foo', salt);
await authentication.create_user ('test', 'asd@example.com', hash, salt);
app.use (cookie_parser ());
app.use (authentication.handler);
app.get ('/', (req, res) => {
2020-03-06 18:02:08 +01:00
res.status (consts.http_consts.status_ok)
2020-03-06 12:06:10 +01:00
.end ('foo');
});
app.listen (3000);
return app;
}
module.exports = { start_server };