Guest User

Untitled

a guest
Sep 25th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. String _uid = '';
  2. void initState() {
  3. super.initState();
  4. auth.currentUser().then((userId) {
  5. setState(() {
  6. _uid = userId;
  7. });
  8. });
  9. }
  10.  
  11. class RootPage extends StatefulWidget {
  12.  
  13. RootPage({this.auth});
  14. final BaseAuth auth;
  15.  
  16. @override
  17. State<StatefulWidget> createState() => new _RootPageState();
  18.  
  19. }
  20.  
  21. enum AuthStatus {
  22. notSignedIn,
  23. signedIn
  24. }
  25.  
  26. class _RootPageState extends State<RootPage>{
  27.  
  28. AuthStatus authStatus = AuthStatus.notSignedIn;
  29.  
  30. @override
  31. void initState() {
  32. super.initState();
  33. widget.auth.currentUser().then((userId) {
  34. setState(() {
  35. authStatus = userId == null ? AuthStatus.notSignedIn : AuthStatus.signedIn;
  36. });
  37. });
  38. }
  39.  
  40. void _signedIn(){
  41. setState(() {
  42. authStatus = AuthStatus.signedIn;
  43. });
  44. }
  45.  
  46. void _signedOut() {
  47. setState(() {
  48. authStatus = AuthStatus.notSignedIn;
  49. });
  50. }
  51.  
  52. @override
  53. Widget build(BuildContext context) {
  54. switch(authStatus) {
  55. case AuthStatus.notSignedIn:
  56. return new LoginPage(
  57. auth: widget.auth,
  58. onSignedIn: _signedIn,
  59. );
  60. case AuthStatus.signedIn:
  61. /*
  62. return new HomePageState(
  63. auth: widget.auth,
  64. onSignedOut: _signedOut,
  65. );
  66. */
  67. return new HomePage(
  68. auth: widget.auth,
  69. onSignedOut: _signedOut,
  70. );
  71. }
  72. }
  73. }
  74.  
  75. abstract class BaseAuth{
  76. Future<String> signInWithEmailAndPassword(String email, String password);
  77. Future<String> createUserWithEmailAndPassword(String email, String password);
  78. Future<String> currentUser();
  79. Future<void> signOut();
  80. }
  81.  
  82. class Auth implements BaseAuth{
  83.  
  84. final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
  85.  
  86. Future<String> signInWithEmailAndPassword(String email, String password) async {
  87. FirebaseUser user = await _firebaseAuth.signInWithEmailAndPassword(email: email, password: password);
  88. return user.uid;
  89. }
  90.  
  91. Future<String> createUserWithEmailAndPassword(String email, String password) async {
  92. FirebaseUser user = await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
  93. return user.uid;
  94. }
  95.  
  96. Future<String> currentUser() async {
  97. FirebaseUser user = await _firebaseAuth.currentUser();
  98. return user.uid;
  99. }
  100.  
  101. Future<void> signOut() async {
  102. return _firebaseAuth.signOut();
  103. }
  104.  
  105. }
  106.  
  107. class HomePage extends StatefulWidget {
  108.  
  109. HomePage({this.auth, this.onSignedOut});
  110.  
  111. final BaseAuth auth;
  112. final VoidCallback onSignedOut;
  113.  
  114. void _signOut() async {
  115. try {
  116. await auth.signOut();
  117. onSignedOut();
  118. } catch (e) {
  119. print(e);
  120. }
  121. }
  122. @override
  123. State<StatefulWidget> createState() => new _HomePageState();
  124. }
  125.  
  126. class _HomePageState extends State<HomePage> {
  127. /*
  128. _HomePageState({this.auth, this.onSignedOut});
  129. final BaseAuth auth;
  130. final VoidCallback onSignedOut;
  131. */
  132. //final BaseAuth auth;
  133.  
  134. String _uid = '';
  135. void initState() {
  136. super.initState();
  137. auth.currentUser().then((userId) {
  138. setState(() {
  139. _uid = userId;
  140. });
  141. });
  142. }
Add Comment
Please, Sign In to add comment