loginFor method

Future<String?> loginFor(
  1. BuildContext context, {
  2. String hint = '',
  3. String idpHint = "",
  4. String emailHint = "",
  5. String prompt = "login",
})

Returns the callback URL when auth completes (e.g. on iOS from plugin). Caller (example app main) should handle it like app_links.

Implementation

Future<String?> loginFor(BuildContext context, {String hint = '', String idpHint = "", String emailHint = "", String prompt = "login"}) async {
  user_flow = idpHint.isNotEmpty ? idpHint : hint;
  final encodedRedirectUri = Uri.encodeComponent(redirectUri);
  encodedScope = Uri.encodeComponent(scope);

  // 🔐 PKCE
   _codeVerifier = generateCodeVerifier();
  final codeChallenge = generateCodeChallenge(_codeVerifier!);

  final authUrl =
      "https://globalinchexternal.ciamlogin.com/$tenantId/oauth2/v2.0/authorize"
      "?client_id=$clientId"
      "&response_type=code"
      "&redirect_uri=$encodedRedirectUri"
      "&scope=$encodedScope"
       "&response_mode=query"
      "&prompt=$prompt"
      "${idpHint.isNotEmpty ? "&idp_hint=$idpHint" : ""}"
      "&code_challenge_method=S256"
      "${emailHint.isNotEmpty ? "&login_hint=${Uri.encodeComponent(emailHint)}" : ""}"
      "&code_challenge=$codeChallenge";


  print("Auth URL: $authUrl");
  try {
    // Return the callback URL to the caller (example app main) so all handling happens in one place (like Android).
    final String? resultUrl = await FlutterWebAuth2.authenticate(
      url: authUrl,
      callbackUrlScheme: Uri.parse(redirectUri).scheme,
      options: const FlutterWebAuth2Options(useWebview: false),
    );
    if (resultUrl != null && resultUrl.isNotEmpty && Uri.parse(resultUrl).queryParameters['code'] != null) {
      return resultUrl;
    }
  } catch (e) {
    if (e is PlatformException && e.code == 'CANCELED') {
      print('Login canceled by user');
    } else {
      print('Login error: $e');
    }
  }
  return null;
}