diff --git a/src/stores/notifications.ts b/src/stores/notifications.ts index f14c64b..9fc0bf0 100644 --- a/src/stores/notifications.ts +++ b/src/stores/notifications.ts @@ -14,7 +14,7 @@ export const useNotifications = defineStore('notifications', { this.notifications.delete(id) }, - add(type : string, text: string) : void { + add(type : string, text: string, timeout: number = 1000) : void { const id = uuidv4() this.notifications.set( id, @@ -23,7 +23,7 @@ export const useNotifications = defineStore('notifications', { text:text, } ) - setTimeout(() => this.remove(id), 1500) + if (timeout !== -1) setTimeout(() => this.remove(id), timeout) } } }) diff --git a/src/stores/user.ts b/src/stores/user.ts index 293aff5..5d31acf 100644 --- a/src/stores/user.ts +++ b/src/stores/user.ts @@ -5,23 +5,11 @@ import { AxiosError } from 'axios' import axios from 'axios' type AuthSuccResult = { - notification: { - type: string, - text: string - } user: string, role: string, token: string } -type AuthErrResult = { - notification: { - text: string, - type: string - } -} - - export const useUser = defineStore({ id: 'storeUser', @@ -38,16 +26,14 @@ export const useUser = defineStore({ getters: { isAdmin: (state) => state.role === 'admin', - - preferredStorage: (state) => { - return localStorage - /* if (state.rememberMe) return localStorage - else return sessionStorage */ - } }, actions: { - async login(username: string, password: string): Promise { + async login(username: string, password: string): Promise< + boolean | + 'E_INVALID_AUTH_PASSWORD: Password mis-match' | + 'E_INVALID_AUTH_UID: User not found' + > { const notifications = useNotifications() @@ -64,18 +50,26 @@ export const useUser = defineStore({ this.role = response.data.role this.token = response.data.token - notifications.add(response.data.notification.type, response.data.notification.text) + notifications.add('success', 'Login successful.') return true } catch(err : unknown) { if (axios.isAxiosError(err) && err.response && err.response.data){ - const data = err.response.data as AuthErrResult - - notifications.add(data.notification.type, data.notification.text) + const data = err.response.data + if( + data === 'E_INVALID_AUTH_PASSWORD: Password mis-match' || + data === 'E_INVALID_AUTH_UID: User not found' + ) return data } + else if(err instanceof Error){ + notifications.add('danger', err.message, -1) + return false + } + + notifications.add('danger', err as string, -1) return false } @@ -105,7 +99,7 @@ export const useUser = defineStore({ catch(error) { if(error instanceof Error) { - notifications.add('danger', error.message) + notifications.add('danger', error.message, -1) } return false } diff --git a/src/views/Login.vue b/src/views/Login.vue index d8e5fa3..abff9ab 100644 --- a/src/views/Login.vue +++ b/src/views/Login.vue @@ -1,29 +1,36 @@