1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-09-21 11:11:22 -04:00
shimapan/app/models/Token.js
2017-10-11 20:26:07 -04:00

30 lines
716 B
JavaScript

var fs = require('fs');
var path = require('path');
var mongoose = require('mongoose');
var jwt = require('jsonwebtoken');
var jwtsign = require('jwt-sign');
var TokenSchema = mongoose.Schema({
scope: [String],
issuer: String,
issued: Date,
exp: Date
});
TokenSchema.methods.genJwt = function(expiry) {
var exp = new Date();
exp.setDate(exp.getDate() + expiry);
var payload = {
_id: this._id,
username: this.username,
scope: this.scope,
exp: parseInt(exp.getTime() / 1000)
};
var key = fs.readFilySync(path.join(__dirname, '../../jwt.pem'), 'utf8');
return jwt.sign(payload, key);
};
module.exports = mongoose.model('Token', TokenSchema);