Authenticator class

Authenticator is a ChangeNotifier that wraps various authentication methods supported through Firebase. Google, Facebook and email authentication are currently supported.

It also supports mocked platform channels so authentication flow can be included in flutter tests.

Usage:

void main() {
  auth = Authenticator();
  await auth.signInWithFacebook();
  await auth.signOut();
}

Since it is a ChangeNotifier, it can notify widgets when there is a change. For example:

class LoginPage extends StatelessWidget {
  static final loginPageKey = ValueKey(loginPageKeyName);

  LoginPage() : super(key: loginPageKey);

  @override
  Widget build(BuildContext context) {
    final auth = Provider.of<Authenticator>(context);

    return ListenableProvider<Authenticator>.value(
      value: locator<Authenticator>(),
      child: Scaffold(
          appBar: AppBar(title: Text('Welcome!'), centerTitle: true),
          body: Container(
            child: Center(
              child: Column(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  LoginLogoView(),
                  auth.authState != AuthState.AUTHENTICATED
                      ? SignInButtonsView()
                      : Container()
                ],
              ),
            ),
          )),
    );
  }
}

And in a SignInButtonsView, there may be buttons to login with Google, Facebook, email, or create a new account using email. One of the onPressed values for a button could execute this code to sign in with google.

  void _signInWithGoogleButton() {
    var auth = Provider.of<Authenticator>(context);
    setState(() {
      signingIn = true;
    });
    auth.signInWithGoogle();
}

Mixing Authenticator with packages like GetIt to turn it into a globally accessible object can be convenient. There should only be one Authenticator at any point in time anyway.

Mixed in types

Constructors

Authenticator()
The default constructor should be used in standard flutter applications. If you are using something like GetIt, you can follow the setup pattern using Authenticator like so: [...]

Properties

authState AuthState
read-only
currentSignInProvider String
read-only
hashCode int
The hash code for this object. [...]
read-only, inherited
hasListeners bool
Whether any listeners are currently registered. [...]
@protected, read-only, inherited
mockEmail String
read-only
mockPassword String
read-only
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited
user FirebaseUser
read-only

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes. [...]
inherited
createUserWithEmailAndPassword(String email, String password) Future<String>
Starts an asynchronous call to Firebase to create a new account for the email address and password supplied. It will immediately set Authenticator's auth state to AuthState.AUTHENTICATING and notify any listeners for changes so the UI can be updated appropriately. [...]
currentUser() Future<String>
Returns the current user, or null if no user has been authenticated.
dispose() → void
Discards any resources used by the object. After this is called, the object is not in a usable state and should be discarded (calls to addListener and removeListener will throw after the object is disposed). [...]
@mustCallSuper, inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
notifyListeners() → void
Call all the registered listeners. [...]
@protected, @visibleForTesting, inherited
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes. [...]
inherited
sendPasswordResetEmail(String emailAddress) Future<void>
Initiates a password reset flow. Firebase will send an email to the user with links to reset their password. This is an async method that doesn't return anything or send any notifications.
signInWithEmailAndPassword(String email, String password) Future<String>
This will initiate an email+password login through Firebase. The user must have an existing account in Firebase for this to succeed. (See createUserWithEmailAndPassword()) [...]
signInWithFacebook() Future<String>
Just like it says on the tin. This will initiate a Facebook sign in. On devices, this may, at least once, open a system overlay asking the user if they would like to sign in with Facebook. [...]
signInWithGoogle() Future<String>
This will initiate a Google sign in. On devices, this may, at least once, open a system overlay asking the user if they would like to sign in with Google, or select which account of they use more than one. [...]
signOut() Future<void>
Signs out the user. Once the operation has completed it will set the current user to null, set the authentication state for the Authenticator to AuthState.UNAUTHENTICATED and set the authentication type to AuthType.UNKNOWN. A notification will then be sent to all listeners.
toString() String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) bool
The equality operator. [...]
inherited

Static Methods

createMocked({String email: 'testemailaddress@testmail.com', String password: '123456'}) Authenticator
The named constructor best used with unit tests (e.g. flutter test). [...]