diff --git a/components/epics/epic-form.tsx b/components/epics/epic-form.tsx index 20134a2..462a8a5 100644 --- a/components/epics/epic-form.tsx +++ b/components/epics/epic-form.tsx @@ -59,6 +59,7 @@ export function EpicForm({ projectId, epic, onSuccess, onCancel }: EpicFormProps const createEpic = useCreateEpic(); const updateEpic = useUpdateEpic(); const user = useAuthStore((state) => state.user); + const isHydrated = useAuthStore((state) => state.isHydrated); const form = useForm({ resolver: zodResolver(epicSchema), @@ -71,12 +72,19 @@ export function EpicForm({ projectId, epic, onSuccess, onCancel }: EpicFormProps }); async function onSubmit(data: EpicFormValues) { - console.log('[EpicForm] onSubmit triggered', { data, user: user?.id, projectId }); + console.log('[EpicForm] onSubmit triggered', { data, user: user?.id, projectId, isHydrated }); try { + // Check if auth store has completed hydration + if (!isHydrated) { + console.warn('[EpicForm] Auth store not hydrated yet, waiting...'); + toast.error('Loading user information, please try again in a moment'); + return; + } + if (!user?.id) { console.error('[EpicForm] User not authenticated'); - toast.error('User not authenticated'); + toast.error('Please log in to create an epic'); return; } @@ -239,9 +247,9 @@ export function EpicForm({ projectId, epic, onSuccess, onCancel }: EpicFormProps Cancel )} - diff --git a/stores/authStore.ts b/stores/authStore.ts index 9ffa237..9e5ca3d 100644 --- a/stores/authStore.ts +++ b/stores/authStore.ts @@ -6,6 +6,7 @@ interface AuthState { user: User | null; isAuthenticated: boolean; isLoading: boolean; + isHydrated: boolean; setUser: (user: User) => void; clearUser: () => void; @@ -18,6 +19,7 @@ export const useAuthStore = create()( user: null, isAuthenticated: false, isLoading: true, + isHydrated: false, setUser: (user) => set({ user, isAuthenticated: true, isLoading: false }), @@ -31,6 +33,16 @@ export const useAuthStore = create()( user: state.user, isAuthenticated: state.isAuthenticated, }), + onRehydrateStorage: () => (state) => { + console.log('[AuthStore] Hydration started'); + if (state) { + state.isHydrated = true; + console.log('[AuthStore] Hydration completed', { + user: state.user?.id, + isAuthenticated: state.isAuthenticated + }); + } + }, } ) );