Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

riverpod_firebase_phone_auth

a new flutter project template showing how to handle mobile authentication using firebase and riverpod.

packages used

  • freezed – to generate union types that handle the various authstates
  • googlefonts – to use any font easily from fonts.google.com
  • responsive_sizer – for sizing purposes
  • flutter_typeahead – to implement the countries typeahead
  • country_list_pick – to flag assets and names for countries typeahead
  • firebase_core and firebase_auth

about

this project mainly uses freezed union types to implement the various states which most login with mobile applications use.
if you don’t know about freezed, go to the package freezed doc. the author has awesome docs, then find yourself a use case and keep freezing.

so when registering with phone, there are a couple of states where the register with phone could be in.
– initial e.g welcome screen
– page where the user enters the phone number.
– loading i.e prompting for sms code/checking if sms code is valid
– smscode has been sent to the users device i.e show code input screen
– sms has been validated.
– sms code is invalid.
– error – some other error happened

what this template mainly does is handle these various states which could happen during mobile signup using union types and then maps the states into widgets.

the various authentication states

@freezed
class authstate with _$authstate {
/// welcome page/user has not entered the phone number
const factory authstate.initial() = _initial;

/// when something is happening. 
const factory authstate.loading(string msg) = _loading;

/// when waiting for user to enter phone number
const factory authstate.waitingforuserinput() = _waitingforuserinput;

/// when the code has been sent to the user
const factory authstate.codesent(string verificationid) = _codesent;

/// when the user successfully signs in
const factory authstate.gotfirebaseuser(
  user user,
) = _gotfirebaseuser;

/// if you're planning to intergrate an external backend with a custom user model
const factory authstate.success(user firebaseuser, customuser user) =
    _success;

/// when firebase sms auto-retrieval times out
const factory authstate.coderetreivaltimedout() = _codetimedout;

/// when an error occurred during phone number verification.
const factory authstate.verificationerror(
  firebaseauthexception firebaseauthexception, {
  string? verificationid,
  string? message,
}) = _verificationerror;

/// when an invalid-phone-number error occurs during phone number verification
const factory authstate.invalidphonenumber(
  firebaseauthexception firebaseauthexception,
  function(string) retry,
  string message,
) = _invalidphonenumber;
const factory authstate.unknownerror(object error, stacktrace? stacktrace) =
    _unknownerror;
}

in our view, we then read the authentication state and map the various states to widgets which need to be shown depending on the state. no if statements, just pure functional transformation. thanks to freezed

  @override
  widget build(buildcontext context, scopedreader watch) {
    final authstate = watch(phoneverificationstateprovider);
    final authstatenotifier = watch(phoneverificationstateprovider.notifier);
    return authstate.when(
      initial: () => welcomepage(
        onacceptbuttonpressed: () {
          authstatenotifier.accepttousephone;
        },
      ),
      loading: (msg) => loadingwidget(msg: msg),
      waitingforuserinput: () => phoneinputwidget(
        verifyfunction: (validatedphone) {
          authstatenotifier.verifyphonenumber(validatedphone);
        },
      ),
      codesent: (verificationid) => smscodeinputwidget(
        ontap: (smscode) {
          authstatenotifier.verifycode(smscode, verificationid);
        },
      ),
      gotfirebaseuser: (user) => whenauthentcated(user),
      success: (firebaseuser, customuser) => whenauthentcated(firebaseuser),
      coderetreivaltimedout: () => coderetrievaltimedoutwidget(
        ontryagain: () => authstatenotifier.retry,
      ),
      verificationerror: (_, verificationid, msg) => invalidcodewidget(
        verificationid: verificationid,
        invalidcodemsg: msg,
        onenternewcode: () {
          if (verificationid != null) {
            authstatenotifier.reenterverificationcode(verificationid);
          }
        },
        onusedifferentphone: () => authstatenotifier.retry,
      ),
      invalidphonenumber: (_, retryfn, _) => invalidphonenumberwidget(
        onchangephonenumber: () {
          authstatenotifier.retry;
        },
      ),
      unknownerror: (error, stacktrace) => genericerrorwidget(
        error: error,
        message: 'unknown error occured',
        retrywidget: elevatedbutton(
          onpressed: () {
            authstatenotifier.retry;
          },
          child: container(
            child: const text('start again'),
            alignment: alignment.center,
            width: adaptive.w(40),
          ),
        ),
      ),
    );
  }

steps to use the template

  1. download the repository and rename the package to what you want it called.
    flutter pub run change_app_package_name:main com.new.package.name
  2. run flutter create --org com.new.package.name
  3. proceed to setup firebase as per the instructions on flutterfire
  4. ensure phone authentication is enabled in firebase console for your project
  5. for android ensure you’ve enabled safetynet here
  6. download the google-services.json file into android/app
  7. run the app.

issues

known issues

  • not tested on ios
  • tests not written

unknown issues

if you encounter any issues running the example, feel free to open an issue.


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.