# API Integration Issues - Analysis & Fixes

## Issues Found

### Issue #1: Response Structure Mismatch
**Backend Returns:** 
```json
{
  "token": "jwt_token",
  "user": {
    "id": "user_id",
    "name": "User Name",
    "email": "user@email.com",
    "balance": 100.00,
    "created_at": "2026-04-08T10:00:00Z"
  }
}
```

**Flutter UserModel.fromJson()** handles the user data but missing `balance` field support.

---

### Issue #2: Type Casting Error in auth_repository.dart
[lib/data/repositories/auth_repository.dart](lib/data/repositories/auth_repository.dart#L30-L37)

The code does:
```dart
final user = response['user'] is Map<String, dynamic>
    ? UserModel.fromJson(response['user'])
    : null;
```

This might be failing if `response['user']` contains unexpected data type.

---

### Issue #3: No Null Safety Check on Token
[lib/data/repositories/auth_repository.dart](lib/data/repositories/auth_repository.dart#L29)

```dart
final token = response['token'] as String?;
```

This could fail if 'token' is not in response or is null.

---

### Issue #4: LocalStorageService Provider May Not Be Ready
[lib/data/repositories/auth_repository.dart](lib/data/repositories/auth_repository.dart#L128-L140)

The provider uses `.maybeWhen()` which could return null if local storage isn't initialized yet.

---

### Issue #5: API Service No Decoder Pass
The `apiService.post()` doesn't pass a decoder, so response comes back as raw data.

---

## Fixes Applied

### Fix 1: Update UserModel to handle all fields from backend
[lib/domain/models/user_model.dart](lib/domain/models/user_model.dart)

Add `balance` field and improve fromJson parsing.

### Fix 2: Add better response validation in auth_repository.dart
[lib/data/repositories/auth_repository.dart](lib/data/repositories/auth_repository.dart#L22-L45)

Add proper null checks and clear error messages.

### Fix 3: Update api_service.dart decoder support
Ensure response parsing handles decoder properly.

### Fix 4: Add logging for debugging
Add debug prints to see actual responses from backend.

---

## Testing Before & After

### Before (Failing):
```
user@email.com : password123
→ POST /api/v1/login
← Response: {"token": "jwt...", "user": {...}}
→ Exception: Type casting error or "Invalid login response"
```

### After (Working):
```
user@email.com : password123
→ POST /api/v1/login
← Response parsed correctly
← Token saved to storage
← User data saved
← Redirected to home
```

---

## Steps to Test

1. **Start Go Backend**
   ```powershell
   cd D:\go project\avi_bd
   go run cmd/server/main.go
   ```

2. **Run Flutter App**
   - Connect to the backend on http://localhost:8080
   - Test registration with new email
   - Test login with registered email

3. **Check Logs**
   - Look for "Invalid login response" errors
   - Check type mismatch issues
   - Verify token is being saved

---

## Required Backend Check

Verify backend is returning correct structure:
```bash
curl -X POST http://localhost:8080/api/v1/register \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Test User",
    "email": "test@example.com",
    "password": "password123"
  }'
```

Should return:
```json
{
  "token": "eyJ...",
  "user": {
    "id": "...",
    "name": "Test User",
    "email": "test@example.com",
    "balance": 100,
    "created_at": "2026-04-08T10:00:00Z"
  }
}
```

---

**Status:** Analyzing and fixing...
