authenticate method

  1. @override
Future<AuthenticationResult?> authenticate({
  1. required OAuthProvider provider,
  2. required Uri redirectUri,
  3. required List<String> scope,
  4. required String responseType,
  5. required String responseMode,
  6. required String? prompt,
  7. required String? codeChallenge,
  8. required String? codeChallengeMethod,
  9. required Map<String, dynamic> otherParams,
  10. required WebAuthenticationMode webMode,
})
override

Implementation

@override
Future<AuthenticationResult?> authenticate({
  required OAuthProvider provider,
  required Uri redirectUri,
  required List<String> scope,
  required String responseType,
  required String responseMode,
  required String? prompt,
  required String? codeChallenge,
  required String? codeChallengeMethod,
  required Map<String, dynamic> otherParams,
  required WebAuthenticationMode webMode,
}) async {
  final authUri = provider.getAuthUri(
    redirectUri: redirectUri,
    scope: scope,
    responseType: responseType,
    responseMode: responseMode,
    prompt: prompt,
    codeChallenge: codeChallenge,
    codeChallengeMethod: codeChallengeMethod,
    otherParams: otherParams,
  );

  final completer = Completer<Uri?>();

  webMode.when(
    sameTab: (sameTab) {
      html.window.location.href = authUri.toString();
    },
    popup: (popup) async {
      final screen = html.window.screen;
      final screenWidth = screen?.width;
      final screenHeight = screen?.height;
      final ScreenDimensions? screenDimensions;
      if (screenWidth != null && screenHeight != null) {
        screenDimensions = ScreenDimensions(
          width: screenWidth,
          height: screenHeight,
        );
      } else {
        screenDimensions = null;
      }

      final window = html.window.open(
        authUri.toString(),
        popup.windowName,
        popup.constructSpecs(screenDimensions: screenDimensions),
      );

      html.window.onMessage.first.then(
        (value) {
          final redirect = Uri.parse(value.data['redirect']);
          completer.complete(redirect);
        },
      );

      Timer.periodic(const Duration(seconds: 1), (timer) {
        if (completer.isCompleted) {
          timer.cancel();
        } else if (window.closed == true) {
          completer.complete(null);
          timer.cancel();
        }
      });
    },
  );

  final redirect = await completer.future;
  if (redirect != null) {
    return AuthenticationResult(redirect: redirect);
  } else {
    return null;
  }
}