fix: resolve onboarding network error by using authenticated user context
Issue: createItem was using hardcoded userId: 1 without checking if currentUser was authenticated. Backend requires Bearer token from authenticated user, not userId parameter. Changes: - Use currentUser.id instead of hardcoded 1 - Check currentUser exists before attempting cloud sync - Add error handling for cloud sync failures - Graceful fallback to local-only save if not authenticated Resolves 'Network Error' during onboarding item creation.
This commit is contained in:
@@ -165,11 +165,18 @@ export default function Home() {
|
||||
await db.items.add(itemData);
|
||||
|
||||
// 2. If online, try to push to backend immediately
|
||||
if (isOnline) {
|
||||
await inventoryApi.createItem(1, itemData);
|
||||
toast.success("Item saved to cloud catalog!");
|
||||
} else {
|
||||
if (isOnline && currentUser) {
|
||||
try {
|
||||
await inventoryApi.createItem(currentUser.id, itemData);
|
||||
toast.success("Item saved to cloud catalog!");
|
||||
} catch (err: any) {
|
||||
console.error("Cloud sync failed:", err.message);
|
||||
toast.error("Item saved locally. Cloud sync failed.");
|
||||
}
|
||||
} else if (!isOnline) {
|
||||
toast.success("Item saved locally. Will sync when online.");
|
||||
} else {
|
||||
toast("Please log in to save to cloud.", { icon: '⚠️' });
|
||||
}
|
||||
|
||||
setShowOnboarding(false);
|
||||
|
||||
Reference in New Issue
Block a user