Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Auth (Android) - Chrome Custom Tabs Doesn't Close on Redirect

I've implemented the Xamarin.Auth sample code to authenticate with google's identity provider on Android. I'm successfully navigated to the Google login page using the device's Chrome browser where I can enter my credentials. I successfully authorize with Google but the Chrome Custom Tabs doesn't close when it redirects back to my app, i.e., I'm left looking at the google search in the chrome browser. If I close the browser I can see my app again with the user details returned from google's identity provider displayed.

Why is Chrome's custom tabs not closing on redirect from the Google identity provider, and how can I get it to close using Xamarin Forms and Xamarin.Auth?

like image 993
James B Avatar asked Jul 27 '17 12:07

James B


1 Answers

You can go back to your app if you add this code to the end of OnCreate method in the class that captures the Redirect (CustomUrlSchemeInterceptorActivity) in Xamarin.Auth example in Android

new Task(() =>{
         StartActivity(new Intent(Application.Context,typeof(MainActivity)));
     }).Start();

Where MainActivity is the name of your main Activity class in Android. To be more exact here is a complete class that you can inherit for each redirection you intercept

public class UrlSchemeInterceptor : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        try
        {
            base.OnCreate(savedInstanceState);

            // Convert Android.Net.Url to Uri
            var uri = new Uri(Intent.Data.ToString());
            new Task(() =>
            {
                var intent = new Intent(ApplicationContext, typeof(MainActivity));
                intent.AddFlags(ActivityFlags.IncludeStoppedPackages);
                intent.AddFlags(ActivityFlags.ReorderToFront);
                StartActivity(intent);

            }).Start();

            // Load redirectUrl page
            AuthenticationState.Authenticator.OnPageLoading(uri);
            Finish();

        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }

}

public class AuthenticationState
{
    public static WebAuthenticator Authenticator;
   /*This static field is used to store the object
   from OAuth1Authenticator or OAuth2Authenticator
   upon initialization in the UI (Xamarin forms or Android or iOS).
   For example:
   var authenticatorObject = new  OAuth2Authenticator (YOUR PARAMETERS);
   AuthenticationState.Authenticator = (WebAuthenticator)authenticatorObject;
    var presenter = new OAuthLoginPresenter();
        presenter.Login(authenticatorObject);
    */
}

For example in Google Case

[Activity(Label = "YOURLABEL", NoHistory = true, LaunchMode = LaunchMode.SingleTop)]
[IntentFilter( new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
    DataSchemes = new[]
    {
        "com.googleusercontent.apps.",// YOUR GOOGLE ID INVERTED
    },
    DataPaths = new[]
    {
        "/oauth2redirect",
    })]
public class GoogleUrlSchemeInterceptorActivity : UrlSchemeInterceptor { }
like image 177
Ahmed Samy Moursi Hashwa Avatar answered Nov 09 '22 15:11

Ahmed Samy Moursi Hashwa