fb_auth 1.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 84

fb_auth #

A Shared Firebase Auth Plugin for Mobile, Web and Desktop. Included AuthBloc for minimal setup! Single dynamic import and compile time ready for ios, android, macos, windows, linux and web.

Getting Started #

Setup your Bloc

final _auth = AuthBloc();

Follow Installation Instructions for Web: https://pub.dev/packages/firebase

Update /web/index.html in the body tag.

<!-- The core Firebase JS SDK is always required and must be listed first -->
  <script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-app.js"></script>

  <!-- TODO: Add SDKs for Firebase products that you want to use
      https://firebase.google.com/docs/web/setup#config-web-app -->
  <script src="https://www.gstatic.com/firebasejs//6.3.3/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-firestore.js"></script>
  <script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-storage.js"></script>
  <script src="https://www.gstatic.com/firebasejs/6.3.3/firebase-functions.js"></script>

  <script>
    // Your web app's Firebase configuration
    var firebaseConfig = {
      apiKey: "API_KEY",
      authDomain: "AUTH_DOMAIN",
      databaseURL: "DATABASE_URL",
      projectId: "PROJECT_ID",
      storageBucket: "STORAGE_BUCKET",
      messagingSenderId: "MESSAGING_SENDER_ID",
      appId: "APP_ID"
    };
    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
  </script>

Follow Installation Instructions for Mobile: https://pub.dev/packages/firebase_auth

  • Update ios/Runner and add the GoogleService-Info.plist downloaded from firebase

  • Update android/app and add the google-services.json downloaded from firebase

  • Update android/build.gradle and update the classpath:

    classpath 'com.google.gms:google-services:4.2.0'

Actions #

  • Check Current User
_auth.dispatch(CheckUser());
  • Listen for Auth Changes
_userChanged = _fbAuth.onAuthChanged().listen((user) {
    _auth.dispatch(UpdateUser(user));
});
  • Logout
_auth.dispatch(LogoutEvent(_user));
  • Login
_auth.dispatch(LoginEvent(_email, _password));
  • Guest
_auth.dispatch(LoginGuest());
  • Create Account
_auth.dispatch(CreateAccount(_email, _password, displayName: _name));
  • Edit Info
_auth.dispatch(EditInfo(displayName: _name));
  • Forgot Password
_auth.dispatch(ForgotPassword(_email));
  • Send Email Verification
_auth.dispatch(SendEmailVerification());

1.1.0 #

  • Updating to Bloc 2.0.0

1.0.6 #

  • Fixing Google Login (Mobile, Web)

1.0.3 #

  • Updating to bloc 1.0.0
  • Fixing Google Login (Mobile, Web)
  • Try login after failed create account or user is null

1.0.1 #

  • Support for Login with Access Token
  • Support for Google Sign In for Mobile and Web

1.0.0 #

  • Adding Support for Desktop
  • Updated Example for Desktop
  • Adding App Config Requirement
  • Fixing Bugs with io auth

0.0.2 #

  • Add Sign In As Guest

0.0.1 #

  • TODO: Describe initial release.

example/README.md

fb_auth_example #

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:fb_auth/fb_auth.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  StreamSubscription<AuthUser> _userChanged;
  final _auth = AuthBloc(
    saveUser: _saveUser,
    deleteUser: _deleteUser,
  );

  static _deleteUser() async {}
  static _saveUser(user) async {}

  @override
  void initState() {
    _auth.dispatch(CheckUser());
    final _fbAuth = FBAuth();
    _userChanged = _fbAuth.onAuthChanged().listen((user) {
      _auth.dispatch(UpdateUser(user));
    });
    super.initState();
  }

  @override
  void dispose() {
    _auth.dispose();
    _userChanged.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider<AuthBloc>(builder: (_) => _auth),
      ],
      child: MaterialApp(
        home: AuthCheck(),
      ),
    );
  }
}

class AuthCheck extends StatelessWidget {
  const AuthCheck({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AuthBloc, AuthState>(
      builder: (context, state) {
        if (state is LoggedInState) {
          return HomeScreen();
        }
        return LoginScreen();
      },
    );
  }
}

class LoginScreen extends StatefulWidget {
  @override
  _LoginScreenState createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  String _email, _password, _name;
  bool _createAccount = true;
  final _formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    final _auth = BlocProvider.of<AuthBloc>(context);
    return BlocBuilder<AuthBloc, AuthState>(
      builder: (context, state) => Scaffold(
        appBar: AppBar(
          title: Text('Login to Firebase'),
        ),
        body: SingleChildScrollView(
          child: Form(
            key: _formKey,
            child: Column(
              children: <Widget>[
                Visibility(
                  visible: _createAccount,
                  child: ListTile(
                    title: TextFormField(
                      decoration: InputDecoration(labelText: 'Display Name'),
                      onSaved: (val) => _name = val,
                    ),
                  ),
                ),
                ListTile(
                  title: TextFormField(
                    decoration: InputDecoration(labelText: 'Email'),
                    validator: (val) => val.isEmpty ? 'Email Required' : null,
                    onSaved: (val) => _email = val,
                  ),
                ),
                ListTile(
                  title: TextFormField(
                    decoration: InputDecoration(labelText: 'Password'),
                    validator: (val) =>
                        val.isEmpty ? 'Password Required' : null,
                    onSaved: (val) => _password = val,
                  ),
                ),
                if (_createAccount) ...[
                  ListTile(
                      title: RaisedButton(
                    child: Text('Sign Up'),
                    onPressed: () {
                      if (_formKey.currentState.validate()) {
                        _formKey.currentState.save();
                        _auth.dispatch(CreateAccount(_email, _password,
                            displayName: _name));
                      }
                    },
                  )),
                ] else ...[
                  ListTile(
                      title: RaisedButton(
                    child: Text('Login'),
                    onPressed: () {
                      if (_formKey.currentState.validate()) {
                        _formKey.currentState.save();
                        _auth.dispatch(LoginEvent(_email, _password));
                      }
                    },
                  )),
                ],
                ListTile(
                    title: FlatButton(
                  child: Text(_createAccount
                      ? 'Already have an account?'
                      : 'Create a new account?'),
                  onPressed: () {
                    if (mounted)
                      setState(() {
                        _createAccount = !_createAccount;
                      });
                  },
                )),
                if (state is AuthLoadingState) ...[CircularProgressIndicator()],
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final _user = AuthBloc.currentUser(context);
    return Scaffold(
      appBar: AppBar(
        title: Text('Welcome ${_user?.displayName ?? 'Guest'}'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Logout'),
          onPressed: () {
            BlocProvider.of<AuthBloc>(context).dispatch(LogoutEvent(_user));
          },
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  fb_auth: ^1.1.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:fb_auth/fb_auth.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
80
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
84
Learn more about scoring.

We analyzed this package on Nov 19, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.0
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.6

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health issues and suggestions

Document public APIs. (-0.12 points)

225 out of 232 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/data/blocs/auth/auth_bloc.dart. (-0.50 points)

Analysis of lib/data/blocs/auth/auth_bloc.dart reported 1 hint:

line 165 col 11: Close instances of dart.core.Sink.

Fix lib/data/services/rest_api/helpers/user.dart. (-0.50 points)

Analysis of lib/data/services/rest_api/helpers/user.dart reported 1 hint:

line 5 col 30: Field doesn't override an inherited getter or setter.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (firebase_auth).

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
bloc ^2.0.0 2.0.0
firebase ^6.0.0 6.0.0
firebase_auth ^0.14.0+5 0.14.0+9 0.15.0
flutter 0.0.0
flutter_bloc ^2.0.0 2.0.1
http ^0.12.0+2 0.12.0+2
path_provider ^1.4.0 1.4.2
Transitive dependencies
async 2.4.0
charcode 1.1.2
collection 1.14.11 1.14.12
firebase_core 0.4.1+5
http_parser 3.1.3
js 0.6.1+1
meta 1.1.7 1.1.8
path 1.6.4
pedantic 1.8.0+1
platform 2.2.1
provider 3.1.0+1
rxdart 0.22.6
sky_engine 0.0.99
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test