Coder Social home page Coder Social logo

plugin.firebaseauth's Introduction

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.)
  • 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
Email FirebaseAuthEmailException
ActionCode FirebaseAuthActionCodeException
InvalidUser FirebaseAuthInvalidUserException
TooManyRequests FirebaseTooManyRequestsException
WeakPassword FirebaseAuthWeakPasswordException
UserCollision FirebaseAuthUserCollisionException
InvalidCredentials FirebaseAuthInvalidCredentialsException
RecentLoginRequired FirebaseAuthRecentLoginRequiredException
MultiFactor FirebaseAuthMultiFactorException
Web FirebaseAuthWebException
ApiNotAvailable FirebaseApiNotAvailableException

plugin.firebaseauth's People

Contributors

f-matsushima avatar f-miyu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.