<commit_before>/**
 * Backend of the login page
 */

const express = require('express');
const router = express.Router();
//const url = require('url');
const database = require('../log_management/database');
const logger = require('winston');
const jwt = require('jsonwebtoken');

const db = new database();

router.post('/authenticate', function (req, res, next) {
    logger.debug('Authenticating', req.body);
    db.authenticate(req.body.username, req.body.password)
        .then(data => {
            onSuccess(data, res);
        })
        .catch(error => {
            onFailure(error, res);
        });
});

router.post('/accounts', function (req, res, next) {
    logger.debug('Adding new user', req.body.username);
    db.addAccount(req.body.username, req.body.password)
        .then(data => {
            onSuccess(data, res);
        })
        .catch(error => {
            onFailure(error, res);
        });
});

onSuccess = (data, res) => {
    const token = getToken(data);
    logger.info('Issued token');
    res.status(200);
    res.json({
        success: true,
        message: 'Enjoy the token!',
        token: token
    });
};

onFailure = (error, res) => {
    console.log(error);
    //res.send(error);
    res.status(401);
    res.json({
        success: false,
        message: error
    });
}

getToken = (data) => {
    const token = jwt.sign({
            username: data[0].username,
            admin: data[0].admin
        },
        'secret',
        { expiresIn: 15 }
    );
    return token;
};<commit_msg>registration fixed<commit_after>/**
 * Backend of the login and registration pages
 */

const express = require('express');
const router = express.Router();
//const url = require('url');
const database = require('../log_management/database');
const logger = require('winston');
const jwt = require('jsonwebtoken');

const db = new database();

router.post('/authenticate', function (req, res, next) {
    logger.debug('Authenticating', req.body);
    db.authenticate(req.body.username, req.body.password)
        .then(data => {
            onSuccess(data, res);
        })
        .catch(error => {
            onFailure(error, res);
        });
});

router.post('/accounts', function (req, res, next) {
    logger.debug('Adding new user', req.body.username);
    db.addAccount(req.body.username, req.body.password)
        .then(data => {
            onAddSuccess(req.body.username, res);
        })
        .catch(error => {
            onFailure(error, res);
        });
});

onAddSuccess = (data, res) => {
    logger.verbose('Registration successful');
    const token = getToken(data, true); // change to false once not needed!
    logger.info('Issued token');
    res.status(200);
    res.json({
        success: true,
        message: 'Enjoy the token!',
        token: token
    });
};

onSuccess = (data, res) => {
    const token = getToken(data[0].username, data[0].admin);
    logger.info('Issued token');
    res.status(200);
    res.json({
        success: true,
        message: 'Enjoy the token!',
        token: token
    });
};

onFailure = (error, res) => {
    res.status(401);
    res.json({
        success: false,
        message: error
    });
}

getToken = (username, admin) => {
    const token = jwt.sign({
            username: username,
            admin: admin
        },
        'secret',
        { expiresIn: 15 }
    );
    return token;
};
{'fraction_non_alphanumeric': 0.1646680315881837, 'fraction_numerical': 0.006727113190991518, 'mean_word_length': 2.356231599607458, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6237262', 'n_tokens_mistral': 1058, 'n_tokens_neox': 1031, 'n_words': 236}