@@ -2,7 +2,6 @@ import type { UserResource } from '@/types/resources';
22
33import { authenticate } from '@/service/authAPI' ;
44import { defineStore } from 'pinia' ;
5- import { AxiosError } from 'axios' ;
65import { toast } from '@aminnausin/cedar-ui' ;
76import { ref } from 'vue' ;
87
@@ -15,43 +14,37 @@ export const useAuthStore = defineStore('Auth', () => {
1514
1615 const fetchUser = async ( force : boolean = false ) : Promise < boolean > => {
1716 /*
18- Auth States:
17+ Auth States (not very good) :
1918
20- 1: Never logged in -> No token -> state is null
21- 2: Logged in previously -> Token -> Token is invalid (ajax) -> State is false
22- 3: Logged in previously -> Token -> Token is valid (ajax) -> State is true
23- 4: State exists -> State is State (Logged in or out has already been checked)
19+ 1: Initial -> No checks have run -> State is null
20+ 2: Never logged in -> No State -> State is null
21+ 3: State exists -> State is State (Logged in or out has already been checked)
2422
2523 */
2624
27- if ( ! localStorage . getItem ( 'auth-token' ) ) return false ; // no auth token
28-
29- if ( userData . value && ! force ) return true ;
30-
25+ if ( userData . value ?. id && ! force ) return true ; // Bad practice? Should I always check?
26+ if ( isLoadingUserData . value ) return false ;
3127 if ( userFetchPromise ) return userFetchPromise ;
3228
3329 userFetchPromise = ( async ( ) => {
3430 isLoadingUserData . value = true ;
3531
3632 try {
37- const localToken = localStorage . getItem ( 'auth-token' ) ;
38- if ( ! localToken ) {
39- clearAuthState ( ) ;
40- return false ;
41- }
42-
43- const { data, status } = await authenticate ( localToken ) ;
33+ const { data } = await authenticate ( ) ;
4434
45- if ( status !== 200 ) {
46- throw new AxiosError ( 'Not Authenticated' , status . toString ( ) ) ;
35+ if ( data . isAuthenticated ) {
36+ userData . value = data . user ;
37+ return true ;
4738 }
4839
49- userData . value = data . data ?. user ;
50- return true ;
51- } catch ( error ) {
52- clearAuthState ( ) ;
40+ console . log ( 'Clear auth state because not authenticated' ) ;
5341
54- console . error ( 'Authentication failed:' , error ) ;
42+ clearAuthState ( false ) ;
43+ return false ;
44+ } catch ( error ) {
45+ // Only when network or server error
46+ console . error ( error ) ;
47+ clearAuthState ( true ) ;
5548 return false ;
5649 } finally {
5750 isLoadingUserData . value = false ;
@@ -64,12 +57,12 @@ export const useAuthStore = defineStore('Auth', () => {
6457
6558 const clearAuthState = ( showMessage : boolean = false , status = 401 ) : void => {
6659 userData . value = null ;
67- isLoadingUserData . value = false ;
68- localStorage . removeItem ( 'auth-token' ) ;
60+ localStorage . removeItem ( 'auth-token' ) ; // Legacy: Clears existing auth-tokens. No auth-tokens are created ever again.
6961
7062 if ( ! showMessage ) return ;
71- const message = status === 419 ? 'Session Expired' : `Authentication Failed (${ status } )`;
63+ const message = `Login Expired ${ status ? ` (${ status } )` : '' } `;
7264
65+ // Can remove?
7366 toast . warning ( message , {
7467 description : 'Please log in again.' ,
7568 } ) ;
0 commit comments