0
votes

I'm trying to implement keycloak on my node.js apps.

I'm using keycloak-nodejs-connect on my node.js apps.

If there is no operation on the website for longer than session idle time, I would like to automatically go to the login page or notify the user that are logged out (When session is expired, pressing F5 will automatically bring up the login page).

Keycloak version : 12.0.0 keycloak-nodejs-connect version : 12.0.4

I just using I wrote the code by referring to the source code below.

https://github.com/keycloak/keycloak-nodejs-connect/blob/master/example/index.js

How do I redirect the client page to login page or logout page?

Thanks for comments.

Here is the code.

app.js var memoryStore = new session.MemoryStore();

app.use(session({ secret: 'mySecret', resave: false, saveUninitialized: true, store: memoryStore }));

var keycloak = new Keycloak({ store: memoryStore });

app.use(keycloak.middleware({ logout: '/logout', admin:'/' }));

index.js(router) var Keycloak = require('keycloak-connect');

var memoryStore = new session.MemoryStore(); var keycloak = new Keycloak({ store: memoryStore });

1
What have you tried so far? Can you include a code snippet in your question?Dominik
Can you include a code snippet in your question?Roman
Thanks for comment. I added code. Is there any preparation for session exprie?user3371918

1 Answers

0
votes

If you are using keycloak-connect, you do not have to worry about redirecting of the user, because this is the reason, why you are using the library, it does it for you.

Regarding the links: if you are running the example, the url http://localhost:3000/logout will redirect to keycloak server and removes the session there. After that, it will redirect back to your application, in this case http://localhost:3000

Any resource of the express app, which needs authentication and/or authorization, like

app.get('/hello', keycloak.protect(), function (req, res) {
  res.json({message: `Hello ${req.kauth.grant.id_token.content.preferred_username}`});
});

will automatically redirect to the keycloak server and either show the login form or handle single sign on, whatever is configured for the realm. So there is no link to the login page, but if there is no valid user session at http://localhost:3000/hello, it will redirect to it, otherwise it will already know, who you are and will print your name.