You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
985 B
37 lines
985 B
import { observable } from "mobx"; |
|
|
|
import { Token } from "@client/state/Token"; |
|
|
|
const LOCAL_STORAGE_KEY = "TokenStore"; |
|
|
|
export class TokenStore { |
|
@observable accessToken: Token = new Token(); |
|
@observable refreshToken: Token = new Token(); |
|
|
|
clear() { |
|
this.accessToken.token = null; |
|
this.refreshToken.token = null; |
|
this.saveLocalStorage(); |
|
} |
|
|
|
saveLocalStorage() { |
|
window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(this.toJSON())); |
|
} |
|
|
|
loadLocalStorage() { |
|
const data = window.localStorage.getItem(LOCAL_STORAGE_KEY); |
|
if (data) { |
|
const data2 = JSON.parse(data); |
|
this.updateFromJson(data2); |
|
} |
|
} |
|
|
|
toJSON() { |
|
return { accessToken: this.accessToken.toJSON(), refreshToken: this.refreshToken.toJSON() }; |
|
} |
|
|
|
updateFromJson(json: any) { |
|
this.accessToken.token = json.accessToken; |
|
this.refreshToken.token = json.refreshToken; |
|
} |
|
}
|
|
|