tokenController.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const config = require('../config.js')
  2. const db = require('knex')(config.database)
  3. let tokenController = {}
  4. tokenController.verify = function(req, res, next){
  5. if(req.body.token === undefined) return res.json({ success: false, description: 'No token provided' })
  6. let token = req.body.token
  7. db.table('users').where('token', token).then((user) => {
  8. if(user.length === 0) return res.json({ success: false, description: 'Token mismatch' })
  9. return res.json({ success: true })
  10. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  11. }
  12. tokenController.list = function(req, res, next){
  13. let token = req.headers.token
  14. if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
  15. db.table('users').where('token', token).then((user) => {
  16. if(user.length === 0) return res.json({ success: false, description: 'Token mismatch' })
  17. return res.json({ success: true, token: token })
  18. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  19. }
  20. tokenController.change = function(req, res, next){
  21. let token = req.headers.token
  22. if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
  23. let newtoken = randomstring.generate(64)
  24. db.table('users').where('token', token).update({
  25. token: newtoken,
  26. timestamp: Math.floor(Date.now() / 1000)
  27. }).then((user) => {
  28. res.json({ success: true, token: newtoken })
  29. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  30. }
  31. module.exports = tokenController