Reputation: 404
Is Firebase Email/Password Authentication is possible to develop with common code for Android and IOS with Common Code .net Standards? I have Designed Simple Login page with .XAML format but i dont find any samples related to integration of Firebase auth with Xamarin.Forms with .net standard code share. If this strategy is possible can provide me with sample integration will be helpful for further understanding.
Upvotes: 2
Views: 4291
Reputation: 138
To Implement a common code for Firebase Auth you need to create an Interface
in your Shared Code
, then create a Platform specific Classes
and Methods
.
As stated in the Documentation you'll need to use DependencyService
to call on the methods from Shared Code
.
First: Create the Interface
public interface IFirebaseAuthenticator
{
/// <summary>
/// Login / Signup with email and password.
/// </summary>
/// <returns>OAuth token</returns>
/// <param name="email">Email</param>
/// <param name="password">Password</param>
Task<string> LoginWithEmailPassword(string email, string password);
Task<string> SignupWithEmailPassword(string email, string password);
}
Second: Create Platform Specific Methods
IOS
[assembly: Dependency(typeof(OFIrebaseAuthClass))]
namespace MyFirebaseAuth.iOS.Services
{
class OFIrebaseAuthClass : IFirebaseAuthenticator
{
public async Task<string> LoginWithEmailPassword(string email, string password)
{
var authDataResult = await Auth.DefaultInstance.SignInWithPasswordAsync(
email,
password);
return await authDataResult.User.GetIdTokenAsync();
}
public Task<string> SignupWithEmailPassword(string email, string password)
{
var authDataResult = await Auth.DefaultInstance.CreateUserAsync(
email,
password);
return await authDataResult.User.GetIdTokenAsync();
}
}
}
Android
[assembly: Dependency (typeof (AFirebaseAuthClass))]
namespace MyFirebaseAuth.Droid
{
class AFirebaseAuthClass : IFirebaseAuthenticator
{
public async Task<string> LoginWithEmailPassword(string email, string password)
{
var user = await FirebaseAuth.Instance.SignInWithEmailAndPasswordAsync(email, password);
var token = await user.User.GetIdTokenAsync(false);
return token.Token;
}
public async Task<string> SignupWithEmailPassword(string email, string password)
{
var user = await FirebaseAuth.Instance.CreateUserWithEmailAndPasswordAsync(email, password);
var token = await user.User.GetIdTokenAsync(false);
return token.Token;
}
}
}
Implementing in Shared Code
private async void Signin_Clicked(object sender, EventArgs e)
{
var token = await DependencyService.Get<IFirebaseAuthenticator>().SignupWithEmailPassword("[email protected]", "admin12345");
await DisplayAlert("Logged in", "Token: " + token, "Ok");
}
private async void Signup_Clicked(object sender, EventArgs e)
{
var token = await DependencyService.Get<IFirebaseAuthenticator>().SignupWithEmailPassword("[email protected]", "admin12345");
await DisplayAlert("Logged in", "Token: " + token, "Ok");
}
References
Xamarin.Forms DependencyService
Useful Links
Add Email Authentication To Xamarin.Android App Using Firebase - Part One
Add Email Authentication To Xamarin.Android App Using Firebase - Part Two
Upvotes: 2
Reputation: 12179
Yes it is definitely possible possible.
Since there are only platform specific Xamarin.Firebase NuGet packages, we will have to create a simple abstraction layer that will look like this:
public interface IFirebaseAuthenticator
{
Task<string> LoginWithEmailPassword(string email, string password);
}
Each platform will have to implement this interface separately. Android implementation:
public class FirebaseAuthenticator : IFirebaseAuthenticator
{
public async Task<string> LoginWithEmailPassword(string email, string password)
{
var user = await FirebaseAuth.Instance.
SignInWithEmailAndPasswordAsync(email, password);
var token = await user.User.GetIdTokenAsync(false);
return token.Token;
}
}
iOS implementation:
public class FirebaseAuthenticator : IFirebaseAuthenticator
{
public async Task<string> LoginWithEmailPassword(string email, string password)
{
var user = await Auth.DefaultInstance.SignInAsync(email, password);
return await user.GetIdTokenAsync();
}
}
Now you can simply use IFirebaseAuthenticator
on the XF level. For more details you can check a detailed article about it and its source code is available here.
Upvotes: 5