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 “”, 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) {"inside custom authenticator" + context);
  var username = user ? user.username : "anonymous"; + " --> trace auth for: " + username);
  if (username.endsWith("")) {
    redirect_to_idp(context, "ABC-IDP");
  var response = context
    .setAttribute("userId", username)

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.