Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as _ from 'underscore';
- import * as moment from 'moment';
- import * as jwt from 'jwt-simple';
- import * as config from 'config';
- import * as Log from './Log';
- /**
- * エンコードする
- * 有効期限を付与する
- * @param data
- * @return {string}
- */
- export function encode(data) {
- const expires = moment().add(6, 'hours').valueOf();
- return jwt.encode(_.extend({}, data, { expires }), config.jwt.secret);
- }
- /**
- * デコードする
- * @param token
- * @return {any}
- * @desc 呼び出し元でtokenのnullチェックを行う
- */
- export function decode(token) {
- try {
- const decoded = jwt.decode(token, config.jwt.secret);
- if (decoded) {
- if (_.isNumber(decoded.expires) && decoded.expires > Date.now()) {
- return decoded;
- }
- Log.access.warn('expired', decoded);
- } else {
- Log.access.error('invalid token', token);
- }
- } catch (err) {
- Log.access.error('decode error', token, err.stack || err.message || err);
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement