eastern-cyan
eastern-cyan2mo ago

How to configure Refine for Keycloak?

I want to configure my application and connect to a Keycloak instance. I don't seem to find a ny configuration file. Am I missing something?
6 Replies
kapa.ai
kapa.ai2mo ago
Hi @gorrilack. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To configure Refine for Keycloak authentication, you can follow these steps: 1. First, make sure you have the necessary dependencies installed. Refine provides an example for Keycloak integration that uses the @react-keycloak/web package. 2. Refine doesn't use a separate configuration file for Keycloak. Instead, you'll need to set up the Keycloak client in your main application file (usually App.tsx). 3. Here's a basic setup for using Keycloak with Refine:
import { Refine } from "@refinedev/core";
import { ReactKeycloakProvider } from "@react-keycloak/web";
import Keycloak from "keycloak-js";

const keycloakConfig = {
url: "YOUR_KEYCLOAK_URL",
realm: "YOUR_REALM",
clientId: "YOUR_CLIENT_ID",
};

const keycloakClient = new Keycloak(keycloakConfig);

const App = () => {
return (
<ReactKeycloakProvider authClient={keycloakClient}>
<Refine
authProvider={authProvider}
// ... other Refine configurations
>
{/* Your app components */}
</Refine>
</ReactKeycloakProvider>
);
};
import { Refine } from "@refinedev/core";
import { ReactKeycloakProvider } from "@react-keycloak/web";
import Keycloak from "keycloak-js";

const keycloakConfig = {
url: "YOUR_KEYCLOAK_URL",
realm: "YOUR_REALM",
clientId: "YOUR_CLIENT_ID",
};

const keycloakClient = new Keycloak(keycloakConfig);

const App = () => {
return (
<ReactKeycloakProvider authClient={keycloakClient}>
<Refine
authProvider={authProvider}
// ... other Refine configurations
>
{/* Your app components */}
</Refine>
</ReactKeycloakProvider>
);
};
4. You'll need to implement an authProvider that uses Keycloak for authentication. This should include methods for login, logout, checking auth status, etc. For a complete example of how to set up Keycloak with Refine, you can refer to the Keycloak example in the Refine documentation (https://refine.dev/docs/examples/auth-provider/keycloak/). This example provides a working implementation that you can use as a starting point for your own application. If you're having trouble finding or setting up the configuration, you might want to start with the Refine Keycloak example using the following command:
npm create refine-app@latest -- --example auth-keycloak
npm create refine-app@latest -- --example auth-keycloak
This will create a new Refine application with Keycloak authentication already set up, which you can then customize for your specific Keycloak instance. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@176352967992016896> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
eastern-cyan
eastern-cyanOP2mo ago
@kapa.ai what if I use useKeycloak
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- The useKeycloak hook is part of the @react-keycloak/web package, which is used in Refine's Keycloak authentication integration. This hook provides access to the Keycloak instance and authentication state within your React components. When you use useKeycloak in a Refine application configured with Keycloak, you can access various Keycloak-related functionalities and state. Here's a brief overview of what you can do with useKeycloak: 1. Access the Keycloak instance and authentication state:
const { keycloak, initialized } = useKeycloak();
const { keycloak, initialized } = useKeycloak();
2. Check if the user is authenticated:
const isAuthenticated = keycloak.authenticated;
const isAuthenticated = keycloak.authenticated;
3. Get the user's token:
const token = keycloak.token;
const token = keycloak.token;
4. Perform login or logout actions:
keycloak.login();
keycloak.logout();
keycloak.login();
keycloak.logout();
5. Access user information:
const userProfile = keycloak.profile;
const userProfile = keycloak.profile;
In the context of Refine, useKeycloak can be particularly useful when implementing custom authentication logic or when you need to access Keycloak-specific information within your components. For example, you might use it in your authProvider implementation:
const authProvider: AuthProvider = {
login: async () => {
const { keycloak } = useKeycloak();
await keycloak.login();
return Promise.resolve();
},
// ... other authProvider methods
};
const authProvider: AuthProvider = {
login: async () => {
const { keycloak } = useKeycloak();
await keycloak.login();
return Promise.resolve();
},
// ... other authProvider methods
};
It's important to note that useKeycloak should be used within components that are descendants of the ReactKeycloakProvider in your component tree. For more detailed information on how to use useKeycloak within a Refine application, you can refer to the Keycloak example in the Refine documentation (https://refine.dev/docs/examples/auth-provider/keycloak/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@176352967992016896> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
eastern-cyan
eastern-cyanOP2mo ago
For those as dumb as I am, configuration can be set in the src/index.ts file

Did you find this page helpful?