vendredi 30 juillet 2021

Dynamic redirection in expressjs

Am using Keycloak for sso which serves multiple idp autentication and using Express js as my middleware. Here am stuck with dynamic redirection, when i try to login using first tenant credentials am getting preferred-username as empid@domain1.com where as when i try to login with second tenant am getting empid@domain2.com

My query here is how to redirect to different pages based on this preferred_username obtained from response. Example: When i login using 1st tenant i should get redirected to https://example1.com and for second tenant i should get redirected to https://example2.com. How to make it work using if condition? Any help is appreciated. Example code is attached for reference

     var express = require('express');
     var session = require('express-session');
     var bodyParser = require('body-parser');
     var Keycloak = require('keycloak-connect');
     var cors = require('cors');

     app.use(bodyParser.json());

     app.use(cors());

     var memoryStore = new session.MemoryStore();

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

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

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

     app.get('/Example1', function (req, res) {
     res.redirect('https://example1.com')
     });

     app.get('/Example2', keycloak.protect('realm:user'), function (req, res) {
     res.redirect('https://example2.com')
     console.log(req.kauth.grant.access_token.content.preferred_username)
     });

    app.use('*', function (req, res) {
    res.send('Not found!');
    });

Aucun commentaire:

Enregistrer un commentaire