Does Keycloak provide user registration API to register user from client side app?

Viewed 3286

I'd like to provide my users with a link to the registration process in KC, instead of them having to go via the 'login' page and follow a link. (Ultimately, I'd like to provide their user_id within the link, but that is for another question :-))

It seems that I'd can not link straight to:

/auth/realms/my-relm/login-actions/registration?client_id=my-client&tab_id=D92kNju2J1M

as it looks like KC needs to start a 'session' and append the tab_id to the URL.

I've tried using this link:

/auth/realms/my-realm/protocol/openid-connect/registrations?response_type=code&client_id=my-client&login=true&scope=openid

Bu I get a 'wrong protocol' error as I'm using SAML.

Does anybody know how to do this?

Thanks, Stuart

1 Answers

This is quite old question but it can help others, Since Keycloak doesn't provide dedicated API for user registration so I have used the admin api to do the job. This is how I managed to create user from client app.

import querystring from "querystring";
....
....

const adminCredential = () => {
    return querystring.stringify({
        username: process.env.KEYCLOAK_ADMIN_USERNAME,
        password: process.env.KEYCLOAK_ADMIN_PASSWORD,
        client_id: process.env.KEYCLOAK_ADMMIN_CLIENT_ID,
        grant_type: "password"
    });
};

export const createUserHandler = async (userParams, callback) => {
    const keycloakParams = {
        username: userParams.username,
        email: userParams.email,
        lastName: userParams.lastName,
        firstName: userParams.firstName,
        emailVerified: false,
        enabled: false,
        attributes: {
            country: userParams.country,
            phoneVerified: false,
            emailVerificationCode: randomSixDigitNumber(),
            userRole:  "user",
            avatarUrl
        },
        credentials: [
            {
                type: "password",
                value: userParams.password,
                temporary: false
            }
        ]
    };


    try {
        const {
            data: { access_token }
        } = await KCAuthRestClient(adminCredential());

        const { code, data, status } = await KCUserCreateRestClient({
            access_token,
            data: keycloakParams
        }); 
    } catch (error) {
        console.error(error)
    }
const axios = require('axios');
const { KEYCLOAK_URL } = require("../API");

export const KCUserCreateRestClient = async ({ data, access_token }) => {
    const url = `${KEYCLOAK_URL}/auth/admin/realms/${REALM}/users`
    const config = {
        headers: {
            Authorization: `Bearer ${access_token}`
        }
    };

    try {
        const response = await axios.post(url, data, config);
        const userId = response.headers.location.split(`${url}/`)[1]
        return await {
            code: response.status,
            status: response.statusText,
            data: { id: userId }
        }
    } catch (error) {
        return {
            statusCode: error.response.status,
            status: error.response.statusText
        }
    }
}
Related