import _isObject from 'lodash/isObject'; import _isNull from 'lodash/isNull'; import jwtDecode from 'jwt-decode'; import { getStorage, refreshTokenGetter } from 'tailchat-shared'; /** * 获取完整jwt字符串的载荷信息(尝试解析json) * @param jwt 完整的jwt字符串 */ export function getJWTPayload(jwt: string): Partial { try { const decoded = jwtDecode(jwt); return decoded; } catch (e) { console.error(`getJWTInfo Error: [jwt: ${jwt}]`, e); } return {}; } // JWT的内存缓存 let _userJWT: string | null = null; /** * 设置用户登录标识 */ export async function setUserJWT(jwt: string | null): Promise { _userJWT = jwt; // 更新内存中的缓存 await getStorage().save('jsonwebtoken', jwt); refreshTokenGetter(); } /** * 获取用户登录标识 */ export async function getUserJWT(): Promise { if (_isNull(_userJWT)) { const jwt = await getStorage().get('jsonwebtoken'); _userJWT = jwt; // 将其缓存到内存中 return jwt; } return _userJWT; } export interface JWTUserInfoData { _id?: string; nickname?: string; email?: string; avatar?: string; } /** * 获取token中的明文信息 * 明确需要返回一个对象 */ export async function getJWTUserInfo(): Promise { try { const token = await getUserJWT(); const info = getJWTPayload(token); if (_isObject(info)) { return info; } } catch (e) { console.error('getJWTInfo Error:', e); } return {}; }