authenticate static method

Future<Map<String, dynamic>> authenticate({
  1. String? accessToken,
  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? accessToken,
  String? code,
  String? callbackPath,
  required String? clientId,
  required String? clientSecret,
  required String redirectBase,
}) async {
  if (clientId == null || clientSecret == null) {
    throw AuthException('Facebook OAuth is not configured');
  }

  String finalAccessToken = accessToken ?? '';

  // If code is provided, exchange it for access token
  if (code != null) {
    finalAccessToken = await _exchangeCodeForToken(
      code,
      clientId,
      clientSecret,
      callbackPath != null
          ? '$redirectBase$callbackPath'
          : '$redirectBase/api/auth/facebook/callback',
    );
  }

  if (finalAccessToken.isEmpty) {
    throw AuthException('Either accessToken or code must be provided');
  }

  // Verify token and get user profile
  final profile = await _verifyTokenAndGetProfile(finalAccessToken, clientId);

  return AuthProvider.formatUserData(
    provider: 'facebook',
    providerId: profile['id'],
    email: profile['email'],
    name: profile['name'],
    picture: profile['picture']?['data']?['url'],
    rawProfile: profile,
  );
}