updateUser method

Future<AppUser> updateUser({
  1. Map<String, dynamic>? data,
  2. String? email,
  3. String? password,
})

Update the current user's profile data

Example:

await db.auth.updateUser(
  data: {
    'bio': 'Updated bio',
    'website': 'https://example.com'
  }
);

Implementation

Future<AppUser> updateUser({
  Map<String, dynamic>? data,
  String? email,
  String? password,
}) async {
  if (_token == null) {
    throw Exception('User is not authenticated');
  }

  // Build request body by excluding null values
  final Map<String, dynamic> body = {};
  if (data != null) {
    final currentData = _user?.data ?? {};
    body['data'] = {...currentData, ...data};
  }
  if (email != null) body['email'] = email;
  if (password != null) body['password'] = password;

  final response = await _request<Map<String, dynamic>>(
    'PATCH',
    '/auth-collections/user',
    body: body,
    useDataKey: false,
  );

  _user = AppUser.fromJson(response);
  await setUser(_user!);

  // Trigger user update callback
  _callbacks.onUserUpdate?.call(_user!);

  return _user!;
}