Importing user from legacy store to Keycloak

Hi all,
I’ve created a User Storage SPI for migrating all users from a legacy store to Keycloak. Inside isValid() I’m updating the user credentials and setting the setFederationLink as null. All looks fine for the first time login for the user. Next time when user try to login, I get the message “Invalid username or password.” looks like password is not updating into the Keycloak database. Following is the code snippet.

public boolean isValid(RealmModel realm, UserModel user, CredentialInput credentialInput) {
		if (!supportsCredentialType(credentialInput.getType()) || !(credentialInput instanceof UserCredentialModel)) {
			return false;
		boolean valid = apiService.validate(user.getUsername(), credentialInput.getChallengeResponse());
		if (valid) {
			session.userCredentialManager().updateCredential(realm, user, credentialInput);
		return valid; 

Is this the right way to do it? Can someone help me on this. Any help would be much appropriated.