Skip to content

Commit

Permalink
feat: tokens cache cookie -> localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
chenweigh committed Oct 16, 2024
1 parent 343e19c commit e01bf49
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
2 changes: 0 additions & 2 deletions xterio-auth/src/modules/XterAuthInfo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { Env, ISSoTokensParams, ITokenRes, IUserInfo } from 'interfaces/loginInfo'
import { LoginType } from 'interfaces/loginInfo'
import { XterioCache } from './XterCache'
import { XterEventEmiter } from './XterEventEmitter'
import { XTERIO_EVENTS } from 'utils'

export class XterioAuthInfo {
/** client id */
Expand Down
21 changes: 10 additions & 11 deletions xterio-auth/src/modules/XterCache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ITokenRes, IUserInfo } from 'interfaces/loginInfo'
import { XTERIO_CONST } from 'utils'
import Cookies from 'js-cookie'

export class XterioCache {
static set loginType(_type: string) {
Expand All @@ -12,25 +11,25 @@ export class XterioCache {
}
static set tokens(value: Partial<ITokenRes>) {
const { access_token = '', id_token = '', refresh_token = '' } = value
Cookies.set(XTERIO_CONST.ACCESS_TOKEN, access_token, { expires: 1 })
Cookies.set(XTERIO_CONST.ID_TOKEN, id_token, { expires: 1 })
Cookies.set(XTERIO_CONST.REFRESH_TOKEN, refresh_token, { expires: 180 })
localStorage.setItem(XTERIO_CONST.ACCESS_TOKEN, access_token)
localStorage.setItem(XTERIO_CONST.ID_TOKEN, id_token)
localStorage.setItem(XTERIO_CONST.REFRESH_TOKEN, refresh_token)
}
static get tokens(): ITokenRes {
const _t: ITokenRes = {
access_token: Cookies.get(XTERIO_CONST.ACCESS_TOKEN) || '',
id_token: Cookies.get(XTERIO_CONST.ID_TOKEN) || '',
refresh_token: Cookies.get(XTERIO_CONST.REFRESH_TOKEN) || ''
access_token: localStorage.getItem(XTERIO_CONST.ACCESS_TOKEN) || '',
id_token: localStorage.getItem(XTERIO_CONST.ID_TOKEN) || '',
refresh_token: localStorage.getItem(XTERIO_CONST.REFRESH_TOKEN) || ''
}
return _t
}
static deleteTokens(key?: string) {
if (key) {
Cookies.remove(key)
localStorage.removeItem(key)
} else {
Cookies.remove(XTERIO_CONST.ACCESS_TOKEN)
Cookies.remove(XTERIO_CONST.REFRESH_TOKEN)
Cookies.remove(XTERIO_CONST.ID_TOKEN)
localStorage.removeItem(XTERIO_CONST.ACCESS_TOKEN)
localStorage.removeItem(XTERIO_CONST.REFRESH_TOKEN)
localStorage.removeItem(XTERIO_CONST.ID_TOKEN)
}
}

Expand Down

0 comments on commit e01bf49

Please sign in to comment.