Cra sh приложение xamarin.forms при входе / регистрации, с Firebase - PullRequest
1 голос
/ 28 апреля 2020

Я видел NullReference, когда я нажимал на кнопку Войти или Зарегистрироваться, пожалуйста, помогите мне. Моё приложение Cra sh при входе / регистрации, с firebase. Я полностью следую официальному руководству Firebase.

Это код входа в систему .cs

namespace yourActivity
{
    // Learn more about making custom code visible in the Xamarin.Forms      previewer
    // by visiting https://aka.ms/xamarinforms-previewer

    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        IFirebaseAuthenticator auth;
        public MainPage()
        {

            InitializeComponent();
            auth = DependencyService.Get<IFirebaseAuthenticator>(); 
        }

        async void LoginClicked(object sender, EventArgs e)
        {
            var token = await auth.LoginWithEmailPassword(EmailInput.Text, PasswordInput.Text);
            if (token != "")
                await Navigation.PushAsync(new TabbedPage1());
            else
                ShowError();
        }

        async void CreateAccountClicked(object sender, EventArgs e)
        {
            await Navigation.PushAsync(new CreateAccountPage());
        }

        private async void ShowError()
        {
            await DisplayAlert("Ops!", "E-mail o password errate. Ritenta o Registrati!", "OK");
        }
    }
}

Это код входа в систему .xaml

<ContentPage  x:Class="yourActivity.MainPage">
    <StackLayout VerticalOptions="Center Margin="20">
        <Label
            Text="yourActivity" HorizontalOptions="Center" TextColor="#4682B4"
            FontSize="35"
            Margin="0, 20"/>
         <Entry
            Placeholder="E-mail" Keyboard="Email" x:Name="EmailInput"/>
         <Entry
            Placeholder="Password"
            IsPassword="true"
            x:Name="PasswordInput"/>
           <Button 
                Text="Login" 
                Clicked="LoginClicked" 
                Margin="60, 40" 
                BackgroundColor="#4682B4" 
                TextColor="White"/>

        <Button
            Text="Crea Account"
            Clicked="CreateAccountClicked"
            Margin="60, 40"
            BackgroundColor="White"
            FontSize="Small"
            TextColor="#4682B4"/>
    </StackLayout>
</ContentPage>

Я не знаю, как решить эту ошибку, это IfirebaseAuthenticator.cs

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

Это FireBaseAuthenticator.cs android

namespace yourActivity.Droid
{
    class FirebaseAuthenticator : IFirebaseAuthenticator
    {
        public async Task<string> LoginWithEmailPassword(string email, string password)
        {
            try
            {
                var user = await 
                FirebaseAuth.Instance.SignInWithEmailAndPasswordAsync(email, password);
                var token = await user.User.GetIdTokenAsync(false);

                return token.Token;
            }
            catch (FirebaseAuthInvalidUserException e)
            {
                e.PrintStackTrace();
                return "";
            }
        }

        public string SignUpWithEmailPassword(string email, string password)
        {
            var signUpTask = FirebaseAuth.Instance.CreateUserWithEmailAndPassword(email, password);
            return signUpTask.Exception?.Message;
        }

        public string GetUserEmail()
        {
            return FirebaseAuth.Instance.CurrentUser.Email;
        }
    }
}

CreateAccountPage.cs

namespace yourActivity
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CreateAccountPage : ContentPage
    {
        IFirebaseAuthenticator auth;
        public CreateAccountPage()
        {
            InitializeComponent();
            auth = DependencyService.Get<IFirebaseAuthenticator>();
        }

        async void SalveUserClicked(object sender, EventArgs e)
        {
            var token = auth.SignUpWithEmailPassword(EmailInput.Text, PasswordInput.Text);
            if (token == null)
            {
                await Navigation.PushAsync(new MainPage());
                ShowSuccess();
            }
            else
                ShowError(token);
        }

        private async void ShowSuccess()
        {
            await DisplayAlert("Successo!", "Utente Creato con successo!", "OK");
        }

        private async void ShowError(string mensagem)
        {
            await DisplayAlert("Ops!", mensagem, "OK");
        }
    } 
}

Ответы [ 2 ]

0 голосов
/ 30 апреля 2020

Создайте класс FirebaseHelper для чтения, добавления, обновления, удаления данных в Firebase realtime database. Пожалуйста, не забудьте заменить ссылку FirebaseClient своей собственной ссылкой.

public class FirebaseHelper
{
  public static FirebaseClient firebase = new FirebaseClient("https://fir-database-44541.firebaseio.com/");

    //Read All
    public static async Task<List<Users>> GetAllUser()
    {
        try
        {
            var userlist = (await firebase
            .Child("Users")
            .OnceAsync<Users>()).Select(item =>
            new Users
            {
                Email = item.Object.Email,
                Password = item.Object.Password
            }).ToList();
            return userlist;
        }
        catch(Exception e)
        {
            Debug.WriteLine($"Error:{e}");
            return null;
        }
    }

    //Read 
    public static async Task<Users> GetUser(string email)
    {
        try
        {
            var allUsers = await GetAllUser();
            await firebase
            .Child("Users")
            .OnceAsync<Users>();
            return allUsers.Where(a => a.Email == email).FirstOrDefault();
        }
        catch(Exception e)
        {
            Debug.WriteLine($"Error:{e}");
            return null;
        }
    }

    //Inser a user
    public static async Task<bool> AddUser(string email,string password)
    {
        try
        {


            await firebase
            .Child("Users")
            .PostAsync(new Users() { Email = email, Password = password });
            return true;
        }
        catch(Exception e)
        {
            Debug.WriteLine($"Error:{e}");
            return false;
        }
    }

    //Update 
    public static async Task<bool> UpdateUser(string email,string password)
    {
        try
        {


            var toUpdateUser = (await firebase
            .Child("Users")
            .OnceAsync<Users>()).Where(a => a.Object.Email == email).FirstOrDefault();
            await firebase
            .Child("Users")
            .Child(toUpdateUser.Key)
            .PutAsync(new Users() { Email = email, Password = password });
            return true;
        }
        catch(Exception e)
        {
            Debug.WriteLine($"Error:{e}");
            return false;
        }
    }

    //Delete User
    public static async Task<bool> DeleteUser(string email)
    {
        try
        {


            var toDeletePerson = (await firebase
            .Child("Users")
            .OnceAsync<Users>()).Where(a => a.Object.Email == email).FirstOrDefault();
            await firebase.Child("Users").Child(toDeletePerson.Key).DeleteAsync();
            return true;
        }
        catch(Exception e)
        {
            Debug.WriteLine($"Error:{e}");
            return false;
        }
    }

}

Вы можете использовать приведенный ниже код для создания страницы входа.

private async void Login()
    {
        //null or empty field validation, check weather email and password is null or empty            
        if (string.IsNullOrEmpty(Email) || string.IsNullOrEmpty(Password))
         await App.Current.MainPage.DisplayAlert("Empty Values", "Please enter Email and Password", "OK");
        else
        {
            //call GetUser function which we define in Firebase helper class
            var user = await FirebaseHelper.GetUser(Email);
            //firebase return null valuse if user data not found in database
            if(user!=null)
            if (Email == user.Email && Password == user.Password)
            {
            await  App.Current.MainPage.DisplayAlert("Login Success", "", "Ok");
             await App.Current.MainPage.Navigation.PushAsync(new WelcomPage(Email));
            }
            else
            await  App.Current.MainPage.DisplayAlert("Login Fail", "Please enter correct Email and Password", "OK");
            else
                await App.Current.MainPage.DisplayAlert("Login Fail", "User not found", "OK");
        }
    }

Весь проект на GitHub для вашей справки. https://github.com/WendyZang/Test/tree/master/FirebaseRealtimeDatabase_Login/XF_FirebaseLogin

0 голосов
/ 28 апреля 2020

Проверьте нулевую ссылку перед вызовом функции или свойства объекта.

async void LoginClicked(object sender, EventArgs e)
{
    if ( auth == null ) {
        Console.WriteLine("Object is null");
        return;
    }

    var token = await auth.LoginWithEmailPassword(EmailInput.Text, PasswordInput.Text);
    if (token != "")
        await Navigation.PushAsync(new TabbedPage1());
    else
        ShowError();
}

Добавьте Внедрение зависимости в FireBaseAuthenticator.cs в android

[assembly: Dependency(typeof(FirebaseAuthenticator))]
namespace yourActivity.Droid
{
    class FirebaseAuthenticator : IFirebaseAuthenticator
    {
        //....
    }
}
...