'use strict';

/**
 * Module dependencies
 */

// Public node modules
const jwt = require('jsonwebtoken');

/**
 * Service method to generate a new token based on payload we want to put on it.
 *
 * @param   {String}    payload
 *
 * @return {*}
 */

exports.issue = function (payload) {
  return jwt.sign(
    payload,
    process.env.TOKEN_SECRET || strapi.config.jwtSecret || 'oursecret'
  );
};

/**
 * Service method to verify that the token we received on a req hasn't be tampered with.
 *
 * @param   {String}    token   Token to validate
 *
 * @return {*}
 */

exports.verify = function (token) {
  const deferred = Promise.defer();

  jwt.verify(
    token,
    process.env.TOKEN_SECRET || strapi.config.jwtSecret || 'oursecret',
    {},
    function (err, user) {
      if (err || !user || !user.id) {
        return deferred.reject(err);
      }
      deferred.resolve(user);
    }
  );

  return deferred.promise;
};

/**
 * Service method to get current user token. Note that this will also verify actual token value.
 *
 * @param   {Object}    ctx         Context
 * @param   {Boolean}   throwError  Throw error from invalid token specification
 *
 * @return  {*}
 */

exports.getToken = function *(ctx, throwError) {
  const params = _.assign({}, ctx.request.body, ctx.request.query);

  let token = '';

  // Yeah we got required 'authorization' header.
  if (ctx.request && ctx.request.header && ctx.request.header.authorization) {
    const parts = ctx.request.header.authorization.split(' ');

    if (parts.length === 2) {
      const scheme = parts[0];
      const credentials = parts[1];
      if (/^Bearer$/i.test(scheme)) {
        token = credentials;
      }
    } else if (throwError) {
      throw new Error('Invalid authorization header format. Format is Authorization: Bearer [token]');
    }
  } else if (params.token) {
    token = params.token;
  } else if (throwError) {
    throw new Error('No authorization header was found');
  }

  return exports.verify(token);
};

@@ -321,37 +321,35 @@
     process.env.
-TOKEN
+JWT
 _SECRET %7C%7C strap
@@ -349,32 +349,38 @@
 %7C strapi.config.
+users.
 jwtSecret %7C%7C 'ou
@@ -684,13 +684,11 @@
 env.
-TOKEN
+JWT
 _SEC
@@ -708,16 +708,22 @@
 .config.
+users.
 jwtSecre

Rename JWT_SECRET process variable