Integrating Firebase Auth in Xamarin.Forms is very easy and basic authentication flow implementation can be achieved under 20 lines of code. There is more work with settings than code writing. In this article we will:
- Configure Firebase app
- Create Xamarin.Forms application to authenticate users via Firebase Auth
- Create a .NET Core WEB API project to validate Firebase Auth token and return simple data
Firebase Configuration
Firebase has simple and intuitive UI and it is very hard to get lost there, thumbs up for the great work Google! In order to achieve our goal we have to do few simple steps:
- Create a new project
- Add application for Android
- Download google-services.json
- Add application for iOS
- Download GoogleService-Info.plist
- Add application for Android
- Open Authentication section
- Switch to “Sign-in method” tab
- Enable “Email/Password”
- Switch to “Users” tab
- Add a new user
- Switch to “Sign-in method” tab
At this point we should be done with the Firebase configuration and should have all the necessary files in place.
Xamarin.Forms Configuration
Android
- Add
Xamarin.Firebase.Auth
NuGet package - Import “google-services.json” and set the building action to “GoogleServicesJson”
- In other words the
.csproj
should contain the next line: <GoogleServicesJson Include=”google-services.json” />
- In other words the
- Make sure that your package name is identical to the package name inside “google-services.json”
- Add the next line of code in the MainActivity.cs OnCreate before LoadApplication:
FirebaseApp.InitializeApp(Application.Context);
iOS
- Add
Xamarin.Firebase.iOS.Auth
NuGet package to iOS project - Import “GoogleService-Info.plist” and set the building action to “BundleResource”
- Make sure that your bundle identifier is identical to the bundle identifier inside “GoogleService-Info.plist”
- Add the next line of code in the AppDelegate.cs FinishedLaunching before LoadApplication:
Firebase.Core.App.Configure();
- Please note that if you want to test on the iOS simulator you will have to do one extra step as described here due to a bug.
Xamarin.Forms
Since there are only platform specific Xamarin.Firebase
NuGet packages, we will have to create a simple abstraction layer that will look like this:
Each platform will have to implement this interface separately. Android implementation:
iOS implementation:
The implementations are almost identical and in both cases are 2-3 lines of code to authenticate a user and return a token.
After obtaining a token we could use it to access our protected API:
.NET Core WEB API Configuration
Now we want to validate the Firebase token we obtained in our Xamarin.Forms app. For this, we have to setup and enable Authentication
in Startup.cs
:
The code is self explanatory, but if you would like to dive into details there is a great article explaining the code above.
Conclusion
Integrating Firebase to Xamarin.Forms and .NET Core WEB API is simple and fun. While this example was pretty ordinary you could extend it to support other Firebase Auth flows as a homework.
Code example is available on github.