The authentication store manages the user’s authentication state. It tracks whether the user is currently authenticated but does not handle the authentication flow itself (that is managed by Google’s authentication system).
State Properties
Whether the user is currently authenticated. Defaults to false.
Actions
setAuthenticated
function setAuthenticated(value: boolean): void
Sets the authentication state. Note that this value is not persisted to localStorage - Google handles automatic token refreshes and sign-ins.
Parameters:
Whether the user should be marked as authenticated
Usage:
import useAuthenticationStore from '@/stores/authentication'
const authStore = useAuthenticationStore()
// After successful Google sign-in
authStore.setAuthenticated(true)
// After sign-out
authStore.setAuthenticated(false)
Example Usage
Here’s an example of using the authentication store in a component:
import { storeToRefs } from 'pinia'
import useAuthenticationStore from '@/stores/authentication'
const authStore = useAuthenticationStore()
const { authenticated } = storeToRefs(authStore)
// Watch for authentication changes
watch(authenticated, (isAuth) => {
if (isAuth) {
console.log('User is authenticated')
// Load user data, etc.
} else {
console.log('User is not authenticated')
// Clear user data, redirect to login, etc.
}
})
// Check authentication status
if (authenticated.value) {
// Show authenticated content
}
Important Notes
- The authentication state is not persisted to localStorage
- Google’s authentication system automatically handles token refreshes and sign-ins
- This store is only for tracking the current authentication state in the application
- The actual authentication flow should be handled by Google’s authentication APIs