A cross platform plugin for Firebase Authentication. A wrapper for Xamarin.Firebase.iOS.Auth and Xamarin.Firebase.Auth.
Install Nuget package to each projects.
- Add GoogleService-Info.plist to iOS project. Select BundleResource as build action.
- Initialize as follows in AppDelegate.
Firebase.Core.App.Configure();
- Add google-services.json to Android project. Select GoogleServicesJson as build action. (If you can't select GoogleServicesJson, reload this android project.)
- Target framework version needs to be Android 10.0.
- This Plugin uses Plugin.CurrentActivity. Setup as follows in MainActivity.
Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, bundle);
var result = await CrossFirebaseAuth.Current.Instance.CreateUserWithEmailAndPasswordAsync(email, password);
var result = await CrossFirebaseAuth.Current.Instance.SignInWithEmailAndPasswordAsync(email, password);
var credential = CrossFirebaseAuth.Current.GoogleAuthProvider.GetCredential(idToken, accessToken);
var result = await CrossFirebaseAuth.Current.Instance.SignInWithCredentialAsync(credential);
var credential = CrossFirebaseAuth.Current.FacebookAuthProvider.GetCredential(accessToken);
var result = await CrossFirebaseAuth.Current.Instance.SignInWithCredentialAsync(credential);
var provider = new OAuthProvider("twitter.com");
var result = await CrossFirebaseAuth.Current.Instance.SignInWithProviderAsync(provider);
var provider = new OAuthProvider("github.com");
var result = await CrossFirebaseAuth.Current.Instance.SignInWithProviderAsync(provider);
var provider = new OAuthProvider("yahoo.com");
var result = await CrossFirebaseAuth.Current.Instance.SignInWithProviderAsync(provider);
var provider = new OAuthProvider("microsoft.com");
var result = await CrossFirebaseAuth.Current.Instance.SignInWithProviderAsync(provider);
// For iOS
var credential = CrossFirebaseAuth.Current.OAuthProvider.GetCredential("apple.com", idToken, rawNonce: rawNonce);
var result = await CrossFirebaseAuth.Current.Instance.SignInWithCredentialAsync(credential);
// For Android
var provider = new OAuthProvider("apple.com");
var result = await CrossFirebaseAuth.Current.Instance.SignInWithProviderAsync(provider);
var verificationResult = await CrossFirebaseAuth.Current.PhoneAuthProvider.VerifyPhoneNumberAsync(phoneNumber);
var credential = CrossFirebaseAuth.Current.PhoneAuthProvider.GetCredential(verificationResult.VerificationId, verificationCode);
var result = await CrossFirebaseAuth.Current.Instance.SignInWithCredentialAsync(credential);
var credential = CrossFirebaseAuth.Current.PlayGamesAuthProvider.GetCredential(serverAuthCode);
var result = await CrossFirebaseAuth.Current.Instance.SignInWithCredentialAsync(credential);
var credential = CrossFirebaseAuth.Current.GameCenterAuthProvider.GetCredentialAsync();
var result = await CrossFirebaseAuth.Current.Instance.SignInWithCredentialAsync(credential);
var user = await CrossFirebaseAuth.Current.Instance.SignInWithCustomTokenAsync(token);
var result = await CrossFirebaseAuth.Current.Instance.SignInAnonymouslyAsync()
AuthState event invokes when there is a change in the authentication state.
CrossFirebaseAuth.Current.Instance.AuthState += (sender, e) =>
{
...
};
IdToken event invokes when the id token is changed.
CrossFirebaseAuth.Current.Instance.IdToken += (sender, e) =>
{
...
};
var user = CrossFirebaseAuth.Current.Instance.CurrentUser;
By using a listener
var registration = CrossFirebaseAuth.Current.Instance.AddAuthStateChangedListener(auth =>
{
var user = auth.CurrentUser;
});
var request = new UserProfileChangeRequest
{
DisplayName = displayName,
PhotoUrl = photoUrl
};
await CrossFirebaseAuth.Current.Instance.CurrentUser.UpdateProfileAsync(request);
await CrossFirebaseAuth.Current.Instance.CurrentUser.UpdateEmailAsync(email);
await CrossFirebaseAuth.Current.Instance.CurrentUser.SendEmailVerificationAsync();
await CrossFirebaseAuth.Current.Instance.CurrentUser.UpdatePasswordAsync(password);
await CrossFirebaseAuth.Current.Instance.SendPasswordResetEmailAsync(email);
await CrossFirebaseAuth.Current.Instance.CurrentUser.DeleteAsync();
// With Credential
var credential = CrossFirebaseAuth.Current.EmailAuthProvider.GetCredential(email, password);
await CrossFirebaseAuth.Current.Instance.CurrentUser.ReauthenticateAsync(credential);
// With Provider
var provider = new OAuthProvider("twitter.com");
var result = await CrossFirebaseAuth.Current.Instance.CurrentUser.ReauthenticateWithProviderAsync(provider);
// With Credential
var credential = CrossFirebaseAuth.Current.GoogleAuthProvider.GetCredential(idToken, accessToken);
var result = await CrossFirebaseAuth.Current.Instance.CurrentUser.LinkWithCredentialAsync(credential);
// With Provider
var provider = new OAuthProvider("twitter.com");
var result = await CrossFirebaseAuth.Current.Instance.CurrentUser.LinkWithProviderAsync(provider);
await CrossFirebaseAuth.Current.Instance.CurrentUser.UnlinkAsync(CrossFirebaseAuth.Current.GoogleAuthProvider.ProviderId);
var actionCodeSettings = new ActionCodeSettings
{
Url = url,
IosBundleId = iosBundleId,
HandleCodeInApp = true
};
actionCodeSettings.SetAndroidPackageName(androidPackageName, true, null);
await CrossFirebaseAuth.Current.Instance.CurrentUser.SendEmailVerificationAsync(actionCodeSettings);
Reset password
var email = await CrossFirebaseAuth.Current.Instance.VerifyPasswordResetCodeAsync(code);
await CrossFirebaseAuth.Current.Instance.ConfirmPasswordResetAsync(code, newPassword);
Recover email
var info = await CrossFirebaseAuth.Current.Instance.CheckActionCodeAsync(code);
await CrossFirebaseAuth.Current.Instance.ApplyActionCodeAsync(code);
Verify email
await CrossFirebaseAuth.Current.Instance.ApplyActionCodeAsync(code);
var result = await CrossFirebaseAuth.Current.GetInstance("SecondAppName").CreateUserWithEmailAndPasswordAsync(email, password);
var user = CrossFirebaseAuth.Current.Instance.CurrentUser;
var session = await user.MultiFactor.GetSessionAsync();
var verificationResult = await CrossFirebaseAuth.Current.PhoneAuthProvider.VerifyPhoneNumberAsync(phoneNumber, session);
var credential = CrossFirebaseAuth.Current.PhoneAuthProvider.GetCredential(verificationResult.VerificationId, verificationCode);
var assertion = CrossFirebaseAuth.Current.PhoneMultiFactorGenerator.GetAssertion(credential);
await user.MultiFactor.EnrollAsync(assertion, "phone number");
var user = CrossFirebaseAuth.Current.Instance.CurrentUser;
var multiFactor = user.MultiFactor;
await multiFactor.UnenrollAsync(multiFactor.EnrolledFactors[0]);
try
{
var porvider = new OAuthProvider("github.com");
var result = await CrossFirebaseAuth.Current.Instance.SignInWithProviderAsync(porvider);
}
catch (FirebaseAuthException e)
{
var resolver = e.Resolver;
if (resolver != null)
{
var hint = resolver.Hints.First() as IPhoneMultiFactorInfo;
var verificationResult = await CrossFirebaseAuth.Current.PhoneAuthProvider.VerifyPhoneNumberAsync(hint, resolver.Session);
var credential = CrossFirebaseAuth.Current.PhoneAuthProvider.GetCredential(verificationResult.VerificationId, verificationCode);
var assertion = CrossFirebaseAuth.Current.PhoneMultiFactorGenerator.GetAssertion(credential);
var result = await resolver.ResolveSignInAsync(assertion);
}
}
The error types are based on the exceptions of Android Java. Refert to Firebase documents for the representations.
Error types | Exceptions of Android Java |
---|---|
Other | FirebaseAuthException |
NetWork | FirebaseNetworkException |
FirebaseAuthEmailException | |
ActionCode | FirebaseAuthActionCodeException |
InvalidUser | FirebaseAuthInvalidUserException |
TooManyRequests | FirebaseTooManyRequestsException |
WeakPassword | FirebaseAuthWeakPasswordException |
UserCollision | FirebaseAuthUserCollisionException |
InvalidCredentials | FirebaseAuthInvalidCredentialsException |
RecentLoginRequired | FirebaseAuthRecentLoginRequiredException |
MultiFactor | FirebaseAuthMultiFactorException |
Web | FirebaseAuthWebException |
ApiNotAvailable | FirebaseApiNotAvailableException |