jwt-autorefresh
如果您使用的是节点(React / Redux / Universal JS),则可以安装 npm i -S jwt-autorefresh .
此库根据用户计算的访问令牌到期之前的秒数(基于令牌中编码的exp声明)计划刷新JWT令牌 . 它有一个广泛的测试套件,并检查很多条件,以确保任何奇怪的活动伴随着有关您的环境配置错误的描述性消息 .
Full example implementation
import autorefresh from ‘jwt-autorefresh’
/** Events in your app that are triggered when your user becomes authorized or deauthorized. */
import { onAuthorize, onDeauthorize } from ‘./events’
/** Your refresh token mechanism, returning a promise that resolves to the new access tokenFunction (library does not care about your method of persisting tokens) */
const refresh = () => {
const init = { method: ‘POST’
, headers: { ‘Content-Type’: `application/x-www-form-urlencoded` }
, body: `refresh_token=${localStorage.refresh_token}&grant_type=refresh