Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // this was auto-generated by Bloc-Code-Generator
- // If you want to use rxdart then remove the commented part of the code
- // and also comment the part above the previously commented one
- /// for example : comment this-> [final _inputController = StreamController<InputMap>();]
- /// and uncomment this -> [//final _inputController = BehaviorSubject<InputMap>();]
- // also don't forget to uncomment the import of rxdart
- import 'package:rxdart/rxdart.dart';
- import 'package:meta/meta.dart';
- import 'package:firebase_auth/firebase_auth.dart';
- class OTPBloc {
- final _inputController = BehaviorSubject<InputMap>();
- final _outputController = BehaviorSubject<OutputMap>();
- Sink<InputMap> get inputSink =>
- _inputController.sink; // use the 'inputSink' to add data
- Observable<OutputMap> get outputStream => _outputController.stream;
- final FirebaseAuth _auth = FirebaseAuth.instance;
- var _verificationId;
- OTPBloc() {
- _inputController.stream.listen(eventDispather);
- }
- void _verifyPhoneNumber(String phoneNumber) async {
- print('loading');
- _outputController.add(OutputMap(state: ScreenState.loading, value: ""));
- final PhoneVerificationCompleted verificationCompleted =
- (AuthCredential phoneAuthCredential) {
- _auth.signInWithCredential(phoneAuthCredential);
- _outputController
- .add(OutputMap(state: ScreenState.done, value: "Verified"));
- // setState(() {
- // _message = 'Received phone auth credential: $phoneAuthCredential';
- // });
- };
- final PhoneVerificationFailed verificationFailed =
- (AuthException authException) {
- _outputController.add(OutputMap(
- state: ScreenState.failed,
- value: authException.message.split('.')[0]));
- // setState(() {
- // _message =
- // 'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}';
- // });
- };
- final PhoneCodeSent codeSent =
- (String verificationId, [int forceResendingToken]) async {
- // widget._scaffold.showSnackBar(SnackBar(
- // content:
- // const Text('Please check your phone for the verification code.'),
- // ));
- _verificationId = verificationId;
- _outputController
- .add(OutputMap(state: ScreenState.codesent, value: 'codesent'));
- };
- final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
- (String verificationId) {
- _verificationId = verificationId;
- print("timed out");
- _outputController
- .add(OutputMap(state: ScreenState.timedOut, value: 'enter code'));
- };
- await _auth.verifyPhoneNumber(
- phoneNumber: phoneNumber,
- timeout: const Duration(seconds: 5),
- verificationCompleted: verificationCompleted,
- verificationFailed: verificationFailed,
- codeSent: codeSent,
- codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
- }
- // Example code of how to sign in with phone.
- void _signInWithPhoneNumber(String smsCode) async {
- _outputController.add(OutputMap(state: ScreenState.loading, value: ""));
- final AuthCredential credential = PhoneAuthProvider.getCredential(
- verificationId: _verificationId,
- smsCode: smsCode,
- );
- final FirebaseUser user = await _auth.signInWithCredential(credential);
- final FirebaseUser currentUser = await _auth.currentUser();
- assert(user.uid == currentUser.uid);
- if (user != null) {
- _outputController.add(OutputMap(state: ScreenState.done, value: ""));
- } else {
- _outputController
- .add(OutputMap(state: ScreenState.failed, value: "Sign in failed"));
- }
- // setState(() {
- // if (user != null) {
- // _message = 'Successfully signed in, uid: ' + user.uid;
- // } else {
- // _message = 'Sign in failed';
- // }
- // });
- }
- eventDispather(InputMap inputMap) {
- if (inputMap.event == Event.verify) {
- _verifyPhoneNumber(inputMap.value.toString());
- } else if (inputMap.event == Event.login) {
- _signInWithPhoneNumber(inputMap.value.toString());
- }
- _outputController
- .add(OutputMap(value: inputMap.value, state: ScreenState.idle));
- }
- dispose() {
- _inputController.close();
- _outputController.close();
- }
- }
- enum Event {
- verify,
- login
- } // The events that may dispatch, you can edit as you wish
- enum ScreenState {
- idle,
- loading,
- codesent,
- failed,
- timedOut,
- done
- } // The state of the screen or app, you can edit as you wish
- class InputMap {
- /// I strongly recommend to change the type from dynamic to your preffered one. for example: [int value];
- dynamic value;
- Event event;
- InputMap({@required this.value, @required this.event});
- }
- class OutputMap {
- /// I strongly recommend to change the type from dynamic to your preffered one. for example: [int value];
- dynamic value;
- ScreenState state;
- OutputMap({@required this.value, @required this.state});
- }
- OTPBloc otpBloc = OTPBloc();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement