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

Provide example of how to use keycloak

Open vincentsong opened this issue 1 year ago • 8 comments

Could you please provide an example app to show how to use this lib with keycloak?

vincentsong avatar Jun 19 '23 03:06 vincentsong

Hey there! I'm experimenting with Keycloak too, and I landed on this GitHub repository.

I'm still trying to learn how this entire Keycloak + React-OIDC works, but so far I managed to get the authentication working by using these settings in my App.tsx:

import { AuthProvider, AuthProviderProps } from "react-oidc-context";

import MainRouter from "./router";

const oidcConfig: AuthProviderProps = {
  authority: "https://your-keycloak.example.com/realms/your-realm", // replace your-keycloak and your-realm
  client_id: "your-client-id", // replace your-client-id
  redirect_uri: "http://localhost:5173", // replace this with the URL of your application, probably injected depending on the environment... here I'm using Vite and this is the URL of the development server
  onSigninCallback: (): void => {
    window.history.replaceState({}, document.title, window.location.pathname);
  },
};

export default function App() {
  return (
    <AuthProvider {...oidcConfig}>
      <MainRouter />
    </AuthProvider>
  );
}

Then in a page you can do as suggested in the documentation:

import { useAuth } from "react-oidc-context";

export default function Dashboard() {
  const auth = useAuth();

  switch (auth.activeNavigator) {
    case "signinSilent":
      return <div>Signing you in ...</div>;
    case "signoutRedirect":
      return <div>Signing you out ...</div>;
  }

  if (auth.isLoading) {
    return <div>Auth is loading...</div>;
  }

  if (auth.error) {
    return <div>Oops... Auth error {auth.error.message}</div>;
  }

  if (auth.isAuthenticated) {
    return (
      <div>
        Hello {auth.user?.profile.name}{" "}
        <button onClick={() => void auth.removeUser()}>Log out</button>
      </div>
    );
  }

  return <button onClick={() => void auth.signinRedirect()}>Log in</button>;
  );
}

I agree that it would be useful to have some kind of Wiki page on the repo explaining how to setup correctly this library depending on the auth provider used.

Maybe we can arrange something like that?

frikyfriky11 avatar Jun 26 '23 08:06 frikyfriky11

Thank you @frikyfriky11, your solution is working like a charm.

vincentsong avatar Jun 26 '23 23:06 vincentsong

Does someone had problems with issuing the token? Sometimes the /token route just returns Code not valid and no useful message in the terminal

Rafael-Ramblas avatar Jul 04 '23 19:07 Rafael-Ramblas

Guys and about roles? How can i get access this from react-oidc-context?

luisfagottani avatar Oct 24 '23 21:10 luisfagottani

@luisfagottani maybe https://github.com/authts/react-oidc-context/issues/872 helps you

pamapa avatar Oct 25 '23 07:10 pamapa

@frikyfriky11 I tried to replicate your setup but just nothing happens as of yet. Something seems to be missing. then I forgot to add the actual sign in (e.g. autosignin from the README.md). Could you share a minimal viable project for Keycloak?

It could be added to https://github.com/authts/oidc-client-ts/blob/main/docs/index.md#provider-specific-settings an example folder in the repository then.

EDIT I'm still struggling a little with making the redirect work correctly but at least it's working apart from that. So thanks a lot for sharing the configuration.

Sax388 avatar Feb 06 '24 14:02 Sax388

@Sax388 this was something I worked on briefly a couple of months ago and I don't have a ready-to-share minimal viable project right now. Maybe the repo owners could consider giving us an opinion on this, so maybe I could send a PR to integrate this into the official docs of this library if this is something that is well received :)

frikyfriky11 avatar Feb 09 '24 15:02 frikyfriky11

In case it's helpful: https://github.com/authts/react-oidc-context/issues/1208

zach-betz-hln avatar Apr 19 '24 16:04 zach-betz-hln