import * as express from 'express' ; import * as jwt from 'jsonwebtoken' ; export const tokenMiddleware = ( serverConfig ) : express . RequestHandler => { return ( req , res , next ) => { const originalJson = res . json ; const token = req . body . token || req . query . token || req . get ( 'x-access-token' ) || req . get ( 'authentication' ) || req . get ( 'authorization' ) ; res . json = ( body ? ) : express . Response => { if ( res . statusCode === 200 && body && token && ! req . originalUrl . startsWith ( '/auth' ) && typeof body === 'object' ) { body . token = updateToken ( token , serverConfig . secretTokent , + serverConfig . jwtExpire ) ; } return originalJson . call ( res , body ) ; } ; next ( ) ; } ; } ; 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 '' ; } } ;