Authentiction using Rxdart/BloC

88 views
Skip to first unread message

Scott J

unread,
May 25, 2020, 1:31:16 PM5/25/20
to Flutter Development (flutter-dev)
Hello,

I've been following a couple of guides and got stuck. I've been trying to learn BloC and incorporate it into my project but it 's been difficult for me to understand right away. I've managed to come up with something following this video, but still having some trouble. Using what I have so far I want to add authentication but I'm not sure about how I should do it using what I have now. 

signupBloc:
class SignUpBloc {
 final _fullNm = BehaviorSubject<String>();
 final _userNm = BehaviorSubject<String>();
 final _emailAd = BehaviorSubject<String>();
 final _password = BehaviorSubject<String>();

  //Get
 Stream<String> get fullNm => _fullNm.stream.transform(validateFullName);
 Stream<String> get userNm => _userNm.stream.transform(validateUserName);
 Stream<String> get emailAd => _emailAd.stream.transform(validateEmailAddress);
 Stream<String> get password => _password.stream.transform(validatePassword);

  Stream<bool> get signUpInfoValid => Rx.combineLatest4(fullNm, userNm, emailAd,
     password, (fullNm, userNm, emailAd, password) => true);

  //Set
 Function(String) get changeFullName => _fullNm.sink.add;
 Function(String) get changeUserName => _userNm.sink.add;
 Function(String) get changeEmailAddress => _emailAd.sink.add;
 Function(String) get changePassword => _password.sink.add;

  dispose() {
   _fullNm.close();
   _userNm.close();
   _emailAd.close();
   _password.close();
 }

  //Transformers
 final validateFullName = StreamTransformer<String, String>.fromHandlers(
     handleData: (fullNm, sink) {
   if (validator.name(fullNm) != true)
     sink.addError("Please enter a valid name");
   else
     sink.add(fullNm);
 });
 final validateUserName = StreamTransformer<String, String>.fromHandlers(
     handleData: (userName, sink) {
   bool validateStructure(String value) {
     String pattern =
         r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[!@#\$&*~]).{8,}$';
     RegExp regExp = new RegExp(pattern);
     return regExp.hasMatch(value);
   }

    if (validateStructure(userName) != true)
     sink.addError("Please enter a user name");
   else
     sink.add(userName);
 });
 final validateEmailAddress = StreamTransformer<String, String>.fromHandlers(
     handleData: (emailAd, sink) {
   if (validator.email(emailAd) != true)
     sink.addError("Please enter a email address");
   else
     sink.add(emailAd);
 });

  final validatePassword = StreamTransformer<String, String>.fromHandlers(
     handleData: (password, sink) {
   if (validator.password(password) != true)
     sink.addError("Please enter a password");
   else
     sink.add(password);
 });

  Future signUpUsr() async {
   AuthResult user = await FirebaseAuth.instance
       .createUserWithEmailAndPassword(
           email: _emailAd.value, password: _password.value);
   FirebaseUser fbUser = user.user;
   await DatabaseService(uid: fbUser.uid)
       .updataUserData(_fullNm.value, _userNm.value, _emailAd.value);
   debugPrint("Name: ${_fullNm.value}");
   debugPrint("\nUser name: ${_userNm.value}");
   debugPrint("\nEmail Address: ${_emailAd.value}");
   debugPrint("\nPassword: ${_password.value}");
 }
}
As far the signup bloc goes,when the user finishes typing and presses the button it would go to the signUpUsr().

loginbloc:
class LoginBloc {
 final _userNm = BehaviorSubject<String>();
 final _emailAd = BehaviorSubject<String>();
 final _password = BehaviorSubject<String>();

  //Get

  Stream<String> get emailAd => _emailAd.stream;
 Stream<String> get password => _password.stream;

  Stream<bool> get loginInfoValid =>
     Rx.combineLatest2(emailAd, password, (emailAd, password) => true);

  //Set
 Function(String) get changeEmailAddress => _emailAd.sink.add;
 Function(String) get changePassword => _password.sink.add;

  dispose() {
   _userNm.close();
   _emailAd.close();
   _password.close();
 }

  Future loginUsr() async {
   try {
     AuthResult user = await FirebaseAuth.instance.signInWithEmailAndPassword(
         email: _emailAd.value, password: _password.value);
     // Navigator.pushNamed(context, 'home')
   } catch (error_msg) {
     switch (error_msg.code) {
       case "ERROR_USER_NOT_FOUND":
         {}
         break;
       case "ERROR_WRONG_PASSWORD":
         {}
         break;
       default:
         var n = null;
     }
   }

    debugPrint("\nUser name: ${_userNm.value}");
   debugPrint("\nPassword: ${_password.value}");
 }
}

With the loginBloc I have the problem of when the user logins in I cant go to the page I want because it doesn't have a context. So I navigate to the home page from the login page, regardless of the error that firebase may or may not gives back.

Looking at other tutorials ,I see that I would need an authBloc(i think?) and depending on the authentication status of the user it would go to the login or home page. I understand that part, but I want to know if I can salvage and use what I have here because I understand what's going on here but don't understand how I can use it further.

My question is:

Using what I have, how can I add authentication, do I need an authentication bloc?

I've watched plenty of tutorials but still cant wrap my head around it. Any help is appreciated, thanks!

Mutlu Şimşek

unread,
May 25, 2020, 1:38:32 PM5/25/20
to Scott J, Flutter Development (flutter-dev)
I really don't understand why people make coding hard for themselves with these kind of packages. I used mobx and auth packages and I was able to enable authentication with only a couple of lines... 

25 May 2020 Pzt 20:31 tarihinde Scott J <sje...@gmail.com> şunu yazdı:
--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/1be9dba0-b941-47d2-a8bf-24666bb93feb%40googlegroups.com.

Scott J

unread,
May 25, 2020, 1:44:55 PM5/25/20
to Flutter Development (flutter-dev)
There's alot of mixed messages online about what you should use, I just tossed a coin and picked one! Mind pointing me to the packages that you use?
To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.

Mutlu Şimşek

unread,
May 25, 2020, 1:53:57 PM5/25/20
to Scott J, Flutter Development (flutter-dev)
Auth package for authentication: https://pub.dev/packages/auth

Mobx for state management: https://pub.dev/packages/mobx

You can create an auth store, create an auth instance in auth store, use provider to access auth store throughout the app. 

25 May 2020 Pzt 20:44 tarihinde Scott J <sje...@gmail.com> şunu yazdı:
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/2de117cb-0e45-43da-8a35-5119f25880bf%40googlegroups.com.

Scott J

unread,
May 25, 2020, 1:57:33 PM5/25/20
to Flutter Development (flutter-dev)

re:fi.64

unread,
May 25, 2020, 10:29:42 PM5/25/20
to Scott J, Flutter Development (flutter-dev)
Even using just BloCs I think this code is a bit overkill...

Personally, I wouldn't separate the username / password / etc. It's arguably a bit prettier to have the validation logic split up like this, but given that that's the *only time* they're used split, it would be easier to just pass a single LoginDetails around I'd think.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages