【发布时间】:2020-06-26 00:22:39
【问题描述】:
我正在努力让 Xamarin Firebase Auth on iOS return 值正确编码以处理我的成功/失败逻辑适用于 SignInWithEmailPasswordAsync(email, password) 和 CreateUserAsync。我确定问题出在我的var user = 和AuthDataResult signUpTask = 代码中,因为 CreateUser 和 SignIn 方法实际上正在将用户添加/登录到 firebase,因为我可以在控制台中看到它们......如果我手动注册一个firebase 中的用户,登录日期是空白的......当我通过我的 iOS 应用程序登录他们时,我看到那里出现了一个日期戳。如果我注册了一个新用户,他们会出现在 Firebase 身份验证控制台中……一切都很好。它只是将我的 AuthIOS.cs 文件的返回状态返回到调用登录和注册页面,以便我可以正确处理成功或失败。任何帮助或将不胜感激。这是我的代码:
项目级别 - IAuth.cs
using System.Threading.Tasks;
namespace PelletPirate.Interfaces {
public interface IAuth
{
Task<string> LoginWithEmailPassword(string email, string password);
Task<string> SignUpWithEmailPassword(string email, string password);
} }
项目级别 - LoginPage.xaml.cs
using PelletPirate.Interfaces;
using System;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace PelletPirate.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[DesignTimeVisible(true)]
public partial class LoginPage : ContentPage
{
IAuth auth;
public LoginPage()
{
InitializeComponent();
auth = DependencyService.Get<IAuth>();
}
private async void BtnLogin_Clicked(object sender, EventArgs e)
{
string uid = await auth.LoginWithEmailPassword(Email.Text, Password.Text);
await DisplayAlert("Do we have a user? ", uid, "Ok");
if (uid != "")
{
ShowSuccess(uid);
}
else
{
ShowError();
}
}
public async void ShowSuccess(string UID)
{
await DisplayAlert("Success!", UID, "OK");
await Shell.Current.GoToAsync("//loggedin");
}
private async void ShowError()
{
await DisplayAlert("Authentication Failed", "E-mail or password are incorrect. Try again!", "OK");
}
private async void BtnRegister_Clicked(object sender, EventArgs e)
{
await Shell.Current.GoToAsync("//register");
}
private async void BtnGoogleLogin_Clicked(object sender, EventArgs e)
{
await DisplayAlert("Alert", "You need to delete this ADD button stuff", "OK");
}
}
}
项目级别 - RegistrationPage.xaml.cs
namespace PelletPirate.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class RegisterPage : ContentPage
{
IAuth auth;
public RegisterPage()
{
InitializeComponent();
auth = DependencyService.Get<IAuth>();
}
async void BtnRegister_Clicked(object sender, EventArgs e)
{
string uid = await auth.SignUpWithEmailPassword(Email.Text, Password.Text);
await DisplayAlert("Uid: ", uid, "OK");
if (uid != "")
{
await DisplayAlert("Success", "Welcome to Pellet Pirate!", "OK");
await DisplayAlert("User", uid, "OK");
await Shell.Current.GoToAsync("//main");
}
else
{
await DisplayAlert("Sign Up Failed", "Something went wrong. Try again!", "OK");
}
}
private async void BtnCancel_Clicked(object sender, EventArgs e)
{
await Shell.Current.GoToAsync("//login");
}
}
}
iOS 级别 - AuthIOS.cs
using Firebase.Auth;
using PelletPirate.Interfaces;
using PelletPirate.iOS;
using System;
using System.Threading.Tasks;
using Xamarin.Forms;
[assembly: Dependency(typeof(AuthIOS))]
namespace PelletPirate.iOS
{
public class AuthIOS : IAuth
{
public async Task<string> LoginWithEmailPassword(string email, string password)
{
try
{
var user = await Auth.DefaultInstance.SignInWithPasswordAsync(email, password);
return await user.User.GetIdTokenAsync();
}
catch (Exception)
{
return "";
}
}
public async Task<string> SignUpWithEmailPassword(string email, string password)
{
try
{
AuthDataResult signUpTask = await Auth.DefaultInstance.CreateUserAsync(email, password);
return signUpTask.User.Uid;
}
catch (Exception)
{
return "";
}
}
}
}
** 再次感谢您的指导 **
【问题讨论】:
标签: firebase xamarin.forms firebase-authentication