Skip to content

ElishaMisoi/Plugin.FirebaseAuth

 
 

Repository files navigation

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.

Plugin.FirebaseAuth NuGet

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.)
  • This Plugin uses Plugin.CurrentActivity. Setup as follows in MainActivity.
Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, bundle);
  • Target Framework must be Android 9.0 (Pie). Multi-Dex needs to be enabled if you use other libraries, Xamarin.Forms etc.
<TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
<AndroidEnableMultiDex>true</AndroidEnableMultiDex>

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 credential = CrossFirebaseAuth.Current.TwitterAuthProvider.GetCredential(token, secret);
var result = await CrossFirebaseAuth.Current.Instance.SignInWithCredentialAsync(credential);

Sign in with GitHub

var credential = CrossFirebaseAuth.Current.GitHubAuthProvider.GetCredential(token);
var result = await CrossFirebaseAuth.Current.Instance.SignInWithCredentialAsync(credential);

Sign in with phone number

var verificationResult = await CrossFirebaseAuth.Current.PhoneAuthProvider.VerifyPhoneNumberAsync(CrossFirebaseAuth.Current.Instance, phoneNumber);

var credential = CrossFirebaseAuth.Current.PhoneAuthProvider.GetCredential(CrossFirebaseAuth.Current.Instance, verificationResult.VerificationId, verificationCode);
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

var credential = CrossFirebaseAuth.Current.EmailAuthProvider.GetCredential(email, password);
await CrossFirebaseAuth.Current.Instance.CurrentUser.ReauthenticateAndRetrieveDataAsync(credential);

Link

var credential = CrossFirebaseAuth.Current.GoogleAuthProvider.GetCredential(idToken, accessToken);
var result = await CrossFirebaseAuth.Current.Instance.CurrentUser.LinkWithCredentialAsync(credential);

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

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);

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%