Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Created June 4, 2019 17:52
Show Gist options
  • Save aaronksaunders/464e772a3c5af4488f878f248c3dc4f4 to your computer and use it in GitHub Desktop.
Save aaronksaunders/464e772a3c5af4488f878f248c3dc4f4 to your computer and use it in GitHub Desktop.
Markdium-Simple Firebase Login Flow in Flutter, Now Firebase
import 'package:firebase_auth/firebase_auth.dart';
import 'dart:async';
import 'package:flutter/cupertino.dart';
class AuthService with ChangeNotifier {
final FirebaseAuth _auth = FirebaseAuth.instance;
///
/// return the Future with firebase user object FirebaseUser if one exists
///
Future getUser() {
return _auth.currentUser();
}
// wrapping the firebase calls
Future logout() async {
var result = FirebaseAuth.instance.signOut();
notifyListeners();
return result;
}
///
/// wrapping the firebase call to signInWithEmailAndPassword
/// `email` String
/// `password` String
///
Future loginUser({String email, String password}) async {
try {
var result = await FirebaseAuth.instance
.signInWithEmailAndPassword(email: email, password: password);
// since something changed, let's notify the listeners...
notifyListeners();
return result;
} catch (e) {
// throw the Firebase AuthException that we caught
throw new AuthException(e.code, e.message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment