Are you one of those guys who tried to use the FirebaseUI library and got stuck with the built-in authentication dialog because it doesn’t allows you to use your own layout?
Or did you tried to integrate Facebook, Twitter and Google sign-in with your backend without too much success?
Then the solution to your problem is the SocialAuth library.
Since Facebook has announced that its backend as a service, Parse, will be retired from the market, I have been investigating the Google alternative, Firebase. It looks very promising and I have already adopted it successfully in some of my projects.
In the previous blog post, I have presented a simple app which uses a Backend Adapter interface implemented with Firebase. As the project of that blog post shows, it’s very easy to get started with the basic “read” and “save” operations. The Firebase library does a great job in keeping your data synced with the could in a transparent, reliable and super fast way.
However, things get complicated when you start to deal with the authentication flow (or at least I don’t remember that it was so complicated with Parse). The Firebase team has released the FirebaseUI library to facilitate the integration with the social media authentication flow, but it has two main limitations:
- You have to use the fixed authentication dialog provided by the BaseActivity(as showed below), which means that you cannot use your own buttons or custom views;
- The authentication information and the authenticated firebase instance are very coupled to the Activity which showed the dialog above, therefore, you cannot easily transfer those information from one activity to another.
In conclusion, if you want to make a multi-activity app, in which there are at least a LoginActivity and a following Activity for logged-in users, and if you also want to decouple your app from the specific backend implementation to avoid what is just happening with Parse, then the FirebaseUI library doesn’t really help you.
On the contrary, The SocialAuth library is my solution to the problem.
Social Authentication Library
SocialAuth is open-source and is partially inspired on the Firebase-UI library.
The followings are the main features of this library:
- it provides an authentication service which you can configure with the social media provider of your preference (Facebook, Twitter and Google are already implemented in the library);
- it allows you to bind your own views to each provider login action;
- it can be instantiated in the Application class, so that you can access the authentication information in the whole app;
- it’s independent from the specific backend that you decide to use because it uses a BackendAdapter interface in case you need to authenticate also with a backend which requires the token returned by the social media provider.
The following diagram illustrate those features.
The Social Auth library is available on jCenter, therefore, it can easily be imported into your app with the following gradle dependency:
To start using it, just create a new instance and add one or more social media provider:
mAuthService = new AuthService() .enableAuthProvider(googleAuthProvider) .enableAuthProvider(facebookAuthProvider) .enableAuthProvider(twitterAuthProvider);
You can create social media providers in this way:
/** * Create TWITTER Authentication Provider */ OAuthTokenHandler twitterOAuthTokenHandler = new OAuthTokenHandler(AuthProviderType.TWITTER, mBackendAdapter); AuthProvider twitterAuthProvider = new TwitterAuthProvider.Builder() .activity(this) .signInView(mButtonSignInWithTwitter) .oAuthTokenHandler(twitterOAuthTokenHandler) .build(); /** * Create FACEBOOK Authentication Provider */ OAuthTokenHandler facebookOAuthTokenHandler = new OAuthTokenHandler(AuthProviderType.FACEBOOK, mBackendAdapter); AuthProvider facebookAuthProvider = new FacebookAuthProvider.Builder() .activity(this) .signInView(mButtonSignInWithFacebook) .oAuthTokenHandler(facebookOAuthTokenHandler) .build(); /** * Create GOOGLE Authentication Provider */ OAuthTokenHandler googleOAuthTokenHandler = new OAuthTokenHandler(AuthProviderType.GOOGLE, mBackendAdapter); GoogleApiClient.ConnectionCallbacks googleConnectionCallback = getGoogleConnectionCallback(); GoogleApiClient.OnConnectionFailedListener googleOnConnectionFailedListener = getGoogleOnConnectionFailedListener(); AuthProvider googleAuthProvider = new GoogleAuthProvider.Builder() .activity(this) .signInView(mButtonSignInWithGoogle) .oAuthTokenHandler(googleOAuthTokenHandler) .connectionCallback(googleConnectionCallback) .onConnectionFailedListener(googleOnConnectionFailedListener) .build();
As you can see, each authentication provider can be built with a View on which will be automatically associated the “Sign In” action.
When the authentication with the specific provider is completed successfully, one of the methods of the BackendAdapter interface will be invoked. You can use them to trigger UI changes or add a further step of authentication by using the returned OAuth token.
More information on how to use it can be found here.
In this article I have first explained the current limitations of the FirebaseUI and then I have presented my solution to the problem with the open-source SocialAuth library. This library allows you to easily integrate your app with any backend and any social authentication provider. For my sample project, the current features of the library and of the Backend Adapter were enough, however, I can imagine that you may have different needs and would like a more tailored API for your application. For that, feel free to fork this project and contribute to it. Any suggestion is appreciated.