import * as express from 'express' ; import * as jwt from 'jsonwebtoken' ; param req export const extractToken = ( req ) : string => { if ( ! req ) return '' ; const { body = { token : null } , query = { token : null } } = req ; const token = body . token || query . token || req . get ( 'x-access-token' ) || req . get ( 'authentication' ) || req . get ( 'authorization' ) || null ; const maps = { 'NaN' : NaN , 'null' : null , 'undefined' : undefined , 'Infinity' : Infinity , '-Infinity' : - Infinity } ; return ( ( token in maps ) ? maps [ token ] : token ) ; } ; param secretToken param expire param doc export const getToken = ( secretToken = null , expire = null , doc = { } ) : string => { if ( ! doc . _id || ! doc . email ) return '' ; const payload = { _id : doc . _id , email : doc . email } ; return jwt . sign ( payload , secretToken , { expiresIn : + expire } ) ; } ; export const updateToken = ( token , secretToken , expire ) => { try { const payload = jwt . verify ( token , secretToken ) ; delete payload . exp ; delete payload . iat ; return jwt . sign ( payload , secretToken , { expiresIn : + expire } ) ; } catch ( e ) { return '' ; } } ;