aboutsummaryrefslogtreecommitdiff
path: root/controllers/authController.js
blob: eb7df097b316ad6a899635316eb26cce43612210 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const config = require('../config.js');
const db = require('knex')(config.database);
const bcrypt = require('bcrypt');
const randomstring = require('randomstring');
const utils = require('./utilsController.js');

let authController = {};

authController.verify = async (req, res, next) => {
	const username = req.body.username;
	const password = req.body.password;

	if (username === undefined) return res.json({ success: false, description: 'No username provided' });
	if (password === undefined) return res.json({ success: false, description: 'No password provided' });

	const user = await db.table('users').where('username', username).first();
	if (!user) return res.json({ success: false, description: 'Username doesn\'t exist' });
	if (user.enabled === false || user.enabled === 0) return res.json({
		success: false,
		description: 'This account has been disabled'
	});

	bcrypt.compare(password, user.password, (err, result) => {
		if (err) {
			console.log(err);
			return res.json({ success: false, description: 'There was an error' });
		}
		if (result === false) return res.json({ success: false, description: 'Wrong password' });
		return res.json({ success: true, token: user.token });
	});
};

authController.register = async (req, res, next) => {
	if (config.enableUserAccounts === false) {
		return res.json({ success: false, description: 'Register is disabled at the moment' });
	}

	const username = req.body.username;
	const password = req.body.password;

	if (username === undefined) return res.json({ success: false, description: 'No username provided' });
	if (password === undefined) return res.json({ success: false, description: 'No password provided' });

	if (username.length < 4 || username.length > 32) {
		return res.json({ success: false, description: 'Username must have 4-32 characters' });
	}
	if (password.length < 6 || password.length > 64) {
		return res.json({ success: false, description: 'Password must have 6-64 characters' });
	}

	const user = await db.table('users').where('username', username).first();
	if (user) return res.json({ success: false, description: 'Username already exists' });

	bcrypt.hash(password, 10, async (err, hash) => {
		if (err) {
			console.log(err);
			return res.json({ success: false, description: 'Error generating password hash (╯°□°)╯︵ ┻━┻' });
		}
		const token = randomstring.generate(64);
		await db.table('users').insert({
			username: username,
			password: hash,
			token: token,
			enabled: 1
		});
		return res.json({ success: true, token: token });
	});
};

authController.changePassword = async (req, res, next) => {
	const user = await utils.authorize(req, res);

	let password = req.body.password;
	if (password === undefined) return res.json({ success: false, description: 'No password provided' });

	if (password.length < 6 || password.length > 64) {
		return res.json({ success: false, description: 'Password must have 6-64 characters' });
	}

	bcrypt.hash(password, 10, async (err, hash) => {
		if (err) {
			console.log(err);
			return res.json({ success: false, description: 'Error generating password hash (╯°□°)╯︵ ┻━┻' });
		}

		await db.table('users').where('id', user.id).update({ password: hash });
		return res.json({ success: true });
	});
};

module.exports = authController;