xamarinmauimaui-community-toolkitwebauthenticator

Why Navigation after WebAuthenticator in MAUI doesn't work


I'm building application on .NET MAUI what use "WebAuthenticator" to authenticate with Google. The Problem is that, after successful login, I'm not able to navigate to another page.

    [RelayCommand]
    private async Task OAuthLogin(string authProvider)
    {
        try
        {
            WebAuthenticatorResult authResult = await WebAuthenticator.AuthenticateAsync(
             new Uri($"https://ee33-31-13-238-188.ngrok-free.app/auth/{authProvider.ToLower()}/login"),
             new Uri("myapp://"));

            LoginResultModel result = JsonSerializer.Deserialize<LoginResultModel>(Uri.UnescapeDataString(authResult.Get("loginResult")));

            if (result.Code == (int)GeneralStatusEnum.Success)
            {
                App.UserId = result.User.Id;
                App.JwtToken = result.Jwt;
                App.UserName = result.User.FirstName;
                App.Email = result.User.Email;


                MainThread.BeginInvokeOnMainThread(async () =>
                {
                    await Navigation.PushAsync<HomePage>();

                });

                //await Navigation.PushAsync<HomePage>();

            }
            else
            {
                if (result.Message == MessageConstants.Unsuccess)
                {
                    throw new Exception(MessageConstants.GeneralError);
                }
                else
                {
                    throw new Exception(result.Message);
                }
            }
        }
        catch (TaskCanceledException ex)
        {
            await App.DisplayAlert(App.MESSAGE_HEADER_ERROR, ex.Message, App.MESSAGE_OK);
        }
    }

Tested to invoke the navigation on "Main Thread" but it still don't work, I can see the "Navigation" is not null.

 MainThread.BeginInvokeOnMainThread(async () =>
    {
         await Navigation.PushAsync<HomePage>();

    });

Second test what I did was, to add "await Task.Delay(100);" and this way, the Navigation works as exptected. If someone have idea what can cause the Navigation to doesn't work, please share.

This is my configuration in "App.xaml.cs"

LoginPageViewModel vm = new LoginPageViewModel(authSer, oAuthSer);
var page = new LoginPage(vm);
MainPage = new NavigationPage(page);

I'm not getting any errors, when debugging, i saw that code what Navigates to "Home Page" is invoked, before the browser to be completely closed.


Solution

  • The problem was in some of these packages versions:

        <PackageReference Include="CommunityToolkit.Maui" Version="7.0.1" />
        <PackageReference Include="CommunityToolkit.Maui.Core" Version="7.0.1" />
        <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
        <PackageReference Include="Microsoft.Maui.Controls" Version="8.0.7" />
        <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.7" />
        <PackageReference Include="Microsoft.Maui.Essentials" Version="8.0.7" />
    

    I updated them all to the latest at once, this is the reason why I'm not sure exactly at which is the problem.