Add autorefresh to userService

This commit is contained in:
Matthias 2021-08-31 20:29:02 +02:00
parent 22e3c1a976
commit b73c5cbaed
2 changed files with 20 additions and 2 deletions

View File

@ -37,6 +37,16 @@ export class UserService {
this.storeLoginInfo(loginInfo); this.storeLoginInfo(loginInfo);
} }
/**
* Store autorefresh preference for this bot instance
* @param autoRefresh new autoRefresh value
*/
public setAutoRefresh(autoRefresh: boolean): void {
const loginInfo = this.getLoginInfo();
loginInfo.autoRefresh = autoRefresh;
this.storeLoginInfo(loginInfo);
}
/** /**
* Retrieve full logininfo object (for all registered bots) * Retrieve full logininfo object (for all registered bots)
* @returns * @returns
@ -60,6 +70,7 @@ export class UserService {
apiUrl: '', apiUrl: '',
refreshToken: '', refreshToken: '',
accessToken: '', accessToken: '',
autoRefresh: false,
}; };
} }
@ -81,6 +92,10 @@ export class UserService {
return Object.keys(allInfo); return Object.keys(allInfo);
} }
public getAutoRefresh(): boolean {
return this.getLoginInfo().autoRefresh;
}
public getAccessToken(): string { public getAccessToken(): string {
return this.getLoginInfo().accessToken; return this.getLoginInfo().accessToken;
} }
@ -118,6 +133,7 @@ export class UserService {
apiUrl: auth.url, apiUrl: auth.url,
accessToken: result.data.access_token || '', accessToken: result.data.access_token || '',
refreshToken: result.data.refresh_token || '', refreshToken: result.data.refresh_token || '',
autoRefresh: true,
}; };
this.storeLoginInfo(obj); this.storeLoginInfo(obj);
} }
@ -175,10 +191,12 @@ export class UserService {
const AUTH_REFRESH_TOKEN = 'auth_ref_token'; // Legacy key - do not use const AUTH_REFRESH_TOKEN = 'auth_ref_token'; // Legacy key - do not use
const AUTH_ACCESS_TOKEN = 'auth_access_token'; const AUTH_ACCESS_TOKEN = 'auth_access_token';
const AUTH_API_URL = 'auth_api_url'; const AUTH_API_URL = 'auth_api_url';
const AUTO_REFRESH = 'ft_auto_refresh';
const apiUrl = JSON.parse(localStorage.getItem(AUTH_API_URL) || '{}'); const apiUrl = JSON.parse(localStorage.getItem(AUTH_API_URL) || '{}');
const refreshToken = JSON.parse(localStorage.getItem(AUTH_REFRESH_TOKEN) || '{}'); const refreshToken = JSON.parse(localStorage.getItem(AUTH_REFRESH_TOKEN) || '{}');
const accessToken = JSON.parse(localStorage.getItem(AUTH_ACCESS_TOKEN) || '{}'); const accessToken = JSON.parse(localStorage.getItem(AUTH_ACCESS_TOKEN) || '{}');
const autoRefresh: boolean = JSON.parse(localStorage.getItem(AUTO_REFRESH) || '{}');
if ( if (
typeof apiUrl === 'string' && typeof apiUrl === 'string' &&
typeof refreshToken === 'string' && typeof refreshToken === 'string' &&
@ -189,6 +207,7 @@ export class UserService {
apiUrl, apiUrl,
refreshToken, refreshToken,
accessToken, accessToken,
autoRefresh,
}; };
this.storeLoginInfo(loginInfo); this.storeLoginInfo(loginInfo);
} }
@ -204,5 +223,3 @@ export function useUserService(botId: string) {
userservice.migrateLogin(); userservice.migrateLogin();
return userservice; return userservice;
} }
// export default useUserService('ftbot.0');

View File

@ -10,6 +10,7 @@ export interface AuthStorage {
apiUrl: string; apiUrl: string;
refreshToken: string; refreshToken: string;
accessToken: string; accessToken: string;
autoRefresh: boolean;
} }
export interface AuthStorageMulti { export interface AuthStorageMulti {