author.js 662 B

12345678910111213141516171819202122232425262728
  1. var knex = require('../models/database.js');
  2. var parseToken = function(req,res,next){
  3. const token = req.headers.token;
  4. if(!token || token.length < 5){
  5. //console.log("agains")
  6. req.user = {
  7. ip : req.connection.remoteAddress,
  8. role: 0
  9. };
  10. //console.log(req.user);
  11. return next();
  12. } else {
  13. knex.table('Users').where('token',token).select('*').
  14. then((data)=> {
  15. req.user = data[0] || {};
  16. req.user.ip = req.connection.remoteAddress;
  17. next();
  18. }).catch((error)=> {
  19. console.log("Cannot parseToken");
  20. console.log(error);
  21. res.status(400).send({error:true, message:"something went wrong.."});
  22. });
  23. }
  24. }
  25. module.exports = parseToken;