aboutsummaryrefslogtreecommitdiff
path: root/controllers/tokenController.js
blob: cbcc550e1d7705b8eff671d7a04a97715250ef52 (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
const config = require('../config.js');
const db = require('knex')(config.database);
const randomstring = require('randomstring');
const utils = require('./utilsController.js');

const tokenController = {};

tokenController.verify = async (req, res, next) => {
	const token = req.body.token;
	if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' });

	const user = await db.table('users').where('token', token).first();
	if (!user) return res.status(401).json({ success: false, description: 'Invalid token' });
	return res.json({ success: true, username: user.username });
};

tokenController.list = async (req, res, next) => {
	const user = await utils.authorize(req, res);
	return res.json({ success: true, token: user.token });
};

tokenController.change = async (req, res, next) => {
	const user = await utils.authorize(req, res);
	const newtoken = randomstring.generate(64);

	await db.table('users').where('token', user.token).update({
		token: newtoken,
		timestamp: Math.floor(Date.now() / 1000)
	});

	res.json({ success: true, token: newtoken });
};

module.exports = tokenController;