servicestackservicestack-auth

Authenticating user with Facebook using Servicestack (Java & Swift)


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


Solution

  • 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
                });
        }