Skip to content

Instantly share code, notes, and snippets.

@ptyagicodecamp
Last active September 7, 2019 07:28
Show Gist options
  • Save ptyagicodecamp/faaf520cab68236e9a0fcfaaaf831ba9 to your computer and use it in GitHub Desktop.
Save ptyagicodecamp/faaf520cab68236e9a0fcfaaaf831ba9 to your computer and use it in GitHub Desktop.
class FireAuthService extends BaseAuthService {
final Auth _firebaseAuth = fb.auth();
//Get currently logged-in user
@override
Future<User> currentUser() async {
return await _firebaseAuth.currentUser;
}
//Sign-in using email and password, notifies all subscribers.
@override
Future<User> signIn(String email, String password) async {
try {
var auth =
await _firebaseAuth.signInWithEmailAndPassword(email, password);
notifyListeners();
return auth.user;
} catch (e) {
throw Exception(e);
}
}
//This method is called from register form. A user account is created in FirebaseAuth
@override
Future<User> createUser(
String firstName, String lastName, String email, String password) async {
var auth =
await _firebaseAuth.createUserWithEmailAndPassword(email, password);
var info = fb.UserProfile();
info.displayName = '$firstName $lastName';
await auth.user.updateProfile(info);
updateUser(auth.user);
return auth.user;
}
//A record is created at Firestore to keep track of all personalized data for each user.
@override
Future<User> updateUser(User user) async {
final CollectionReference ref = fb.firestore().collection('users');
String displayName = user.displayName;
String photoUrl = user.photoURL;
if (displayName == null) {
displayName = "No Name yet";
}
if (photoUrl == null) {
photoUrl = "";
}
var newData = {
'uid': user.uid,
'displayName': displayName,
'photoUrl': photoUrl,
'email': user.email,
'lastActive': DateTime.now()
};
await ref.doc(user.uid).set(newData, SetOptions(merge: true));
return user;
}
//Sign-out
@override
Future<void> signOut() async {
_firebaseAuth.signOut();
notifyListeners();
}
@override
Future<User> googleSignIn() async {
//TODO
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment