Custom authenticator not logging in even if I enter correct username and password

I have written a custom authenticator script on Keycloak which is triggered on clicking sign-in button on username form. Script checks whether the username entered ends with “@abc.com”, if yes it redirects the user to configured external IDP for this domain and if not then it should open password form for the user to authentication using credentials. Domain based IDP discovery and corresponding redirection is working fine but the other case i.e when entered username doesn’t match any specified domain then I need to redirect user to password form. But here I am getting the below attached error. (username and password entered are verified and are present in keycloak local DB) enter image description here

Custom authenticator script is as follow:

`function authenticate(context) {
  LOG.info("inside custom authenticator" + context);
  var username = user ? user.username : "anonymous";
  LOG.info(script.name + " --> trace auth for: " + username);
  if (username.endsWith("@abc.com")) {
    redirect_to_idp(context, "ABC-IDP");
    return;
  }
  var response = context
    .form()
    .setAttribute("userId", username)
    .createForm("login-password.ftl");
  context.challenge(response);
  return;
}`

It seems like username entered in username-form is not retained for the password form to authenticate the user. Pls verify if I am approaching the use case the right way and how to resolve this issue.