<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const express = require('express');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const User = require('../models/User');
const secretToken = require('../config').secretToken;
const validate = require('jsonschema').validate;
const { log } = require('../log');
const { getConnection } = require('../db');

const router = express.Router();

/**
 * Authenticate users and return a JSON Web Token with their user ID.
 * @param {String} email
 * @param {String} Password
 */
router.post('/', async (req, res) => {
	const validParams = {
		type: 'object',
		maxProperties: 2,
		required: ['email', 'password'],
		properties: {
			email: {
				type: 'string',
				minLength: 3,
				maxLength: 254
			},
			password: {
				type: 'string',
				minLength: 3
			}
		}
	};

	if (!validate(req.body, validParams).valid) {
		res.sendStatus(400);
	} else {
		const conn = getConnection();
		try {
			const user = await User.getByEmail(req.body.email, conn);
			const isValid = await bcrypt.compare(req.body.password, user.passwordHash);
			if (isValid) {
				let users;
				if(user.role === User.role.ADMIN){
					users = await User.getAll(conn);
				}
				const token = jwt.sign({ data: user.id }, secretToken, { expiresIn: 86400 });
				res.json({ token: token, users: users, role: user.role });
			} else {
				throw new Error('Unauthorized password');
			}
		} catch (err) {
			if (err.message === 'Unauthorized password' || err.message === 'No data returned from the query.') {
				res.status(401).send({ text: 'Not authorized' });
			} else {
				log.error(`Unable to check user password for ${req.body.email}`, err);
				res.status(500).send({ text: 'Internal Server Error' });
			}
		}
	}
});
<commit_msg>Move role into jwt payload<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const express = require('express');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const User = require('../models/User');
const secretToken = require('../config').secretToken;
const validate = require('jsonschema').validate;
const { log } = require('../log');
const { getConnection } = require('../db');

const router = express.Router();

/**
 * Authenticate users and return a JSON Web Token with their user ID.
 * @param {String} email
 * @param {String} Password
 */
router.post('/', async (req, res) => {
	const validParams = {
		type: 'object',
		maxProperties: 2,
		required: ['email', 'password'],
		properties: {
			email: {
				type: 'string',
				minLength: 3,
				maxLength: 254
			},
			password: {
				type: 'string',
				minLength: 3
			}
		}
	};

	if (!validate(req.body, validParams).valid) {
		res.sendStatus(400);
	} else {
		const conn = getConnection();
		try {
			const user = await User.getByEmail(req.body.email, conn);
			const isValid = await bcrypt.compare(req.body.password, user.passwordHash);
			if (isValid) {
				const token = jwt.sign({ data: user.id, role: user.role }, secretToken, { expiresIn: 86400 });
				res.json({ token: token });
			} else {
				throw new Error('Unauthorized password');
			}
		} catch (err) {
			if (err.message === 'Unauthorized password' || err.message === 'No data returned from the query.') {
				res.status(401).send({ text: 'Not authorized' });
			} else {
				log.error(`Unable to check user password for ${req.body.email}`, err);
				res.status(500).send({ text: 'Internal Server Error' });
			}
		}
	}
});

{'fraction_non_alphanumeric': 0.1634590034273662, 'fraction_numerical': 0.012654890587925125, 'mean_word_length': 3.411627906976744, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2730817', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1192, 'n_words': 365}