authenticate static method

Future<Map<String, dynamic>> authenticate({
  1. String? idToken,
  2. String? code,
  3. String? callbackPath,
  4. required String? clientId,
  5. required String? clientSecret,
  6. required String redirectBase,
})

Implementation

static Future<Map<String, dynamic>> authenticate({
  String? idToken,
  String? code,
  String? callbackPath,
  required String? clientId,
  required String? clientSecret,
  required String redirectBase,
}) async {
  if (clientId == null || clientSecret == null) {
    throw AuthException('Google OAuth is not configured');
  }

  Map<String, dynamic> profile;

  if (idToken != null) {
    profile = await verifyIdToken(idToken, clientId: clientId);
  } else if (code != null) {
    if (callbackPath == null) {
      throw AuthException('callbackPath is required when using code');
    }

    final tokens = await _exchangeCodeForToken(
      code,
      clientId,
      clientSecret,
      '$redirectBase$callbackPath',
    );

    final idTokenFromGoogle = tokens['id_token'];
    if (idTokenFromGoogle != null) {
      profile = await verifyIdToken(idTokenFromGoogle as String,
          clientId: clientId);
    } else {
      throw AuthException('No ID token received from Google');
    }
  } else {
    throw AuthException('Either idToken or code must be provided');
  }

  return AuthProvider.formatUserData(
    provider: 'google',
    providerId: profile['sub'],
    email: profile['email'],
    name: profile['name'],
    picture: profile['picture'],
    rawProfile: profile,
  );
}