Skip to content

Instantly share code, notes, and snippets.

@salihgueler
Created August 26, 2022 15:07
Show Gist options
  • Save salihgueler/13915342338adbea1c4c6e8bc687d4e8 to your computer and use it in GitHub Desktop.
Save salihgueler/13915342338adbea1c4c6e8bc687d4e8 to your computer and use it in GitHub Desktop.
Future<void> _loginUser() async {
// (1)
if (_formKey.currentState!.validate()) {
setState(() {
_isLoading = true;
_errorMessage = null;
});
try {
// (2)
final result = await Amplify.Auth.signIn(
username: _usernameController.text,
password: _passwordController.text,
);
setState(() {
_isLoading = false;
});
// (3)
if (result.isSignedIn && mounted) {
context.go('/home');
// (4)
} else if (result.nextStep?.signInStep == CognitoSignInStep.confirmSignUp.value) {
context.go(
'/confirm-user',
extra: _usernameController.text,
);
await Amplify.Auth.resendSignUpCode(
username: _usernameController.text,
);
}
// (5)
} on UserNotFoundException {
setState(() {
_isLoading = false;
_errorMessage = 'The user is not found. Please register';
});
// (6)
} on UserNotConfirmedException {
setState(() {
_isLoading = false;
});
context.go(
'/confirm-user',
extra: _usernameController.text,
);
// (7)
} on NotAuthorizedException {
setState(() {
_isLoading = false;
_errorMessage = 'Username or password is wrong. Please try again';
});
} on Exception catch(error) {
setState(() {
_isLoading = false;
_errorMessage = 'An unexpected error has happened. Check the logs for detail';
});
logger.error(error.toString());
}
_usernameController.clear();
_passwordController.clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment