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.
38 lines
985 B
38 lines
985 B
7 years ago
|
import { observable } from "mobx";
|
||
|
|
||
7 years ago
|
import { Token } from "@client/state/Token";
|
||
7 years ago
|
|
||
7 years ago
|
const LOCAL_STORAGE_KEY = "TokenStore";
|
||
|
|
||
7 years ago
|
export class TokenStore {
|
||
|
@observable accessToken: Token = new Token();
|
||
|
@observable refreshToken: Token = new Token();
|
||
|
|
||
7 years ago
|
clear() {
|
||
|
this.accessToken.token = null;
|
||
|
this.refreshToken.token = null;
|
||
|
this.saveLocalStorage();
|
||
|
}
|
||
|
|
||
7 years ago
|
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() {
|
||
7 years ago
|
return { accessToken: this.accessToken.toJSON(), refreshToken: this.refreshToken.toJSON() };
|
||
7 years ago
|
}
|
||
|
|
||
|
updateFromJson(json: any) {
|
||
|
this.accessToken.token = json.accessToken;
|
||
|
this.refreshToken.token = json.refreshToken;
|
||
|
}
|
||
7 years ago
|
}
|