Last active
June 24, 2019 15:43
-
-
Save itsatifsiddiqui/00ffa83457b44a81ee35fabec7c8b536 to your computer and use it in GitHub Desktop.
USER REPOSITORY
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/widgets.dart'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
enum Status { Uninitialized, Authenticated, Authenticating, Unauthenticated } | |
class UserRepository with ChangeNotifier { | |
FirebaseAuth _auth; | |
FirebaseUser _user; | |
Status _status = Status.Uninitialized; | |
UserRepository.instance() : _auth = FirebaseAuth.instance { | |
_auth.onAuthStateChanged.listen(_onAuthStateChanged); | |
} | |
Status get status => _status; | |
FirebaseUser get user => _user; | |
Future<bool> signIn(String email, String password) async { | |
try { | |
_status = Status.Authenticating; | |
notifyListeners(); | |
await _auth.signInWithEmailAndPassword(email: email, password: password); | |
return true; | |
} catch (e) { | |
_status = Status.Unauthenticated; | |
notifyListeners(); | |
return false; | |
} | |
} | |
Future signOut() async { | |
_auth.signOut(); | |
_status = Status.Unauthenticated; | |
notifyListeners(); | |
return Future.delayed(Duration.zero); | |
} | |
Future<void> _onAuthStateChanged(FirebaseUser firebaseUser) async { | |
if (firebaseUser == null) { | |
_status = Status.Unauthenticated; | |
} else { | |
_user = firebaseUser; | |
_status = Status.Authenticated; | |
} | |
notifyListeners(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment