<commit_before>var log = require('debug')('democracyos:jwt:token')
var moment = require('moment')
var jwt = require('jwt-simple')
var config = require('lib/config')
var utils = require('lib/utils')
var expose = utils.expose

function exposeConfidential (user) {
  return expose('id firstName lastName email avatar staff notifications')(user)
}

exports.encode = function encode (user) {
  user = exposeConfidential(user)

  var expires = moment().add(15, 'days').valueOf()

  var token = jwt.encode({
    iss: user.email,
    exp: expires
  }, config.jwtSecret)

  return {
    token: token,
    expires: expires,
    user: user
  }
}

exports.decode = function decode (encoded, cb) {
  try {
    log('Attempting to decode token...')
    var decoded = jwt.decode(encoded, config.jwtSecret)

    if (decoded.exp <= Date.now()) {
      log('Access token has expired')
      return cb(new Error('Access token has expired'))
    }

    var User = require('lib/models').User
    User.findByEmail(decoded.iss, function (err, user) {
      if (err) log('Token has been decoded, but user fetching failed with the following error: %s', err)<commit_msg>update usage of user.id to save session<commit_after>var log = require('debug')('democracyos:jwt:token')
var moment = require('moment')
var jwt = require('jwt-simple')
var config = require('lib/config')

exports.encode = function encode (user) {
  var expires = moment().add(15, 'days').valueOf()

  var token = jwt.encode({
    iss: user.id,
    exp: expires
  }, config.jwtSecret)

  return {
    token: token,
    expires: expires
  }
}

exports.decode = function decode (encoded, cb) {
  try {
    log('Attempting to decode token...')
    var decoded = jwt.decode(encoded, config.jwtSecret)

    if (decoded.exp <= Date.now()) {
      log('Access token has expired')
      return cb(new Error('Access token has expired'))
    }

    var User = require('lib/models').User
    User.findOne({
      _id: decoded.iss
    }, function (err, user) {
      if (err) log('Token has been decoded, but user fetching failed with the following error: %s', err)
{'fraction_non_alphanumeric': 0.13801337153772683, 'fraction_numerical': 0.0019102196752626551, 'mean_word_length': 3.5842450765864333, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3777958', 'n_tokens_mistral': 674, 'n_tokens_neox': 642, 'n_words': 194}