From 49059e5b835991ce2ac7bb15b1a4fab3b9f12cbb Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 28 Aug 2021 15:31:19 +0200 Subject: [PATCH] Update userservice to be a class --- src/shared/userService.ts | 48 ++++++++++++++++++++++++++------------- src/store/index.ts | 12 +++++----- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/shared/userService.ts b/src/shared/userService.ts index 4f423d54..09a3976f 100644 --- a/src/shared/userService.ts +++ b/src/shared/userService.ts @@ -5,29 +5,27 @@ import { AuthPayload } from '@/types'; const AUTH_REFRESH_TOKEN = 'auth_ref_token'; const AUTH_ACCESS_TOKEN = 'auth_access_token'; const AUTH_API_URL = 'auth_api_url'; -const apiBase = '/api/v1'; +const APIBASE = '/api/v1'; -export default { - apiBase, - AUTH_API_URL, +export class UserService { setAPIUrl(apiurl: string): void { localStorage.setItem(AUTH_API_URL, JSON.stringify(apiurl)); - }, + } setAccessToken(token: string): void { localStorage.setItem(AUTH_ACCESS_TOKEN, JSON.stringify(token)); - }, + } setRefreshTokens(refreshToken: string): void { localStorage.setItem(AUTH_REFRESH_TOKEN, JSON.stringify(refreshToken)); - }, + } logout(): void { console.log('Logging out'); localStorage.removeItem(AUTH_REFRESH_TOKEN); localStorage.removeItem(AUTH_ACCESS_TOKEN); localStorage.removeItem(AUTH_API_URL); - }, + } async login(auth: AuthPayload) { // Login using username / password @@ -46,7 +44,7 @@ export default { if (result.data.refresh_token) { this.setRefreshTokens(result.data.refresh_token); } - }, + } refreshToken(): Promise { console.log('Refreshing token...'); @@ -54,7 +52,7 @@ export default { return new Promise((resolve, reject) => { axios .post( - `${this.getAPIUrl()}${apiBase}/token/refresh`, + `${this.getAPIUrl()}${APIBASE}/token/refresh`, {}, { headers: { Authorization: `Bearer ${token}` }, @@ -78,22 +76,40 @@ export default { } }); }); - }, + } loggedIn() { return localStorage.getItem(AUTH_ACCESS_TOKEN) !== null; - }, + } getAPIUrl(): string { const apiUrl = JSON.parse(localStorage.getItem(AUTH_API_URL) || '{}'); return typeof apiUrl === 'object' ? '' : apiUrl; - }, + } + + getBaseUrl(): string { + const baseURL = this.getAPIUrl(); + if (baseURL === null) { + // Relative url + return APIBASE; + } + if (!baseURL.endsWith(APIBASE)) { + return `${baseURL}${APIBASE}`; + } + return `${baseURL}${APIBASE}`; + } getAccessToken(): string { return JSON.parse(localStorage.getItem(AUTH_ACCESS_TOKEN) || '{}'); - }, + } getRefreshToken() { return JSON.parse(localStorage.getItem(AUTH_REFRESH_TOKEN) || '{}'); - }, -}; + } +} + +export function useUserService() { + return new UserService(); +} + +export default useUserService(); diff --git a/src/store/index.ts b/src/store/index.ts index 6a133e8c..406b9fb4 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -15,6 +15,12 @@ Vue.use(Vuex); const initCurrentTheme = getCurrentTheme(); export default new Vuex.Store({ + modules: { + ftbot: createBotSubStore(), + alerts: alertsModule, + layout: layoutModule, + uiSettings: settingsModule, + }, state: { ping: '', loggedIn: userService.loggedIn(), @@ -39,12 +45,6 @@ export default new Vuex.Store({ return state.uiVersion; }, }, - modules: { - ftbot: createBotSubStore(), - alerts: alertsModule, - layout: layoutModule, - uiSettings: settingsModule, - }, mutations: { setPing(state, ping) { // console.log(ping);