How can I pre-fill the registration fields in Keycloak? (Is there a thing called "register_hint" in keycloak?)

Viewed 1192

I am using Keycloak to secure my Javascript application. And when a button is clicked, I want a registration page to open up with the existing email I have. What I mean is, the keycloak registration page will open with email field being pre-filled.

I found out that we can do this in the login page with login_hint. By adding it at the end of the link. Example:

http://localhost:8080/auth/realms/realm_name/protocol/openid-connect/auth?client_id=client_id&redirect_uri={url}&response_mode=fragment&response_type=code&scope=openid&nonce={...}&login_hint=exmaple@gmail.com

But I do not know if I can do the same with the registration page. Is there a way?

Thank you

1 Answers

I know this question is 10 months old, but I'm answering it because I will undoubtedly run into this issue again in future and hoping I stumble across this answer.

I added a snippet of Javascript to my KeyCloak theme for this.

Something along the lines of:

document.addEventListener("DOMContentLoaded", (event) => {
    let email = getUrlParameter('email');
    
    if (email) {
        let field = document.getElementById('email');
        field.value = email;
    }
});

function getUrlParameter(name) {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    var results = regex.exec(location.search);
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};

Then I just send the user to the keycloak registration screen with their email in a URL parameter.

Related