Auth Store Refactoring
Auth Store Refactoring
Section titled “Auth Store Refactoring”The auth store has been refactored to follow the options API pattern similar to the reference implementation provided.
Changes Made
Section titled “Changes Made”1. Store Structure
Section titled “1. Store Structure”Before (Composition API):
export const useAuthStore = defineStore("auth", () => { const account = ref<Account | null>(null); const isAuthenticated = computed(() => !!account.value); // ... functions});
After (Options API):
export const useAuthStore = defineStore('auth', { state: (): AuthStateStorable => ({ ...defaultAuthState }), getters: { account: (state) => state.userIdentity, isAuthenticated: (state) => state.authenticated, }, actions: { // ... methods },});
2. State Interface
Section titled “2. State Interface”Added explicit state interface and default state:
export interface AuthStateStorable { logon: Promise<unknown> | null; userIdentity: Account | null; authenticated: boolean; profilesLoaded: boolean; ribbonOnProfiles: string; activeProfiles: string;}
export const defaultAuthState: AuthStateStorable = { logon: null, userIdentity: null, authenticated: false, profilesLoaded: false, ribbonOnProfiles: '', activeProfiles: '',};
3. New Actions Added
Section titled “3. New Actions Added”Profile Management
Section titled “Profile Management”setProfilesLoaded()
- Mark profiles as loadedsetActiveProfiles(profile: string)
- Set active profilesetRibbonOnProfiles(ribbon: string)
- Set ribbon configuration
Authentication Flow
Section titled “Authentication Flow”authenticate(promise: Promise<unknown>)
- Track authentication promisesetAuthentication(identity: Account)
- Set user identity and mark as authenticatedlogout()
- Clear authentication state (sync)logoutAsync()
- Logout with server call (async)
4. Method Changes
Section titled “4. Method Changes”Login Process
Section titled “Login Process”// Beforeasync function login(username: string, password: string): Promise<boolean>
// Afterasync login(username: string, password: string): Promise<boolean>
Now tracks the login promise with authenticate()
method.
Logout Process
Section titled “Logout Process”// Beforeasync function logout(): Promise<void>
// Afterlogout() // Sync state clearinglogoutAsync(): Promise<void> // Async server logout
5. Component Updates
Section titled “5. Component Updates”Updated components to use the new async logout method:
AppHeader.vue
:authStore.logout()
→authStore.logoutAsync()
AppLayoutAdmin.vue
:authStore.logout()
→authStore.logoutAsync()
6. Test Updates
Section titled “6. Test Updates”Updated test file to work with the new store structure:
- Changed direct property access:
authStore.account
→authStore.userIdentity
- Updated logout test to use
authStore.logoutAsync()
Benefits
Section titled “Benefits”- Consistency: Matches the reference implementation pattern
- Extensibility: Easy to add profile management features
- State Tracking: Better tracking of authentication promises
- Type Safety: Explicit state interface with TypeScript support
- Separation of Concerns: Clear distinction between sync/async operations
Usage Examples
Section titled “Usage Examples”Basic Authentication Check
Section titled “Basic Authentication Check”const authStore = useAuthStore();if (authStore.isAuthenticated) { // User is logged in}
try { await authStore.login(username, password); // Success} catch (error) { // Handle error}
Logout
Section titled “Logout”try { await authStore.logoutAsync(); // Logged out successfully} catch (error) { // Server logout failed, but local state is cleared}
Profile Management
Section titled “Profile Management”// Set active profileauthStore.setActiveProfiles('admin');
// Mark profiles as loadedauthStore.setProfilesLoaded();
// Set ribbon configurationauthStore.setRibbonOnProfiles('development');
The refactored store maintains backward compatibility for the core authentication features while adding new capabilities for profile management and better state tracking.