Update userservice to be a class

This commit is contained in:
Matthias 2021-08-28 15:31:19 +02:00
parent 60fe49cbaf
commit 49059e5b83
2 changed files with 38 additions and 22 deletions

View File

@ -5,29 +5,27 @@ import { AuthPayload } from '@/types';
const AUTH_REFRESH_TOKEN = 'auth_ref_token'; const AUTH_REFRESH_TOKEN = 'auth_ref_token';
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 apiBase = '/api/v1'; const APIBASE = '/api/v1';
export default { export class UserService {
apiBase,
AUTH_API_URL,
setAPIUrl(apiurl: string): void { setAPIUrl(apiurl: string): void {
localStorage.setItem(AUTH_API_URL, JSON.stringify(apiurl)); localStorage.setItem(AUTH_API_URL, JSON.stringify(apiurl));
}, }
setAccessToken(token: string): void { setAccessToken(token: string): void {
localStorage.setItem(AUTH_ACCESS_TOKEN, JSON.stringify(token)); localStorage.setItem(AUTH_ACCESS_TOKEN, JSON.stringify(token));
}, }
setRefreshTokens(refreshToken: string): void { setRefreshTokens(refreshToken: string): void {
localStorage.setItem(AUTH_REFRESH_TOKEN, JSON.stringify(refreshToken)); localStorage.setItem(AUTH_REFRESH_TOKEN, JSON.stringify(refreshToken));
}, }
logout(): void { logout(): void {
console.log('Logging out'); console.log('Logging out');
localStorage.removeItem(AUTH_REFRESH_TOKEN); localStorage.removeItem(AUTH_REFRESH_TOKEN);
localStorage.removeItem(AUTH_ACCESS_TOKEN); localStorage.removeItem(AUTH_ACCESS_TOKEN);
localStorage.removeItem(AUTH_API_URL); localStorage.removeItem(AUTH_API_URL);
}, }
async login(auth: AuthPayload) { async login(auth: AuthPayload) {
// Login using username / password // Login using username / password
@ -46,7 +44,7 @@ export default {
if (result.data.refresh_token) { if (result.data.refresh_token) {
this.setRefreshTokens(result.data.refresh_token); this.setRefreshTokens(result.data.refresh_token);
} }
}, }
refreshToken(): Promise<string> { refreshToken(): Promise<string> {
console.log('Refreshing token...'); console.log('Refreshing token...');
@ -54,7 +52,7 @@ export default {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios axios
.post( .post(
`${this.getAPIUrl()}${apiBase}/token/refresh`, `${this.getAPIUrl()}${APIBASE}/token/refresh`,
{}, {},
{ {
headers: { Authorization: `Bearer ${token}` }, headers: { Authorization: `Bearer ${token}` },
@ -78,22 +76,40 @@ export default {
} }
}); });
}); });
}, }
loggedIn() { loggedIn() {
return localStorage.getItem(AUTH_ACCESS_TOKEN) !== null; return localStorage.getItem(AUTH_ACCESS_TOKEN) !== null;
}, }
getAPIUrl(): string { getAPIUrl(): string {
const apiUrl = JSON.parse(localStorage.getItem(AUTH_API_URL) || '{}'); const apiUrl = JSON.parse(localStorage.getItem(AUTH_API_URL) || '{}');
return typeof apiUrl === 'object' ? '' : apiUrl; 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 { getAccessToken(): string {
return JSON.parse(localStorage.getItem(AUTH_ACCESS_TOKEN) || '{}'); return JSON.parse(localStorage.getItem(AUTH_ACCESS_TOKEN) || '{}');
}, }
getRefreshToken() { getRefreshToken() {
return JSON.parse(localStorage.getItem(AUTH_REFRESH_TOKEN) || '{}'); return JSON.parse(localStorage.getItem(AUTH_REFRESH_TOKEN) || '{}');
}, }
}; }
export function useUserService() {
return new UserService();
}
export default useUserService();

View File

@ -15,6 +15,12 @@ Vue.use(Vuex);
const initCurrentTheme = getCurrentTheme(); const initCurrentTheme = getCurrentTheme();
export default new Vuex.Store({ export default new Vuex.Store({
modules: {
ftbot: createBotSubStore(),
alerts: alertsModule,
layout: layoutModule,
uiSettings: settingsModule,
},
state: { state: {
ping: '', ping: '',
loggedIn: userService.loggedIn(), loggedIn: userService.loggedIn(),
@ -39,12 +45,6 @@ export default new Vuex.Store({
return state.uiVersion; return state.uiVersion;
}, },
}, },
modules: {
ftbot: createBotSubStore(),
alerts: alertsModule,
layout: layoutModule,
uiSettings: settingsModule,
},
mutations: { mutations: {
setPing(state, ping) { setPing(state, ping) {
// console.log(ping); // console.log(ping);