khosro_68
khosro_68

Reputation: 1

Xamarin forms with firebase user registration error

i am developing an app using xamarin forms and firebase authentication with xamarin.firebase.auth and xamarin.firebase.core when i want to create a new user the code works fine but it gives me the exception

Java.Lang.IllegalStateException: 'Task is not yet complete'

when i trace the code line by line every thing works just fine and i get no errors but when running the app after creating user it gives me the exception.

this is my code:

inerface in pcl:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace XamarinFirebaseAuth
{
    public interface IAuth
    {
        Task<string> LoginWithEmailPassword(string email, string password);
        bool SignUpWithEmailPassword(string email, string password);

    }
}

android implementation:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Foundation;
using UIKit;
using XamarinFirebaseAuth;
using XamarinFirebaseAuth.iOS;
using Firebase.Auth;
using System.Threading.Tasks;
using Xamarin.Forms;

[assembly: Dependency(typeof(AuthIOS))]
namespace XamarinFirebaseAuth.iOS
{
    public class AuthIOS : IAuth
    {
        public async Task<string> LoginWithEmailPassword(string email, string password)
        {
            try
            {
                var user = await Auth.DefaultInstance.SignInWithPasswordAsync(email, password);
                var token = user.User.GetIdTokenAsync();
                return token.ToString();
            }
            catch(Exception e)
            {
                return "";
            }
        }
        public  bool SignUpWithEmailPassword(string email, string password)
        {
            try
            {
                var signUpTask = Auth.DefaultInstance.CreateUserAsync(email, password);
                return true;
            }
            catch (Exception e)
            {
                throw;
                //return false;
            }

        }
    }
}

and this is my sign up page :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

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

        private async void btnRegister_Clicked(object sender, EventArgs e)
        {
            try
            {
                bool created = auth.SignUpWithEmailPassword(EmailInput.Text, PasswordInput.Text);
                if (created)
                {
                    await DisplayAlert("Success", "Your account created successfully", "OK");
                    await Navigation.PopAsync();
                }
                else
                {
                    await DisplayAlert("Error", "Something went wrong. Try again later!", "OK");
                }
            }
            catch
            {
                throw;
            }

        }
    }
}

Upvotes: 0

Views: 625

Answers (1)

nevermore
nevermore

Reputation: 15796

I think you should await the CreateUserAsync method to know whether the account is created successfully or not:

AuthDataResult signUpTask = await Auth.DefaultInstance.CreateUserAsync(email, password);

Then you can get the user info:

await signUpTask.User.GetIdTokenAsync();

Upvotes: 1

Related Questions