Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #235 #236

Merged
merged 1 commit into from
May 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions lib/services/auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ class AuthenticationService {
await GoogleSignIn().signOut();
}

String userEmailValidation(String value, String errorMessage) {
if (value.isEmpty) {
String userEmailValidation({
@required String email,
@required String errorMessage,
}) {
if (email.isEmpty) {
return 'Required';
} else if (errorMessage != null) {
if (!errorMessage.contains('Password') ||
Expand All @@ -55,8 +58,11 @@ class AuthenticationService {
return null;
}

String userPasswordValidation(String value, String errorMessage) {
if (value.isEmpty) {
String userPasswordValidation({
@required String password,
@required String errorMessage,
}) {
if (password.isEmpty) {
return 'Required';
} else if (errorMessage != null) {
if (errorMessage.contains('Password') ||
Expand All @@ -68,8 +74,11 @@ class AuthenticationService {
return null;
}

String userConfirmPasswordValidation(
String value, String password, String confirmPassword) {
String userConfirmPasswordValidation({
@required String value,
@required String password,
@required String confirmPassword,
}) {
if (value.isEmpty) {
return 'Required';
} else if (password != confirmPassword) {
Expand All @@ -78,8 +87,12 @@ class AuthenticationService {
return null;
}

Future<String> userSignUp(String errorText, BuildContext context,
String email, String password) async {
Future<String> userSignUp({
@required String errorText,
@required BuildContext context,
@required String email,
@required String password,
}) async {
String errorTemp = errorText;
try {
final UserCredential newUser = await _auth.createUserWithEmailAndPassword(
Expand All @@ -106,8 +119,12 @@ class AuthenticationService {
return null;
}

Future<String> userLogin(String errorText, BuildContext context, String email,
String password) async {
Future<String> userLogin({
@required String errorText,
@required BuildContext context,
@required String email,
@required String password,
}) async {
String errorTemp = errorText;
try {
final UserCredential existingUser = await _auth
Expand Down
33 changes: 22 additions & 11 deletions lib/views/auth/login_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ class LoginScreenState extends State<LoginScreen> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final AuthenticationService _authenticationService = AuthenticationService();

final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();

String email;
String password;
String errorMessage;
Expand All @@ -41,9 +38,6 @@ class LoginScreenState extends State<LoginScreen> {

@override
void dispose() {
_emailController.clear();
_passwordController.clear();

_email.dispose();
_password.dispose();
_login.dispose();
Expand Down Expand Up @@ -114,10 +108,12 @@ class LoginScreenState extends State<LoginScreen> {
decoration: textFieldDecoration(
hintText: 'Email Address',
),
controller: _emailController,
validator: (String value) => _authenticationService
.userEmailValidation(value, errorMessage),
onFieldSubmitted: (String value) {
validator: (String value) =>
_authenticationService.userEmailValidation(
email: value,
errorMessage: errorMessage,
),
onSaved: (String value) {
email = value;
_email.unfocus();
FocusScope.of(context).requestFocus(_password);
Expand Down Expand Up @@ -154,6 +150,16 @@ class LoginScreenState extends State<LoginScreen> {
}, //for show and hide password
),
),
validator: (String value) =>
_authenticationService.userPasswordValidation(
password: value,
errorMessage: errorMessage,
),
onSaved: (String value) {
password = value;
_password.unfocus();
FocusScope.of(context).requestFocus(_login);
},
),
),
),
Expand All @@ -170,9 +176,14 @@ class LoginScreenState extends State<LoginScreen> {
setState(() {
_loading = true;
});
_formKey.currentState.save();
errorMessage =
await _authenticationService.userLogin(
errorMessage, context, email, password);
errorText: errorMessage,
context: context,
email: email,
password: password,
);
setState(() {
_loading = false;
});
Expand Down
68 changes: 38 additions & 30 deletions lib/views/auth/signup_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ class SignUpScreenState extends State<SignUpScreen> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final AuthenticationService _authenticationService = AuthenticationService();

final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
final TextEditingController _confirmPasswordController =
TextEditingController();

bool showPassword = true;
bool showConfirmPassword = true;

String email;
String password;
String errorMessage;
String confirmPassword;
String _email;
String _password;
String _errorMessage;
String _confirmPassword;

FocusNode _emailFocusNode;
FocusNode _passwordFocusNode;
Expand All @@ -46,10 +41,6 @@ class SignUpScreenState extends State<SignUpScreen> {

@override
void dispose() {
_emailController.clear();
_passwordController.clear();
_confirmPasswordController.clear();

_emailFocusNode.dispose();
_passwordFocusNode.dispose();
_confirmPasswordFocusNode.dispose();
Expand Down Expand Up @@ -120,11 +111,13 @@ class SignUpScreenState extends State<SignUpScreen> {
textInputAction: TextInputAction.next,
decoration:
textFieldDecoration(hintText: 'Email Address'),
controller: _emailController,
validator: (String value) => _authenticationService
.userEmailValidation(value, errorMessage),
onFieldSubmitted: (String value) {
email = value;
validator: (String value) =>
_authenticationService.userEmailValidation(
email: value,
errorMessage: _errorMessage,
),
onSaved: (String value) {
_email = value;
_emailFocusNode.unfocus();
FocusScope.of(context)
.requestFocus(_passwordFocusNode);
Expand Down Expand Up @@ -159,12 +152,14 @@ class SignUpScreenState extends State<SignUpScreen> {
}, //for show and hide password
),
),
validator: (String value) => _authenticationService
.userConfirmPasswordValidation(
value, password, confirmPassword),
onFieldSubmitted: (String value) {
confirmPassword = value;
_confirmPasswordFocusNode.unfocus();
validator: (String value) =>
_authenticationService.userPasswordValidation(
password: value,
errorMessage: _errorMessage,
),
onSaved: (String value) {
_password = value;
_passwordFocusNode.unfocus();
FocusScope.of(context).requestFocus(
_confirmPasswordFocusNode,
);
Expand Down Expand Up @@ -199,7 +194,15 @@ class SignUpScreenState extends State<SignUpScreen> {
}, //for show and hide password
),
),
onFieldSubmitted: (_) {
validator: (String value) => _authenticationService
.userConfirmPasswordValidation(
value: value,
password: _password,
confirmPassword: _confirmPassword,
),
onSaved: (String value) {
_confirmPassword = value;
_confirmPasswordFocusNode.unfocus();
FocusScope.of(context).requestFocus(
_signupFocusNode,
);
Expand All @@ -214,19 +217,24 @@ class SignUpScreenState extends State<SignUpScreen> {
flex: 2,
child: InkWell(
onTap: () async {
debugPrint('SignUp!!');
errorMessage = null;
_errorMessage = null;
if (_formKey.currentState.validate()) {
setState(() {
_loading = true;
});
errorMessage =
_formKey.currentState.save();
_errorMessage =
await _authenticationService.userSignUp(
errorMessage, context, email, password);
errorText: _errorMessage,
context: context,
email: _email,
password: _password,
);
debugPrint('SignUp Success');
setState(() {
_loading = false;
});
if (errorMessage != null) {
if (_errorMessage != null) {
_formKey.currentState.validate();
}
}
Expand Down