AuthStore is an Angular service that manages authentication state using Angular signals. It stores the user access token, user information, and provides computed properties for authentication status.
Overview
This injectable service uses Angular’s signal API to provide reactive authentication state management. The state is automatically persisted to localStorage and restored on application restart. Source:src/app/shared/services/state/auth.store.ts
Computed properties
All properties are reactive signals that automatically update when the authentication state changes.userId
Signal containing the current user’s ID
user
Signal containing the current user object with all user properties
accessToken
Signal containing the current user’s access token. Empty string if not authenticated.
isAuthenticated
Signal that returns
true if the user has an access token (authenticated), false otherwiseisAnonymous
Signal that returns
true if the user does not have an access token (anonymous), false otherwiseMethods
setState()
The new authentication state containing the user object and access token
- Updates the internal signal state
- Automatically saves to localStorage using the
userAccessTokenkey - Triggers all computed properties to update
No return value
Usage example
Basic authentication flow
Using computed signals in templates
Checking authentication status
State persistence
The authentication state is automatically:- Saved to localStorage when
setState()is called - Restored from localStorage when the service is initialized
- Stored under the key
userAccessToken
NULL_USER_ACCESS_TOKEN.