Firebase Auth on Xamarin.Forms

Kako Botasso
Firebase Developers
4 min readJun 22, 2019

One of the most common feature that we find in apps or websites is authentication. You can have access to exclusive content after you sign in on some apps, to your playlists, to your profile information and etc. A very simple way to provide this feature to your app is using Firebase Auth and I'll show you how to use it on Xamarin.Forms apps.

Resultado de imagem para firebase auth

Let's start adding to MainPage.xaml, two fields to user input his email and password and a button to complete the login. We'll also add a handler to our code behind, so we can send Firebase the credentials.

Now, let's create the logged page. Create a new page on the project called LoggedPage. We'll add a simple label just to show that authentication worked.

After the user validate his credentials on the MainPage, we pretend to send him our new page. We have to update the App.xaml.cs file, to add a Navigation element into App() method

With our pages already created, we need to import some libraries to use Firebase. Let's add Xamarin.Firebase.Core into shared project and in Android project, we must add Xamarin.Firebase.Auth and Xamarin.Firebase.Core too. After that, let's create an interface on our shared project that will be implemented on Android and iOS and will call login method

Now we have to create the implementation into Android and iOS projects. First you go to FireAuth.Android and create an implementation to our interface

In this implementation, we pass e-mail and password as parameters and call a method to authenticate the user and then we return a token. If the user does not existis, it throws an exception. To avoid break our app, add a catch to return an empty token

We’ll have to add the google-services.json to our project. After download it from Firebase console, add it to our Android project. To Xamarin find this file, we’ll have to update our FireAuth.Android.csproj by adding an ItemGroup with this file

Android is ok, now it's iOS time. we need to add a NuGet package called Xamarin.Firebase.iOS.Auth and initialize Firebase on our AppDelegate.

To Firebase work well, we need to import GoogleService-Info.plist from our console. Add it to your project and change its Build Action to Bundle Resource by clicking with right button into it.

Now let’s create an implementation to our interface, like we did on Android. Let's create a file to add the login method

We created a simple method that try to sign in using email and password and return user's token. If authentication fails for any reason, Firebase throws an exception. To avoid breaking our application, we used try/catch with an empty string in failure scenarios.

Before call our interface methods on shared project, we must go to Firebase Console, enter on Authentication > Login method and enable e-mail and password login

Enable e-mail and password login

After it, come back to Users tab and create a new user. We will use this user to test our application

Test user created

Now that we have our user created on Firebase Console, let's back to our MainPage.xaml.cs. We must use our interface to call sign in method when user clicks on the button, passing email and password typed on the input fields.

Running the app right now, you will see the login page. If you type e-mail and password as you have created on Firebase Console, you will be redirected to another page. If something goes wrong, you will see a dialog telling you that your authentication failed. If you find some problems using iOS Simulator, try to create a new emulator with iOS 9 version on it.

Hope this article have helped and hope see you soon. If you have any doubt, don’t be afraid to ask in the comments. If you liked it, recommend and share :D

You can find the code from this article on Github and also how to create new users directly from your app:

--

--