tokenController.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. const config = require('../config.js');
  2. const db = require('knex')(config.database);
  3. const randomstring = require('randomstring');
  4. const utils = require('./utilsController.js');
  5. const tokenController = {};
  6. tokenController.verify = async (req, res, next) => {
  7. const token = req.body.token;
  8. if (token === undefined) return res.status(401).json({ success: false, description: 'No token provided' });
  9. const user = await db.table('users').where('token', token).first();
  10. if (!user) return res.status(401).json({ success: false, description: 'Invalid token' });
  11. return res.json({ success: true, username: user.username });
  12. };
  13. tokenController.list = async (req, res, next) => {
  14. const user = await utils.authorize(req, res);
  15. return res.json({ success: true, token: user.token });
  16. };
  17. tokenController.change = async (req, res, next) => {
  18. const user = await utils.authorize(req, res);
  19. const newtoken = randomstring.generate(64);
  20. await db.table('users').where('token', user.token).update({
  21. token: newtoken,
  22. timestamp: Math.floor(Date.now() / 1000)
  23. });
  24. res.json({ success: true, token: newtoken });
  25. };
  26. module.exports = tokenController;