Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. // this was auto-generated by Bloc-Code-Generator
  2. // If you want to use rxdart then remove the commented part of the code
  3. // and also comment the part above the previously commented one
  4. /// for example : comment this-> [final _inputController = StreamController<InputMap>();]
  5. /// and uncomment this -> [//final _inputController = BehaviorSubject<InputMap>();]
  6. // also don't forget to uncomment the import of rxdart
  7.  
  8. import 'package:rxdart/rxdart.dart';
  9. import 'package:meta/meta.dart';
  10. import 'package:firebase_auth/firebase_auth.dart';
  11.  
  12. class OTPBloc {
  13. final _inputController = BehaviorSubject<InputMap>();
  14.  
  15. final _outputController = BehaviorSubject<OutputMap>();
  16.  
  17. Sink<InputMap> get inputSink =>
  18. _inputController.sink; // use the 'inputSink' to add data
  19.  
  20. Observable<OutputMap> get outputStream => _outputController.stream;
  21.  
  22. final FirebaseAuth _auth = FirebaseAuth.instance;
  23.  
  24. var _verificationId;
  25.  
  26. OTPBloc() {
  27. _inputController.stream.listen(eventDispather);
  28. }
  29.  
  30. void _verifyPhoneNumber(String phoneNumber) async {
  31. print('loading');
  32. _outputController.add(OutputMap(state: ScreenState.loading, value: ""));
  33. final PhoneVerificationCompleted verificationCompleted =
  34. (AuthCredential phoneAuthCredential) {
  35. _auth.signInWithCredential(phoneAuthCredential);
  36.  
  37. _outputController
  38. .add(OutputMap(state: ScreenState.done, value: "Verified"));
  39. // setState(() {
  40. // _message = 'Received phone auth credential: $phoneAuthCredential';
  41. // });
  42. };
  43.  
  44. final PhoneVerificationFailed verificationFailed =
  45. (AuthException authException) {
  46. _outputController.add(OutputMap(
  47. state: ScreenState.failed,
  48. value: authException.message.split('.')[0]));
  49. // setState(() {
  50. // _message =
  51. // 'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}';
  52. // });
  53. };
  54.  
  55. final PhoneCodeSent codeSent =
  56. (String verificationId, [int forceResendingToken]) async {
  57. // widget._scaffold.showSnackBar(SnackBar(
  58. // content:
  59. // const Text('Please check your phone for the verification code.'),
  60. // ));
  61. _verificationId = verificationId;
  62. _outputController
  63. .add(OutputMap(state: ScreenState.codesent, value: 'codesent'));
  64. };
  65.  
  66. final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
  67. (String verificationId) {
  68. _verificationId = verificationId;
  69. print("timed out");
  70. _outputController
  71. .add(OutputMap(state: ScreenState.timedOut, value: 'enter code'));
  72. };
  73.  
  74. await _auth.verifyPhoneNumber(
  75. phoneNumber: phoneNumber,
  76. timeout: const Duration(seconds: 5),
  77. verificationCompleted: verificationCompleted,
  78. verificationFailed: verificationFailed,
  79. codeSent: codeSent,
  80. codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
  81. }
  82.  
  83. // Example code of how to sign in with phone.
  84. void _signInWithPhoneNumber(String smsCode) async {
  85. _outputController.add(OutputMap(state: ScreenState.loading, value: ""));
  86. final AuthCredential credential = PhoneAuthProvider.getCredential(
  87. verificationId: _verificationId,
  88. smsCode: smsCode,
  89. );
  90. final FirebaseUser user = await _auth.signInWithCredential(credential);
  91. final FirebaseUser currentUser = await _auth.currentUser();
  92. assert(user.uid == currentUser.uid);
  93. if (user != null) {
  94. _outputController.add(OutputMap(state: ScreenState.done, value: ""));
  95. } else {
  96. _outputController
  97. .add(OutputMap(state: ScreenState.failed, value: "Sign in failed"));
  98. }
  99. // setState(() {
  100. // if (user != null) {
  101. // _message = 'Successfully signed in, uid: ' + user.uid;
  102. // } else {
  103. // _message = 'Sign in failed';
  104. // }
  105. // });
  106. }
  107.  
  108. eventDispather(InputMap inputMap) {
  109. if (inputMap.event == Event.verify) {
  110. _verifyPhoneNumber(inputMap.value.toString());
  111. } else if (inputMap.event == Event.login) {
  112. _signInWithPhoneNumber(inputMap.value.toString());
  113. }
  114. _outputController
  115. .add(OutputMap(value: inputMap.value, state: ScreenState.idle));
  116. }
  117.  
  118. dispose() {
  119. _inputController.close();
  120. _outputController.close();
  121. }
  122. }
  123.  
  124. enum Event {
  125. verify,
  126. login
  127. } // The events that may dispatch, you can edit as you wish
  128.  
  129. enum ScreenState {
  130. idle,
  131. loading,
  132. codesent,
  133. failed,
  134. timedOut,
  135. done
  136. } // The state of the screen or app, you can edit as you wish
  137.  
  138. class InputMap {
  139. /// I strongly recommend to change the type from dynamic to your preffered one. for example: [int value];
  140. dynamic value;
  141. Event event;
  142. InputMap({@required this.value, @required this.event});
  143. }
  144.  
  145. class OutputMap {
  146. /// I strongly recommend to change the type from dynamic to your preffered one. for example: [int value];
  147. dynamic value;
  148. ScreenState state;
  149. OutputMap({@required this.value, @required this.state});
  150. }
  151.  
  152. OTPBloc otpBloc = OTPBloc();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement