Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error /protocol/openid-connect/token 401 (Unauthorized) #4

Open
craftedsro opened this issue Apr 4, 2023 · 1 comment
Open

Error /protocol/openid-connect/token 401 (Unauthorized) #4

craftedsro opened this issue Apr 4, 2023 · 1 comment

Comments

@craftedsro
Copy link

Hi i got error when trying authenticated and I am stuck at /protocol/openid-connect/token 401 (Unauthorized) after login on keycloack.

I try using keycloak server with auth.js by with next.js and it works.

 Keycloak server version 21.0.2

"keycloak-js": "^21.0.2",
"ra-keycloak": "^1.0.0",
"react": "^18.2.0",

Error occure in

keycloack.mjs

on line 762

req.onreadystatechange = function() {
     if (req.readyState == 4) {
          if (req.status == 200) {
               var tokenResponse = JSON.parse(req.responseText);
                   authSuccess(tokenResponse['access_token'], tokenResponse['refresh_token'], tokenResponse['id_token'], kc.flow === 'standard');
                        scheduleCheckIframe();
                    } else {
                        kc.onAuthError && kc.onAuthError();
                        promise && promise.setError();
                    }
                }
            };

            req.send(params);

App.tsx

const config: KeycloakConfig = {
  url: "http://localhost:8080",
  realm: "test-realm",
  clientId: "test-client",
};

const initOptions: KeycloakInitOptions = {
  onLoad: "login-required",
};

const getPermissions = (decoded: KeycloakTokenParsed) => {
  const roles = decoded?.realm_access?.roles;
  if (!roles) {
    return false;
  }
  if (roles.includes("admin")) return "admin";
  if (roles.includes("user")) return "user";
  return false;
};

const App = () => {
  const [keycloak, setKeycloak] = useState<Keycloak | undefined>(undefined);
  const authProvider = useRef<AuthProvider | undefined>(undefined);
  const dataProvider = useRef<DataProvider | undefined>(undefined);

  useEffect(() => {
    const initKeyCloakClient = async () => {
      const keycloakClient = new Keycloak(config);
      keycloakClient.clientSecret = "tPW4yfVEOurE.....";

      await keycloakClient.init(initOptions);

      authProvider.current = keycloakAuthProvider(keycloakClient, {
        onPermissions: getPermissions,
      });
      dataProvider.current = keyCloakTokenDataProviderBuilder(
        myDataProvider,
        keycloakClient
      );
      setKeycloak(keycloakClient);
    };
    if (!keycloak) {
      initKeyCloakClient();
    }
  }, [keycloak]);

  // hide the admin until the dataProvider and authProvider are ready
  if (!keycloak) return <p>Loading...</p>;

  return (
    <Admin
      authProvider={authProvider.current}
...

Access Settings
image

Capability Settings
image

Credentials Settings
image

@MattMx
Copy link

MattMx commented Apr 15, 2024

@craftedsro Were you able to resolve this issue? First I had a problem with infinite loops which I resolved it thanks to a lingering pull request in this repository. Sadly now I hit 401 Unauthorized error while trying to use the demo code in a fresh react-admin vite app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants