Awesome
Plugin.FirebaseAuth
A cross platform plugin for Firebase Authentication. A wrapper for Xamarin.Firebase.iOS.Auth and Xamarin.Firebase.Auth.
Setup
Install Nuget package to each projects.
iOS
- Add GoogleService-Info.plist to iOS project. Select BundleResource as build action.
- Initialize as follows in AppDelegate.
Firebase.Core.App.Configure();
Android
- 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);
Usage
Sign up
var result = await CrossFirebaseAuth.Current.Instance.CreateUserWithEmailAndPasswordAsync(email, password);
Sign in with email and password
var result = await CrossFirebaseAuth.Current.Instance.SignInWithEmailAndPasswordAsync(email, password);
Sign in with Google
var credential = CrossFirebaseAuth.Current.GoogleAuthProvider.GetCredential(idToken, accessToken);
var result = await CrossFirebaseAuth.Current.Instance.SignInWithCredentialAsync(credential);
Sign in with Facebook
var credential = CrossFirebaseAuth.Current.FacebookAuthProvider.GetCredential(accessToken);
var result = await CrossFirebaseAuth.Current.Instance.SignInWithCredentialAsync(credential);
Sign in with Twitter
var provider = new OAuthProvider("twitter.com");
var result = await CrossFirebaseAuth.Current.Instance.SignInWithProviderAsync(provider);
Sign in with GitHub
var provider = new OAuthProvider("github.com");
var result = await CrossFirebaseAuth.Current.Instance.SignInWithProviderAsync(provider);
Sign in with Yahoo
var provider = new OAuthProvider("yahoo.com");
var result = await CrossFirebaseAuth.Current.Instance.SignInWithProviderAsync(provider);
Sign in with Microsoft
var provider = new OAuthProvider("microsoft.com");
var result = await CrossFirebaseAuth.Current.Instance.SignInWithProviderAsync(provider);
Sign in with Apple
// 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);
Sign in with phone number
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);
Sign in with Play Games
var credential = CrossFirebaseAuth.Current.PlayGamesAuthProvider.GetCredential(serverAuthCode);
var result = await CrossFirebaseAuth.Current.Instance.SignInWithCredentialAsync(credential);
Sign in with Game Center
var credential = CrossFirebaseAuth.Current.GameCenterAuthProvider.GetCredentialAsync();
var result = await CrossFirebaseAuth.Current.Instance.SignInWithCredentialAsync(credential);
Sign in with custom token
var user = await CrossFirebaseAuth.Current.Instance.SignInWithCustomTokenAsync(token);
Sign in anonymously
var result = await CrossFirebaseAuth.Current.Instance.SignInAnonymouslyAsync()
Events
AuthState
AuthState event invokes when there is a change in the authentication state.
CrossFirebaseAuth.Current.Instance.AuthState += (sender, e) =>
{
...
};
IdToken
IdToken event invokes when the id token is changed.
CrossFirebaseAuth.Current.Instance.IdToken += (sender, e) =>
{
...
};
Get the currently signed-in user
var user = CrossFirebaseAuth.Current.Instance.CurrentUser;
By using a listener
var registration = CrossFirebaseAuth.Current.Instance.AddAuthStateChangedListener(auth =>
{
var user = auth.CurrentUser;
});
Update a user's profile
var request = new UserProfileChangeRequest
{
DisplayName = displayName,
PhotoUrl = photoUrl
};
await CrossFirebaseAuth.Current.Instance.CurrentUser.UpdateProfileAsync(request);
Set a user's email address
await CrossFirebaseAuth.Current.Instance.CurrentUser.UpdateEmailAsync(email);
Send a user a verification email
await CrossFirebaseAuth.Current.Instance.CurrentUser.SendEmailVerificationAsync();
Set a user's password
await CrossFirebaseAuth.Current.Instance.CurrentUser.UpdatePasswordAsync(password);
Send a password reset email
await CrossFirebaseAuth.Current.Instance.SendPasswordResetEmailAsync(email);
Delete a user
await CrossFirebaseAuth.Current.Instance.CurrentUser.DeleteAsync();
Re-authenticate a user
// 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);
Link
// 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);
Unlink
await CrossFirebaseAuth.Current.Instance.CurrentUser.UnlinkAsync(CrossFirebaseAuth.Current.GoogleAuthProvider.ProviderId);
Action code settings
var actionCodeSettings = new ActionCodeSettings
{
Url = url,
IosBundleId = iosBundleId,
HandleCodeInApp = true
};
actionCodeSettings.SetAndroidPackageName(androidPackageName, true, null);
await CrossFirebaseAuth.Current.Instance.CurrentUser.SendEmailVerificationAsync(actionCodeSettings);
Custom email action handlers
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);
Use multiple projects
var result = await CrossFirebaseAuth.Current.GetInstance("SecondAppName").CreateUserWithEmailAndPasswordAsync(email, password);
Multi-factor authentication
Enroll
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");
Unenroll
var user = CrossFirebaseAuth.Current.Instance.CurrentUser;
var multiFactor = user.MultiFactor;
await multiFactor.UnenrollAsync(multiFactor.EnrolledFactors[0]);
Sign in with a second factor
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);
}
}
FirebaseAuthException error types
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 |