【问题标题】:Xamarin Firebase Auth iOSXamarin Firebase 身份验证 iOS
【发布时间】: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


    【解决方案1】:

    我想通了。只想分享给遇到相同问题的人!这是几个问题。

    1. 确保您的 Apple 开发者帐户已设置并输入 在 Tools:Options:Xamarin:Apple Accounts 中进入 Visual Studio。
    2. 确保在 Visual Studio 菜单中,下拉 Debug 并单击 底部的扳手名为“[您的解决方案名称].iOS”。确保 自动配置已设置并且您从 来自您在第 1 步中所做的 Apple 开发人员设置的下拉菜单。
    3. 最后,我修改了我的代码,专注于获取和返回 用户 ID Token。

    这是最终代码,也添加了 Android 级别:

    项目级别 - 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)
            {
                var token = await auth.LoginWithEmailPassword(Email.Text, Password.Text);
                await DisplayAlert("Logged in: ", "Token: " + token, "OK");
                    if (token != "")
                {
                    ShowSuccess(token);
                }
                else
                {
                    ShowError();
                }
            }
    
            public async void ShowSuccess(string Token)
            {
                await DisplayAlert("Success!", Token, "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

    using PelletPirate.Interfaces;
    using System;
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    
    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)
            {
                var token = await auth.SignUpWithEmailPassword(Email.Text, Password.Text);
                await DisplayAlert("Token: ", token, "OK");
                if (token != "")
                {
                    ShowSuccess(token);
                }
                else
                {
                    ShowError();
                }
            }
    
            public async void ShowSuccess(string Token)
            {
                await DisplayAlert("Success!", Token, "OK");
                await Shell.Current.GoToAsync("//main");
            }
    
            private async void ShowError()
            {
                await DisplayAlert("Registration Failed", "Could not register to Pellet Pirate", "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.Threading.Tasks;
    using Xamarin.Forms;
    
    [assembly: Dependency(typeof(AuthIOS))]
    namespace PelletPirate.iOS
    {
        class AuthIOS : IAuth
        {
            public async Task<string> LoginWithEmailPassword(string email, string password)
            {
                var authDataResult = await Auth.DefaultInstance.SignInWithPasswordAsync(email, password);
                System.Diagnostics.Debug.WriteLine("In AuthIOS right after var authDataResult " + authDataResult.User.Uid + " <<-- user uid here?");
                return await authDataResult.User.GetIdTokenAsync();
            }
    
            public async Task<string> SignUpWithEmailPassword(string email, string password)
            {
                var authDataResult = await Auth.DefaultInstance.CreateUserAsync(email, password);
                System.Diagnostics.Debug.WriteLine("In AuthIOS right after var authDataResult " + authDataResult.User.Uid + " <<-- user uid here?");
                return await authDataResult.User.GetIdTokenAsync();
            }
    
        }
    
    }
    

    Android 级别 - AuthDroid.cs

    using Firebase.Auth;
    using PelletPirate.Droid;
    using PelletPirate.Interfaces;
    using System.Threading.Tasks;
    using Xamarin.Forms;
    
    [assembly: Dependency(typeof(AuthDroid))]
    namespace PelletPirate.Droid
    {
        public class AuthDroid : IAuth
        {
    
            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;
            }
    
        }
    }
    

    E N J O Y

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多