Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String _uid = '';
- void initState() {
- super.initState();
- auth.currentUser().then((userId) {
- setState(() {
- _uid = userId;
- });
- });
- }
- class RootPage extends StatefulWidget {
- RootPage({this.auth});
- final BaseAuth auth;
- @override
- State<StatefulWidget> createState() => new _RootPageState();
- }
- enum AuthStatus {
- notSignedIn,
- signedIn
- }
- class _RootPageState extends State<RootPage>{
- AuthStatus authStatus = AuthStatus.notSignedIn;
- @override
- void initState() {
- super.initState();
- widget.auth.currentUser().then((userId) {
- setState(() {
- authStatus = userId == null ? AuthStatus.notSignedIn : AuthStatus.signedIn;
- });
- });
- }
- void _signedIn(){
- setState(() {
- authStatus = AuthStatus.signedIn;
- });
- }
- void _signedOut() {
- setState(() {
- authStatus = AuthStatus.notSignedIn;
- });
- }
- @override
- Widget build(BuildContext context) {
- switch(authStatus) {
- case AuthStatus.notSignedIn:
- return new LoginPage(
- auth: widget.auth,
- onSignedIn: _signedIn,
- );
- case AuthStatus.signedIn:
- /*
- return new HomePageState(
- auth: widget.auth,
- onSignedOut: _signedOut,
- );
- */
- return new HomePage(
- auth: widget.auth,
- onSignedOut: _signedOut,
- );
- }
- }
- }
- abstract class BaseAuth{
- Future<String> signInWithEmailAndPassword(String email, String password);
- Future<String> createUserWithEmailAndPassword(String email, String password);
- Future<String> currentUser();
- Future<void> signOut();
- }
- class Auth implements BaseAuth{
- final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
- Future<String> signInWithEmailAndPassword(String email, String password) async {
- FirebaseUser user = await _firebaseAuth.signInWithEmailAndPassword(email: email, password: password);
- return user.uid;
- }
- Future<String> createUserWithEmailAndPassword(String email, String password) async {
- FirebaseUser user = await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
- return user.uid;
- }
- Future<String> currentUser() async {
- FirebaseUser user = await _firebaseAuth.currentUser();
- return user.uid;
- }
- Future<void> signOut() async {
- return _firebaseAuth.signOut();
- }
- }
- class HomePage extends StatefulWidget {
- HomePage({this.auth, this.onSignedOut});
- final BaseAuth auth;
- final VoidCallback onSignedOut;
- void _signOut() async {
- try {
- await auth.signOut();
- onSignedOut();
- } catch (e) {
- print(e);
- }
- }
- @override
- State<StatefulWidget> createState() => new _HomePageState();
- }
- class _HomePageState extends State<HomePage> {
- /*
- _HomePageState({this.auth, this.onSignedOut});
- final BaseAuth auth;
- final VoidCallback onSignedOut;
- */
- //final BaseAuth auth;
- String _uid = '';
- void initState() {
- super.initState();
- auth.currentUser().then((userId) {
- setState(() {
- _uid = userId;
- });
- });
- }
Add Comment
Please, Sign In to add comment