Last active
September 18, 2020 09:00
-
-
Save codemobiles/d74413bfbef6922393276c359f59cbf1 to your computer and use it in GitHub Desktop.
my_flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:cubit/cubit.dart'; | |
import 'package:equatable/equatable.dart'; | |
import 'package:my_flutter/src/commons/constants.dart'; | |
import 'package:my_flutter/src/utils/StringValidator.dart'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
part 'login_state.dart'; | |
class LoginCubit extends Cubit<LoginState> { | |
LoginCubit() : super(LoginInitial()); | |
@override | |
void onTransition(Transition<LoginState> transition) { | |
print(transition); | |
super.onTransition(transition); | |
} | |
void login(String username, String password) async { | |
try { | |
String errorUsername; | |
String errorPassword; | |
if (username.isEmpty) { | |
errorUsername = 'The Email is Empty'; | |
} else if (!EmailSubmitRegexValidator().isValid(username)) { | |
errorUsername = "The Email must be a valid email."; | |
} | |
if (password.length < 8) { | |
errorPassword = 'The Password must be at least 8 characters.'; | |
} | |
if (errorUsername == null && errorPassword == null) { | |
emit(LoginInProgress()); | |
await Future.delayed(Duration(seconds: 2)); | |
authen(username, password); | |
} else { | |
emit(LoginInValid(errorUsername: errorUsername, errorPassword: errorPassword)); | |
} | |
} catch (error) { | |
emit(LoginFailure(error: error)); | |
} | |
} | |
Future<void> authen(String username, String password) async { | |
if (username == "admin@gmail.com" && password == "12345678") { | |
SharedPreferences prefs = await SharedPreferences.getInstance(); | |
prefs.setString(Constants.PREF_USERNAME, username); | |
prefs.setString(Constants.PREF_TOKEN, "sf124124sfasfsaf"); | |
emit(LoginSuccess()); | |
} else { | |
emit(LoginFailure(error: "Username or Password incorrect")); | |
} | |
} | |
} | |
class EmailSubmitRegexValidator extends RegexValidator { | |
EmailSubmitRegexValidator() | |
: super( | |
regexSource: "(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-]+\$)"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment