1

I have a problem where if the app (in debug mode) is either freshly opened or freshly installed. If I try to sign in with my provider, it opens the external browser (chrome for example) and it doesn't redirect me back to the application but if I go back to the application, the data reflects. Now here's another weird behavior, if I run the application again, it now opens chrome custom tabs and it handles the redirect correctly. It usually happens with the select_account prompt, otherwise in login prompt, it doesn't and the reason I chose select_account is because I am preventing the app from taking the current session that is logged into the browser.

Portion of my AuthService.dart

Future<UserModel?> signInWithMicrosoft() async {
    try {
      final microsoftProvider = MicrosoftAuthProvider();
      microsoftProvider.setCustomParameters({
        'tenant': '',
        'prompt': 'select_account'
      });

      final userCredential = await _auth.signInWithProvider(microsoftProvider);

      if (userCredential.user != null) {
       String? accessToken;
         if (userCredential.credential != null) {
         
          final oauthCredential = userCredential.credential as dynamic;
          accessToken = oauthCredential.accessToken;

        } else {
          debugPrint("Usercredential is null");
        }

        // 1. Create user immediately without the photo URL. This is fast.
        final user = await _createOrUpdateUser(
          firebaseUser: userCredential.user!,
          principalName: userCredential.additionalUserInfo?.profile?['upn'] as String?,
        );

        // 2. Fetch the photo in the background. Don't await it.
        if (accessToken != null) {
          
          _graphService.getProfilePhotoUrl(
            accessToken: accessToken,
            userId: userCredential.user!.uid,
          ).then((photoUrl) {
            if (photoUrl != null) {
              // 3. Once fetched, update the user's profile in the database.
              _updateUserPhotoUrl(uid: userCredential.user!.uid, photoUrl: photoUrl);
            }
          }).catchError((e){
             print('Failed to fetch and update profile photo in background: $e');
          });
        }
        
        return user; // Return the user immediately.
      }
      return null;
    } on FirebaseAuthException catch (e) {
      if (e.code == 'web-context-cancelled') {
        print('Microsoft sign in cancelled by user.');
        return null;
      }
      rethrow;
    } catch (e) {
      throw Exception('Microsoft sign in failed: $e');
    }
  }

Portion of my Main.dart

void main() async {
  // Ensure Flutter bindings are initialized
  WidgetsFlutterBinding.ensureInitialized();

    await SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]);

  if(Platform.isAndroid){
    warmup_customtabs();
  }
  // Initialize Firebase
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);

  
  
  // Wrap the entire app in a ProviderScope for Riverpod state management
  runApp(const ProviderScope(child: MyApp()));
}
 Future <void> warmup_customtabs() async{
   try {
    // Try the session-based warmup first (more thorough)
    await warmupCustomTabs();
    print('Custom tabs session warmup successful');
  } catch (e) {
    print('Custom tabs session warmup failed: $e');
    
  }

 }

Actually tried implementing warmup service (which is shown in the code above) for custom tabs but to no avail it still happens.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.