import jwt from 'jsonwebtoken';
import db from '../models';

const Users = db.Users;
const secret = process.env.SECRET || 'samplesecret';

export default {

  createToken(userDetails) {
    return jwt.sign(userDetails, secret, {
      expiresIn: '24h'
    });
  },

  verifyUser(req, res, next) {
    const token = req.body.token
      || req.query.token
      || req.headers['x-access-token'];
    if (token) {
      jwt.verify(token, secret, (err, decoded) => {
        if (err) {
          return res.status(403).send({ message: 'Authentication failed' });
        }
        res.locals.decoded = decoded;
        next();
      });
    } else {
      return res.status(403).send({
        message: 'No token provided'
      });
    }
  },

  authorizeAdmin(req, res, next) {
    if (res.locals.decoded.roleId === 1) {
      next();
    } else {
      return res.status(403).send({ message: 'Access denied' });
    }
  },

  authorizeOwner(req, res, next) {
    Users.findById(req.params.id)
      .then((user) => {
        if (!user) return res.status(404).send({ message: 'User not found' });

        if (res.locals.decoded.roleId !== 1
          && res.locals.decoded.id !== user.id) {
          return res.status(403).send({ message: 'Access denied' });
        }

        res.locals.user = user;
        next();
      })
      .catch(error => res.status(400).send(error));
  },

  hideUserDetails(user) {
    return {
      id: user.id,
      username: user.username,
      fullName: user.fullName,
      email: user.email,
      roleId: user.roleId,
    };
  }
};


@@ -267,20 +267,21 @@
   verify
-User
+Token
 (req, re
@@ -317,52 +317,38 @@
 req.
-body.token%0A      %7C%7C req.query.token
+headers.authorization %7C%7C
 %0A     
- %7C%7C
  req
@@ -558,31 +558,24 @@
         res.
-locals.
 decoded = de

modify function in file [#144369551]