Skip to content

Instantly share code, notes, and snippets.

@funwithflutter
Last active June 22, 2021 04:50
Show Gist options
  • Save funwithflutter/27662393d7523b0adb68977d13d6d1ff to your computer and use it in GitHub Desktop.
Save funwithflutter/27662393d7523b0adb68977d13d6d1ff to your computer and use it in GitHub Desktop.

Revisions

  1. funwithflutter revised this gist Sep 23, 2019. No changes.
  2. funwithflutter revised this gist Sep 23, 2019. 1 changed file with 58 additions and 2 deletions.
    60 changes: 58 additions & 2 deletions user_repository.dart
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,67 @@
    import 'package:firebase/firebase.dart';
    import 'package:meta/meta.dart';

    Future<UserCredential> signInWithCredentials(
    @immutable
    class UserRepository {
    UserRepository({Auth firebaseAuth, GoogleAuthProvider googleSignin})
    : _firebaseAuth = firebaseAuth ?? auth(),
    _googleSignIn = googleSignin ?? GoogleAuthProvider();

    final Auth _firebaseAuth;
    final GoogleAuthProvider _googleSignIn;

    Future<UserCredential> signInWithGoogle() async {
    try {
    return await _firebaseAuth.signInWithPopup(_googleSignIn);
    } catch (e) {
    print('Error in sign in with google: $e');
    throw '$e';
    }
    }

    Future<UserCredential> signInWithCredentials(
    String email, String password) async {
    try {
    return await _firebaseAuth.signInWithEmailAndPassword(email, password);
    } catch (e) {
    print('Error in sign in with credentials: $e');
    // return e;
    throw '$e';
    }
    }
    }

    Future<UserCredential> signUp({String email, String password}) async {
    try {
    return await _firebaseAuth.createUserWithEmailAndPassword(
    email,
    password,
    );
    } catch (e) {
    print('Error siging in with credentials: $e');
    throw '$e';
    // throw Error('Error signing up with credentials: $e');
    // return e;
    }
    }

    Future<dynamic> signOut() async {
    try {
    return Future.wait([
    _firebaseAuth.signOut(),
    ]);
    } catch (e) {
    print ('Error signin out: $e');
    // return e;
    throw '$e';
    }
    }

    Future<bool> isSignedIn() async {
    final currentUser = _firebaseAuth.currentUser;
    return currentUser != null;
    }

    Future<String> getUser() async {
    return (_firebaseAuth.currentUser).email;
    }
    }
  3. funwithflutter revised this gist Sep 23, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions user_repository.dart
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    import 'package:firebase/firebase.dart';

    Future<UserCredential> signInWithCredentials(
    String email, String password) async {
    try {
  4. funwithflutter created this gist Sep 23, 2019.
    9 changes: 9 additions & 0 deletions user_repository.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    Future<UserCredential> signInWithCredentials(
    String email, String password) async {
    try {
    return await _firebaseAuth.signInWithEmailAndPassword(email, password);
    } catch (e) {
    print('Error in sign in with credentials: $e');
    throw '$e';
    }
    }