I'm struggling to get Xamarin Firebase Auth on iOS return values coded correctly to process my success/fail logic working for both SignInWithEmailPasswordAsync(email, password) and the CreateUserAsync. I'm sure the problem is in my var user = and AuthDataResult signUpTask = code because the CreateUser and SignIn methods are actually working to add/login users to firebase because I can see them in the console... and if I manually register a user in firebase, the signed-in date is blank... and when I login them in through my iOS app, I see a date stamp appear there. If I register a new user, they appear in Firebase authentication console... all good there. It's just getting the return status from my AuthIOS.cs file back to the calling login and registration pages so I can process success or fail correctly. Any help or would be greatly appreciated. Here's my code:
project level - 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);
} }
project level - 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");
}
}
}
project level - 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 level - 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 "";
}
}
}
}
** Thanks again for any guidance **