0
votes

I am not certain how to ask this question or even if I am heading in the correct direction. I am trying to implement Auth0 into my cross-platform Xamarin.Forms app. I have been successful when building a pure Android, or pure iOS app, but with Xamarin Forms, I am not sure how to have my main project button target a function in either the Android or iOS.

For example:

[MainPage.xaml]

<ToolbarItem Text="Login" Clicked="Login_Clicked" />

[MainPage.xaml.cs]

    using Auth0.OidcClient;
    private async void Login_Clicked(object sender, EventArgs e)
    {
        // This part is in a different file but for the sake of illustration
        _auth0Client = new Auth0Client(new Auth0ClientOptions
        {
            Domain = AuthenticationConfig.Domain,
            ClientId = AuthenticationConfig.ClientId
        });

        var loginResult = await client.LoginAsync();
    }

Note: the Auth0Client bit will 100% error saying that it is missing an assembly reference (which I think is due to the Oidc client only being functional on Android and iOS directly).

So the solution, I believe, would be to have an Android function, in the Android project respond to that button, but I do not know how to do that, and I cannot find any tutorial on how / what / where I should be looking, as the actual layout is created in the main project, and the Android and iOS packages are inheriting from it.

private async void Login_Clicked(object sender, EventArgs e)
{
    FancyAndroidFunction client = new FancyAndroidFunction();
}

Or even better, have the MainActivity.cs target that button specifically (Which I am not sure how to do either).

I have gone through this tutorial and it 100% works but requires building only in Android or iOS:

https://auth0.com/docs/quickstart/native/xamarin

Also I have looked at this tutorial, but it builds an embedded login rather than a universal login, which is frowned upon now.

https://auth0.com/blog/xamarin-authentication-and-cross-platform-app-development/

1
There are a couple of ways to get to platform-specific UI code when using cross-platform controls. I haven't implemented Auth0 myself, but one of these should give you the functionality you need: use a custom renderer for the login buttons on each platform to get the native button; or implement the login natively and embed the native views in the cross-platform code: blog.xamarin.com/adding-bindable-native-views-directly-to-xaml. - DavidS
What you need to do is configure dependency services and call the native methods then get the response and push it back to your XF page - FreakyAli

1 Answers

0
votes

https://github.com/officialdoniald/FileStoringWithDependency this is an easy implementation of Dependency Service. I created a function of each platform an call it from the Xamarin.Froms pages. You have to do the same with your project. You have to create a class on each platform and call it through DependencyService.