使用带有.net标准的Xamarin.Forms(VS2017)进行Firebase电子邮件/密码身份验证

时间:2018-04-02 08:26:32

标签: firebase xamarin.forms firebase-authentication cross-platform

Firebase电子邮件/密码验证是否可以使用Common Code .net标准的Android和IOS公共代码进行开发? 我有.XAML格式的设计简单登录页面,但我没有找到任何与Firebase auth与带有.net标准代码共享的Xamarin.Forms集成相关的示例。 如果这种策略可行,可以为我提供样本集成,将有助于进一步理解。

2 个答案:

答案 0 :(得分:3)

是的,这绝对是可能的。
由于只有平台特定的Xamarin.Firebase NuGet包,我们必须创建一个简单的抽象层,如下所示:

public interface IFirebaseAuthenticator
{
    Task<string> LoginWithEmailPassword(string email, string password);
}

每个平台都必须单独实现此接口。 Android实施:

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实施:

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

现在您可以在XF级别上使用IFirebaseAuthenticator。有关详细信息,请查看a detailed article about it,其源代码可用here

答案 1 :(得分:0)

要为Firebase Auth实施通用代码,您需要在Interface中创建一个Shared Code,然后创建特定于平台的ClassesMethods

如文档中所述,您将需要使用DependencyService来调用Shared Code中的方法。

第一:创建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);
}

第二:创建特定于平台的方法

  

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

实施共享代码

    private async void Signin_Clicked(object sender, EventArgs e)
            {
                var token = await DependencyService.Get<IFirebaseAuthenticator>().SignupWithEmailPassword("mohaaosman12@gmail.com", "admin12345");

                await DisplayAlert("Logged in", "Token: " + token, "Ok");
            }

 private async void Signup_Clicked(object sender, EventArgs e)
        {
            var token = await DependencyService.Get<IFirebaseAuthenticator>().SignupWithEmailPassword("mohaaosman13@gmail.com", "admin12345");

            await DisplayAlert("Logged in", "Token: " + token, "Ok");
        }
  

参考

Firebase Auth on iOS

Xamarin.Forms DependencyService

  

有用的链接

Add Email Authentication To Xamarin.Android App Using Firebase - Part One

Add Email Authentication To Xamarin.Android App Using Firebase - Part Two