@@ -2,6 +2,9 @@ import { useState, useRuntimeConfig } from '#app'
22import { computed , readonly } from 'vue'
33import type { User , UserWithoutPassword } from 'nuxt-users/utils'
44
5+ // Global flag to track if initialization has been attempted
6+ let initializationPromise : Promise < void > | null = null
7+
58export const useAuthentication = ( ) => {
69 const user = useState < UserWithoutPassword | null > ( 'user' , ( ) => null )
710 const { public : { nuxtUsers } } = useRuntimeConfig ( )
@@ -13,17 +16,18 @@ export const useAuthentication = () => {
1316 // Remove password from user data before storing
1417 const { password : _ , ...userWithoutPassword } = userData
1518 user . value = userWithoutPassword
16-
19+
1720 // Store user data based on rememberMe preference
1821 if ( import . meta. client ) {
1922 // Clear any existing user data from both storages
2023 localStorage . removeItem ( 'user' )
2124 sessionStorage . removeItem ( 'user' )
22-
25+
2326 if ( rememberMe ) {
2427 // Store in localStorage for persistent login across browser sessions
2528 localStorage . setItem ( 'user' , JSON . stringify ( userWithoutPassword ) )
26- } else {
29+ }
30+ else {
2731 // Store in sessionStorage for session-only login
2832 sessionStorage . setItem ( 'user' , JSON . stringify ( userWithoutPassword ) )
2933 }
@@ -55,18 +59,20 @@ export const useAuthentication = () => {
5559 try {
5660 const response = await $fetch < { user : UserWithoutPassword } > ( `${ apiBasePath } /me` , { method : 'GET' } )
5761 user . value = response . user
58-
62+
5963 // Update the appropriate storage with fresh user data
6064 if ( import . meta. client ) {
6165 // Determine which storage was being used and update it
6266 const wasInLocalStorage = localStorage . getItem ( 'user' ) !== null
6367 const wasInSessionStorage = sessionStorage . getItem ( 'user' ) !== null
64-
68+
6569 if ( wasInLocalStorage ) {
6670 localStorage . setItem ( 'user' , JSON . stringify ( response . user ) )
67- } else if ( wasInSessionStorage ) {
71+ }
72+ else if ( wasInSessionStorage ) {
6873 sessionStorage . setItem ( 'user' , JSON . stringify ( response . user ) )
69- } else {
74+ }
75+ else {
7076 // Default to sessionStorage for new sessions without rememberMe
7177 sessionStorage . setItem ( 'user' , JSON . stringify ( response . user ) )
7278 }
@@ -95,7 +101,7 @@ export const useAuthentication = () => {
95101 const storedUserLocal = localStorage . getItem ( 'user' )
96102 const storedUserSession = sessionStorage . getItem ( 'user' )
97103 const storedUser = storedUserLocal || storedUserSession
98-
104+
99105 if ( storedUser ) {
100106 try {
101107 // First set the user from storage for immediate UI response
@@ -113,6 +119,14 @@ export const useAuthentication = () => {
113119 }
114120 }
115121
122+ // Auto-initialize user on first access (client-side only)
123+ if ( import . meta. client && ! initializationPromise ) {
124+ initializationPromise = initializeUser ( ) . catch ( ( ) => {
125+ // Silently handle initialization errors
126+ // Don't prevent the composable from working
127+ } )
128+ }
129+
116130 return {
117131 user : readonly ( user ) ,
118132 isAuthenticated,
0 commit comments