react-oidc-context icon indicating copy to clipboard operation
react-oidc-context copied to clipboard

How can i redirect the user to registration link

Open syama-aot opened this issue 2 years ago • 5 comments

Keycloak has register function to redirect the user to registration page.

keycloak.register({ redirectUri: redirectURI, }); How can i acheive same thing with our library. Thanks in advance

syama-aot avatar Sep 12 '23 09:09 syama-aot

From what I've checked, the UserManager, which react-oidc-context internally uses, does not provide a signupRedirect.

If I am missing something I am all ears! You can always build the url manually of course.

alolis avatar Oct 05 '23 14:10 alolis

What about const auth = useAuth(); auth.signinRedirect(); Gets you to provider login page.

benfro avatar Oct 05 '23 18:10 benfro

What about const auth = useAuth(); auth.signinRedirect(); Gets you to provider login page.

signInReqirect() function will return you to Login Page. I want to show the registration page instead of Login Page to the user. Thank you

syama-aot avatar Oct 06 '23 03:10 syama-aot

The issue arises as keycloak uses a non-default OIDC method for this. Looking at their JS adapter implementation (https://github.com/keycloak/keycloak/blob/e69031d411c981fa78b8320b9588771069b4b86d/js/libs/keycloak-js/src/keycloak.js#L846), they have coded a registration endpoint, which will throw an error on access when a default OIDC configuration is used.

                    register: function() {
                        return getRealmUrl() + '/protocol/openid-connect/registrations';
                    },

So looking farther, what is done is the normal createLoginURL (which is done in this library with the createSigninRequest(args) in the UserManager.ts) is wrapped and the options passed have the attribute "action" set to "register". (https://github.com/keycloak/keycloak/blob/e69031d411c981fa78b8320b9588771069b4b86d/js/libs/keycloak-js/src/keycloak.js#L505)

This then causes that method to use the register endpoint instead of the authorize endpoint

        if (options && options.action == 'register') {
            baseUrl = kc.endpoints.register();
        } else {
            baseUrl = kc.endpoints.authorize();
        }

and deactivates the '&kc_action=' URL parameter, while "building" the final URL :

        var url = baseUrl
            + '?client_id=' + encodeURIComponent(kc.clientId)
            + '&redirect_uri=' + encodeURIComponent(redirectUri)
            + '&state=' + encodeURIComponent(state)
            + '&response_mode=' + encodeURIComponent(kc.responseMode)
            + '&response_type=' + encodeURIComponent(kc.responseType)
            + '&scope=' + encodeURIComponent(scope);
        if (useNonce) {
            url = url + '&nonce=' + encodeURIComponent(nonce);
        }
.....

So, essentially, this is IMO not in the scope of this library. However, if someone would want to support this feature, this is probably the things that need to be done:

  1. Add 'register' endpoint to MetadataService
  2. Either
    • Make the OidcClient.createSigninRequest aware of an alternative mode and add logic to use the register endpoint (instead of const url = await this.metadataService.getAuthorizationEndpoint();)
    • or create a new Method for handling registrations.
  3. Extend the UserManager with a method to handle passing appropriate register flags AND/OR controlling that via custom field in CreateSigninRequestArgs.

This is just a first idea, but very likely out of scope for this library. Maybe there are better ways of implementing this. If I magically find time, I might try something.

rcnsol avatar Dec 05 '23 14:12 rcnsol

No news on this feature ?

amd75692 avatar Mar 21 '24 17:03 amd75692