1

I'm using servicestack client in my Android(Java) and iOS(Swift) applications but I need to authenticate users with facebook and google, Do you know if exists a module like Xamarin.Auth https://github.com/xamarin/Xamarin.Auth but for native applications?

Thank you

Lesther Vega
  • 528
  • 3
  • 14

1 Answers1

2

Have a look at AndroidJavaChat which is a port of the C# Xamarin.Android Chat it explains a couple of different ways of Authenticating with Facebook using their SDK and leveraging the new support for Authenticating using an AccessToken in ServiceStack v4.5.7+ that's now available on MyGet.

First follow this getting started guide to install Facebook's SDK.

Then checkout the implementation for LoginButtonActivity which authenticates using the Facebook Login button which you can define in your Layout.xml with:

<com.facebook.login.widget.LoginButton
    android:id="@+id/btnFacebookLogin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp"
    android:layout_marginBottom="30dp" />

Signing in with Facebook Login Button

To use Facebook's SDK your AppId needs to be defined in your AndroidManifest.xml:

<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>

Using the ApplicationId for your Facebook App from your App created in https://developers.facebook.com/apps

Then you can Authenticate with Facebook Login button by registering a callback on the LoginButton. After that it's simply a matter of handling the Facebook onSuccess(), onCancel() and onError() callbacks. When the onSuccess() callback is fired it means the User has successfully Signed into our Android App, we then need to Authenticate with our ServiceStack Chat Server by making an Authenticated request using the User's Facebook AccessToken, e.g:

facebookCallback = CallbackManager.Factory.create();
btnFacebookLogin = (LoginButton) findViewById(R.id.btnFacebookLogin);
btnFacebookLogin.setReadPermissions("email"); // Ask user for permission to view access email address
btnFacebookLogin.registerCallback(facebookCallback, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        UiHelpers.setStatus(txtStatus, "Local facebook sign-in successful, signing into server...");

        App.get().getServiceClient().postAsync(new dtos.Authenticate()
            .setProvider("facebook")
            .setAccessToken(loginResult.getAccessToken().getToken())
            .setRememberMe(true),
            r -> {
                UiHelpers.setStatus(txtStatus, "Server facebook sign-in successful, opening chat...");
                Intent intent = new Intent(activity, MainActivity.class);
                stopProgressBar();
                startActivity(intent);
            },
            error -> {
                UiHelpers.setStatusError(txtStatus, "Server facebook sign-in failed", error);
                stopProgressBar();
            });
    }

    @Override
    public void onCancel() {
        stopProgressBar();
    }

    @Override
    public void onError(FacebookException exception) {
        Log.e(exception);
        stopProgressBar();
    }
});

Once the User has authenticated with the ServiceStack Server, the Authenticated Session Cookies are configured on the Service Client which you can now use to make authenticated requests.

If you instead want to login using your own Custom Image for your login button, follow the steps in Signing in with Custom Facebook Image Button.

Automatically Sign-In previously Signed In Users

After the User has successfully Authenticated with Facebook the first time you can access their saved token from Facebook's AccessToken.getCurrentAccessToken() to automatically sign them in for future restarts, e.g:

AccessToken facebookAccessToken = AccessToken.getCurrentAccessToken();
    if (facebookAccessToken != null){
        client.postAsync(dtos.Authenticate()
            .setProvider("facebook")
            .setAccessToken(facebookAccessToken.getToken())
            .setRememberMe(true),
            r -> {
                //User has signed using Saved Access Token
            });
    }
mythz
  • 141,670
  • 29
  • 246
  • 390
  • Thank you, When is the estimate date for release ServiceStack v4.5.7 in nuget? Can I use it version in production? – Lesther Vega Mar 25 '17 at 22:48
  • @LestherVega odd numbers are only on MyGet, even numbers are on NuGet. Hoping for v4.5.8 to be released on NuGet next week but you can use v4.5.7 in production (uses same CI and passes same test suite as NuGet builds) by [adding the MyGet feed](http://docs.servicestack.net/myget). – mythz Mar 25 '17 at 22:56
  • it works with **facebook** but with **googleoauth** provider I'm getting the error: **System.StackOverflowException was unhandled Message: An unhandled exception of type 'System.StackOverflowException' occurred in ServiceStack.Authentication.OAuth2.dll** any idea? – Lesther Vega Mar 26 '17 at 21:10
  • I'm sending the access_token for googleapis like this http://stackoverflow.com/questions/33998335/how-to-get-access-token-after-user-is-signed-in-from-gmail-in-android/ – Lesther Vega Mar 26 '17 at 21:16
  • 1
    @LestherVega The StackOverflowException should now be [fixed with this commit](https://github.com/ServiceStack/ServiceStack/commit/6ac40152153597314fcb59c2ca3947b4bb258415) which is now on MyGet that you can [redownload after clearing your NuGet cache](http://docs.servicestack.net/myget#redownloading-myget-packages). – mythz Mar 27 '17 at 06:42