docs/self-hosting/auth/next-auth/keycloak.mdx
Keycloak is an open-source identity and access management solution that provides single sign-on, identity brokering, and social login features, suitable for modern applications and services.
<Callout type={'tip'}> If you want to privately deploy Keycloak, we recommend using it together with LobeHub via Docker Compose deployment for easier service management. </Callout>
If you deploy using a local network IP, this guide assumes:
http://LobeHub_IP:3210.http://KEYCLOAK_IP:8080.If you deploy using a public network, this guide assumes:
https://lobe.example.com.https://lobe-auth-api.example.com.Access your privately deployed Keycloak admin console (default is http://localhost:8080/admin) and log in with the administrator account.
Create a new Realm
Create a Client
LobeHubOpenID Connecthttp://localhost:3210/api/auth/callback/keycloakhttp://LobeHub_IP:3210/api/auth/callback/keycloakhttps://lobe.example.com/api/auth/callback/keycloakGet Client Secret
Create Users
Create Roles and Permissions
To ensure the security of your application, it's recommended to control Keycloak's registration functionality.
<Callout type={'warning'}> If registration is not disabled, anyone might be able to register and log in to your application. Please configure according to your security requirements. </Callout>
Set the obtained client ID and client secret as AUTH_KEYCLOAK_ID and AUTH_KEYCLOAK_SECRET in the LobeHub environment variables.
Configure the LobeHub environment variable AUTH_KEYCLOAK_ISSUER as:
http://localhost:8080/realms/LobeHub for local development environmenthttp://KEYCLOAK_IP:8080/realms/LobeHub for privately deployed Keycloak on a local networkhttps://lobe-auth-api.example.com/realms/LobeHub for Keycloak deployed in a public environmentWhen deploying LobeHub, you need to configure the following environment variables:
| Environment Variable | Type | Description |
|---|---|---|
AUTH_SECRET | Required | Key used to encrypt Auth.js session tokens. You can generate a key using: openssl rand -base64 32 |
NEXT_AUTH_SSO_PROVIDERS | Required | Select the single sign-on provider for LobeHub. For Keycloak, fill in keycloak. |
AUTH_KEYCLOAK_ID | Required | Keycloak client ID |
AUTH_KEYCLOAK_SECRET | Required | Keycloak client secret |
AUTH_KEYCLOAK_ISSUER | Required | OpenID Connect issuer URL for the Keycloak provider, in the format {keycloak_url}/realms/{realm_name} |
AUTH_URL | Required | This URL specifies the callback address for Auth.js during OAuth verification. Only needed when the default generated redirect address is incorrect. https://lobe.example.com/api/auth |
<Callout type={'tip'}> Visit 📘 Environment Variables for details on related variables. </Callout> </Steps>
<Callout type={'info'}> After successful deployment, users will be able to authenticate through Keycloak and use LobeHub. </Callout>