auth0-samples / auth0-xamarin-oidc-samples

Auth0 OIDC Client with Xamarin applications
MIT License
29 stars 38 forks source link

Support for Xamarin.Forms on Android #6

Closed gitizenme closed 6 years ago

gitizenme commented 7 years ago

We have a use case that requires integration between Auth0 and Xamarin Forms. I have implemented the iOS version per the Xamarin SDK Quickstarts and it is working well. However, I am unable to implement the Android integration using the PageRenderer (see below) technique that is recommended by Xamarin. Guidance in the area would be really helpful.

`public class Auth0LoginPageRenderer : PageRenderer { Auth0LoginPage page; Auth0Client auth0;

    public Auth0LoginPageRenderer()
    {
        var options = new Auth0ClientOptions
        {
            Domain = ApiConfig.Auth0Domain,
            ClientId = ApiConfig.Auth0ClientId,
            Activity = Context as Android.App.Activity
        };
        auth0 = new Auth0Client(options);
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
    {
        Console.WriteLine("OnElementChanged: BEGIN");
        base.OnElementChanged(e);
        if (Element == null)
        {
            return;
        }
        page = Element as Auth0LoginPage;
    }

    protected override void OnAttachedToWindow()
    {
        Console.WriteLine("OnAttachedToWindow: BEGIN");
        base.OnAttachedToWindow();
        if (auth0 != null)
        {
            Task.FromResult(Login());
        }
    }

    async Task Login()
    {
        Console.WriteLine("Login: BEGIN");
        try
        {
            var loginResult = await auth0.LoginAsync(null);

            if (loginResult.IsError)
            {
                page.LoginFailed();
                MessagingCenter.Send("Failed", "LoginPageDone");
                Console.WriteLine("User login failed!");
            }
            else
            {
                UserProfile userProfile = new UserProfile();
                userProfile.AccessToken = loginResult.AccessToken;
                userProfile.IdToken = loginResult.IdentityToken;
                userProfile.RefreshToken = loginResult.RefreshToken;
                userProfile.AuthenticationTime = loginResult.AuthenticationTime;
                userProfile.AccessTokenExpiration = loginResult.AccessTokenExpiration;

                page.LoginSuccess(userProfile);
                MessagingCenter.Send("Success", "LoginPageDone");
            }
        }
        catch (TaskCanceledException ex)
        {
            System.Diagnostics.Debug.WriteLine(nameof(Auth0LoginPageRenderer) + nameof(Login) + ": " + ex);
            page.LoginCancelled();
        }
        catch (TaskSchedulerException ex)
        {
            System.Diagnostics.Debug.WriteLine(nameof(Auth0LoginPageRenderer) + nameof(Login) + ": " + ex);
            page.LoginFailed();
        }
    }

}

`

jerriep commented 6 years ago

I am locking this issue now, as Xamarin Forms support has been added and other problems keep being discussed here.

For any new problems, open a separate issue