Added documentation to the google sign in provider

This commit is contained in:
Mariano Uvalle 2019-02-27 14:21:07 -06:00
parent 41cbe35ff1
commit 23ec28d6a9

View file

@ -4,17 +4,30 @@ import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:rxdart/rxdart.dart';
/// A Google authentication provider.
///
/// Connects to both Google and Firebase to authenticate a user.
class GoogleSignInProvider {
final GoogleSignIn _googleSignIn;
final FirebaseAuth _auth;
// Instances of [GoogleSignIn] and [FirebaseAuth] can be injected for testing
// purposes. Don't remove.
GoogleSignInProvider([GoogleSignIn googleSignIn, FirebaseAuth firebaseAuth])
: _googleSignIn = googleSignIn ?? GoogleSignIn(),
_auth = firebaseAuth ?? FirebaseAuth.instance;
/// A stream of [FirebaseUser] that notifies when a user signs in and out.
Observable<FirebaseUser> get onAuthStateChange =>
Observable(_auth.onAuthStateChanged);
/// Returns a future of [FirebaseUser].
///
/// Initiates the Google authentication flow. Returns null if the flow fails
/// or gets cancelled.
///
/// If the Google account is being used for the first time, a Firebase user
/// is also created thus this method also works as sign up.
Future<FirebaseUser> signIn() async {
try {
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
@ -34,10 +47,14 @@ class GoogleSignInProvider {
return null;
}
/// Returns a [FirebaseUser] if already signed in, returns null otherwhise.
Future<FirebaseUser> getCurrentUser() async {
return await _auth.currentUser();
}
/// Signs a user out.
///
/// Deletes the firebase user from disk and disconnects the Google user too.
Future<void> signOut() async {
await _googleSignIn.disconnect();
await _auth.signOut();