loginWithGoogle method

Future<AppUser> loginWithGoogle(
  1. String idToken, {
  2. AuthPlatform platform = AuthPlatform.web,
})

Authenticate a user using Google Sign-In with ID token

This method verifies the Google ID token and either creates a new user or logs in an existing user who registered with Google OAuth.

Throws:

  • Exception If Google Sign-In is not enabled in project settings
  • Exception If the Google ID token is invalid or expired
  • Exception If email is already registered with password authentication

Example:

// Mobile - After getting ID token from Google Sign-In SDK
final user = await db.auth.loginWithGoogle(idToken, AuthPlatform.mobile);
print('Logged in: ${user.email}');

Implementation

Future<AppUser> loginWithGoogle(
  String idToken, {
  AuthPlatform platform = AuthPlatform.web,
}) async {
  final response = await _request<Map<String, dynamic>>(
    'POST',
    '/auth-collections/google-verify',
    body: {'id_token': idToken, 'platform': platform.value},
    useDataKey: false,
  );

  _token = response['access_token'];
  await setToken(_token!);

  _user = AppUser.fromJson(response['user']);
  await setUser(_user!);

  // Trigger login callback
  _callbacks.onLogin?.call(_user!, _token!);

  return _user!;
}