zhengyiming
昨天 1327b99efda99ada27a956981648950f580314b0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { Base64 } from 'js-base64';
 
export interface AccountInfo {
  address: string;
  amr: string[];
  aud: string;
  auth_time: number;
  client_id: string;
  email: string;
  email_verified: string;
  exp: number;
  iat: number;
  idp: string;
  iss: string;
  name: string;
  sub: string;
  role: string;
  nbf: number;
}
 
export function getAccountInfoFromAccessToken(accessToken: string): Partial<AccountInfo> {
  if (accessToken) {
    const accessInfo = Base64.decode(accessToken.split(/\.(.+?)\./g)[1]);
    return JSON.parse(accessInfo) as AccountInfo;
  }
  return {};
}
 
export function tokenIsExpired(userInfo: API.IdentityModelTokenCacheItem) {
  const now = new Date().getTime();
  const expiresIn = userInfo.expiresIn;
  const expired = (now - new Date(userInfo.creationTime).getTime()) / 1000 > expiresIn - 300;
  return expired;
}