-1

I am trying to test login in react js application using keycloak but getting Access Denied. Please help me to find the cause. Any details needed I will provide.

Below is the simple reactjs application -

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <title>Hello World Simple App</title>
  </head>
  <body>
    <div>Hello World!</div>
  </body>
</html>

index.js

const express = require("express");
const KeycloakMultirealm = require("keycloak-connect-multirealm");

const app = express();

const config = {};

const keycloakConfig = {
  "auth-server-url": "http://localhost/auth",
  "public-client": true,
  "ssl-required": "none",
  resource: "usermanagement-lite",
  clientId: "usermanagement-lite",
};

const keycloak = new KeycloakMultirealm(config, keycloakConfig);

app.use(keycloak.middleware());

app.get("/", keycloak.protect(), (req, res) => {
  res.sendFile("index.html", { root: __dirname });
});

app.listen(3000);

package.json

{
  "name": "my-project",
  "version": "1.0.0",
  "description": "Demo",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node index.js"
  },
  "author": "Bishal",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "keycloak-connect": "^11.0.3",
    "keycloak-connect-multirealm": "^2.1.0",
    "path": "^0.12.7",
    "pem": "^1.14.8"
  }
}

Response Header details

I am using node 18.16.1 and nginx as localhost for this application. I am trying to get keycloak login page before getting Hello World page but it is throwing me Access Denied. Please help!

Bishal
  • 1
  • 1

1 Answers1

0

U have to check the following

  1. app.use(keycloak.middleware()) middleware is applied before other routes.
  2. Check the console to which might provide specific error
  3. Enable debug logging in Keycloak to see more detailed logs during authentication and authorization. u can do this by enable-logging": true in keylogconfig
  4. Check the redirecting URL
  5. Also check the token
  6. Make sure that the auth-server-url in your keycloakConfig is correct. It should point to the actual Keycloak server UR