From cde09d1b503a15f778a4c00537bfb354d77c85dc Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 29 Mar 2023 11:37:03 +0200 Subject: [PATCH] Cleaned up authentication logic. --- gymboard-app/src/api/main/auth.ts | 51 +-------- .../src/components/AccountMenuItem.vue | 3 +- gymboard-app/src/layouts/MainLayout.vue | 6 +- gymboard-app/src/leaflet.d.js.ts | 3 + gymboard-app/src/pages/auth/LoginPage.vue | 29 ++--- .../src/pages/auth/UserSettingsPage.vue | 20 ++-- gymboard-app/src/pages/user/UserPage.vue | 7 +- gymboard-app/src/stores/auth-store.ts | 101 ++++++++++++++++-- 8 files changed, 129 insertions(+), 91 deletions(-) diff --git a/gymboard-app/src/api/main/auth.ts b/gymboard-app/src/api/main/auth.ts index 141f80a..6343393 100644 --- a/gymboard-app/src/api/main/auth.ts +++ b/gymboard-app/src/api/main/auth.ts @@ -1,6 +1,5 @@ import { api } from 'src/api/main/index'; import {AuthStoreType} from 'stores/auth-store'; -import Timeout = NodeJS.Timeout; import { WeightUnit } from 'src/api/main/submission'; import {Page, PaginationOptions, toQueryParams} from 'src/api/main/models'; @@ -67,41 +66,6 @@ export enum UserFollowResponse { } class AuthModule { - private static readonly TOKEN_REFRESH_INTERVAL_MS = 30000; - - private tokenRefreshTimer?: Timeout; - - /** - * Attempts to use the given credentials to obtain an access token for - * sending authenticated requests. - * @param authStore The auth store to use to update app state. - * @param credentials The credentials for logging in. - */ - public async login(authStore: AuthStoreType, credentials: TokenCredentials) { - authStore.token = await this.getNewToken(credentials); - authStore.user = await this.getMyUser(authStore); - // Load the user's attached data right away too. - const [personalDetails, preferences, roles] = await Promise.all([ - this.getMyPersonalDetails(authStore), - this.getMyPreferences(authStore), - this.getMyRoles(authStore) - ]); - authStore.user.personalDetails = personalDetails; - authStore.user.preferences = preferences; - authStore.roles = roles; - - clearTimeout(this.tokenRefreshTimer); - this.tokenRefreshTimer = setInterval( - () => this.refreshToken(authStore), - AuthModule.TOKEN_REFRESH_INTERVAL_MS - ); - } - - public logout(authStore: AuthStoreType) { - authStore.logOut(); - clearTimeout(this.tokenRefreshTimer); - } - public async register(payload: UserCreationPayload): Promise { const response = await api.post('/auth/register', payload); return response.data; @@ -117,18 +81,9 @@ class AuthModule { return response.data.token; } - public async refreshToken(authStore: AuthStoreType) { - try { - const response = await api.get('/auth/token', authStore.axiosConfig); - authStore.token = response.data.token; - } catch (error: any) { - authStore.logOut(); - if (error.response) { - console.warn('Failed to refresh token: ', error.response); - } else { - console.error(error); - } - } + public async refreshToken(authStore: AuthStoreType): Promise { + const response = await api.get('/auth/token', authStore.axiosConfig); + return response.data.token; } public async getMyUser(authStore: AuthStoreType): Promise { diff --git a/gymboard-app/src/components/AccountMenuItem.vue b/gymboard-app/src/components/AccountMenuItem.vue index a3e551b..24b407e 100644 --- a/gymboard-app/src/components/AccountMenuItem.vue +++ b/gymboard-app/src/components/AccountMenuItem.vue @@ -22,7 +22,7 @@ account-related actions. {{ $t('accountMenuItem.settings') }} - + {{ $t('accountMenuItem.logOut') }} @@ -42,7 +42,6 @@ account-related actions. diff --git a/gymboard-app/src/leaflet.d.js.ts b/gymboard-app/src/leaflet.d.js.ts index b745891..23aa587 100644 --- a/gymboard-app/src/leaflet.d.js.ts +++ b/gymboard-app/src/leaflet.d.js.ts @@ -1,3 +1,6 @@ +/** + * Module declaration for the "leaflet" library for showing openstreetmap content. + */ declare module 'leaflet'; export {}; diff --git a/gymboard-app/src/pages/auth/LoginPage.vue b/gymboard-app/src/pages/auth/LoginPage.vue index d3f428c..cae2d75 100644 --- a/gymboard-app/src/pages/auth/LoginPage.vue +++ b/gymboard-app/src/pages/auth/LoginPage.vue @@ -55,19 +55,17 @@