From b73c5cbaed3d37be65ee838fa8650e394ea604ed Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 31 Aug 2021 20:29:02 +0200 Subject: [PATCH] Add autorefresh to userService --- src/shared/userService.ts | 21 +++++++++++++++++++-- src/types/auth.ts | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/shared/userService.ts b/src/shared/userService.ts index cf235b99..bcc30b06 100644 --- a/src/shared/userService.ts +++ b/src/shared/userService.ts @@ -37,6 +37,16 @@ export class UserService { 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) * @returns @@ -60,6 +70,7 @@ export class UserService { apiUrl: '', refreshToken: '', accessToken: '', + autoRefresh: false, }; } @@ -81,6 +92,10 @@ export class UserService { return Object.keys(allInfo); } + public getAutoRefresh(): boolean { + return this.getLoginInfo().autoRefresh; + } + public getAccessToken(): string { return this.getLoginInfo().accessToken; } @@ -118,6 +133,7 @@ export class UserService { apiUrl: auth.url, accessToken: result.data.access_token || '', refreshToken: result.data.refresh_token || '', + autoRefresh: true, }; this.storeLoginInfo(obj); } @@ -175,10 +191,12 @@ export class UserService { const AUTH_REFRESH_TOKEN = 'auth_ref_token'; // Legacy key - do not use const AUTH_ACCESS_TOKEN = 'auth_access_token'; const AUTH_API_URL = 'auth_api_url'; + const AUTO_REFRESH = 'ft_auto_refresh'; const apiUrl = JSON.parse(localStorage.getItem(AUTH_API_URL) || '{}'); const refreshToken = JSON.parse(localStorage.getItem(AUTH_REFRESH_TOKEN) || '{}'); const accessToken = JSON.parse(localStorage.getItem(AUTH_ACCESS_TOKEN) || '{}'); + const autoRefresh: boolean = JSON.parse(localStorage.getItem(AUTO_REFRESH) || '{}'); if ( typeof apiUrl === 'string' && typeof refreshToken === 'string' && @@ -189,6 +207,7 @@ export class UserService { apiUrl, refreshToken, accessToken, + autoRefresh, }; this.storeLoginInfo(loginInfo); } @@ -204,5 +223,3 @@ export function useUserService(botId: string) { userservice.migrateLogin(); return userservice; } - -// export default useUserService('ftbot.0'); diff --git a/src/types/auth.ts b/src/types/auth.ts index 2ea06df7..61a2f740 100644 --- a/src/types/auth.ts +++ b/src/types/auth.ts @@ -10,6 +10,7 @@ export interface AuthStorage { apiUrl: string; refreshToken: string; accessToken: string; + autoRefresh: boolean; } export interface AuthStorageMulti {