2017-10-04 02:13:38 +02:00
|
|
|
const config = require('../config.js');
|
|
|
|
const db = require('knex')(config.database);
|
|
|
|
const randomstring = require('randomstring');
|
|
|
|
const utils = require('./utilsController.js');
|
2017-01-17 20:54:25 +01:00
|
|
|
|
2017-10-04 02:13:38 +02:00
|
|
|
const tokenController = {};
|
2017-01-17 20:54:25 +01:00
|
|
|
|
2017-10-04 02:13:38 +02:00
|
|
|
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' });
|
2017-01-17 20:54:25 +01:00
|
|
|
|
2017-10-04 02:13:38 +02:00
|
|
|
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 });
|
|
|
|
};
|
2017-01-17 20:54:25 +01:00
|
|
|
|
2017-10-04 02:13:38 +02:00
|
|
|
tokenController.list = async (req, res, next) => {
|
|
|
|
const user = await utils.authorize(req, res);
|
|
|
|
return res.json({ success: true, token: user.token });
|
|
|
|
};
|
2017-01-17 20:54:25 +01:00
|
|
|
|
2017-10-04 02:13:38 +02:00
|
|
|
tokenController.change = async (req, res, next) => {
|
|
|
|
const user = await utils.authorize(req, res);
|
|
|
|
const newtoken = randomstring.generate(64);
|
2017-01-18 08:51:42 +01:00
|
|
|
|
2017-10-04 02:13:38 +02:00
|
|
|
await db.table('users').where('token', user.token).update({
|
2017-01-30 02:06:52 +01:00
|
|
|
token: newtoken,
|
2017-10-04 02:13:38 +02:00
|
|
|
timestamp: Math.floor(Date.now() / 1000)
|
|
|
|
});
|
|
|
|
|
|
|
|
res.json({ success: true, token: newtoken });
|
|
|
|
};
|
2017-01-18 08:51:42 +01:00
|
|
|
|
2017-10-04 02:13:38 +02:00
|
|
|
module.exports = tokenController;
|