<div dir="ltr"><span style="font-size:12.8px">I have created a rest api in node js and used keycloak-connect npm packge.</span><br style="font-size:12.8px"><span style="font-size:12.8px">I have mapped the nodejs middleware with keycloak middleware and just put</span><br style="font-size:12.8px"><span style="font-size:12.8px">keycloak.Protect() method in side api method.</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">When the user is not logged in, it shows a login screen and ask for</span><br style="font-size:12.8px"><span style="font-size:12.8px">credential. After login, it shows the result. but I don&#39;t want to show a</span><br style="font-size:12.8px"><span style="font-size:12.8px">login screen if user is not already logged in. Instead of that i want to</span><br style="font-size:12.8px"><span style="font-size:12.8px">pass the token and get access based upon that token?</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Do i need to do anything in the API code so that it will accept the user</span><br style="font-size:12.8px"><span style="font-size:12.8px">token?</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">I like to use this api through User interface and set the access type</span><br style="font-size:12.8px"><span style="font-size:12.8px">bearer for this service in the keycloak admin.</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">see the example:</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">  var express = require(&#39;express&#39;);</span><br style="font-size:12.8px"><span style="font-size:12.8px">    var apiRoutes = express.Router();</span><br style="font-size:12.8px"><span style="font-size:12.8px">    var User = require(&#39;../models/user&#39;);</span><br style="font-size:12.8px"><span style="font-size:12.8px">    var jwt = require(&#39;jsonwebtoken&#39;);</span><br style="font-size:12.8px"><span style="font-size:12.8px">    var faker = require(&#39;faker&#39;);</span><br style="font-size:12.8px"><span style="font-size:12.8px">    var session = require(&#39;express-session&#39;);</span><br style="font-size:12.8px"><span style="font-size:12.8px">    var Keycloak = require(&#39;keycloak-connect&#39;);</span><br style="font-size:12.8px"><span style="font-size:12.8px">    var hogan = require(&#39;hogan-express&#39;);</span><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">    var memoryStore = new session.MemoryStore();</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">    var keycloak = new Keycloak({store: memoryStore});</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">        app.use(session({</span><br style="font-size:12.8px"><span style="font-size:12.8px">            secret: app.get(&#39;superSecret&#39;),</span><br style="font-size:12.8px"><span style="font-size:12.8px">            resave: false,</span><br style="font-size:12.8px"><span style="font-size:12.8px">            saveUninitialized: true,</span><br style="font-size:12.8px"><span style="font-size:12.8px">            store: memoryStore</span><br style="font-size:12.8px"><span style="font-size:12.8px">        }));</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">        app.use(keycloak.middleware({</span><br style="font-size:12.8px"><span style="font-size:12.8px">            logout: &#39;/logout&#39;,</span><br style="font-size:12.8px"><span style="font-size:12.8px">            admin: &#39;/&#39;</span><br style="font-size:12.8px"><span style="font-size:12.8px">        }));</span><br style="font-size:12.8px"><span style="font-size:12.8px"> app.get(&#39;/api/user&#39;,* keycloak.protect()*, function (req, res) {</span><br style="font-size:12.8px"><span style="font-size:12.8px">            res.json({</span><br style="font-size:12.8px"><span style="font-size:12.8px">                name: faker.name.findName(),</span><br style="font-size:12.8px"><span style="font-size:12.8px">                email: faker.internet.email(),</span><br style="font-size:12.8px"><span style="font-size:12.8px">                address: faker.address.streetAddress(),</span><br style="font-size:12.8px"><span style="font-size:12.8px">                bio: faker.lorem.sentence(),</span><br style="font-size:12.8px"><span style="font-size:12.8px">                image: faker.image.avatar()</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">  });</span><br style="font-size:12.8px"><span style="font-size:12.8px">        });</span><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Keycloak.json:</span><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">{</span><br style="font-size:12.8px"><span style="font-size:12.8px">  &quot;realm&quot; : &quot;nodejs-example&quot;,</span><br style="font-size:12.8px"><span style="font-size:12.8px">  &quot;realm-public-key&quot; :</span><br style="font-size:12.8px"><span style="font-size:12.8px">&quot;</span><wbr style="font-size:12.8px"><span style="font-size:12.8px">MIGfMA0GCSqGSIb3DQEBAQUAA4GNAD</span><wbr style="font-size:12.8px"><span style="font-size:12.8px">CBiQKBgQCrVrCuTtArbgaZzL1hvh0x</span><wbr style="font-size:12.8px"><span style="font-size:12.8px">tL5mc7o0NqPVnYXkLvgcwiC3BjLGw1</span><wbr style="font-size:12.8px"><span style="font-size:12.8px">tGEGoJaXDuSaRllobm53JBhjx33UNv</span><wbr style="font-size:12.8px"><span style="font-size:12.8px">+5z/</span><wbr style="font-size:12.8px"><span style="font-size:12.8px">UMG4kytBWxheNVKnL6GgqlNabMaFfP</span><wbr style="font-size:12.8px"><span style="font-size:12.8px">LPCF8kAgKnsi79NMo+</span><wbr style="font-size:12.8px"><span style="font-size:12.8px">n6KnSY8YeUmec/</span><wbr style="font-size:12.8px"><span style="font-size:12.8px">p2vjO2NjsSAVcWEQMVhJ31LwIDAQAB</span><wbr style="font-size:12.8px"><span style="font-size:12.8px">&quot;,</span><br style="font-size:12.8px"><span style="font-size:12.8px">  &quot;auth-server-url&quot; : &quot;</span><a href="http://xxxx:9090/auth" rel="noreferrer" target="_blank" style="font-size:12.8px">http://xxxx:9090/auth</a><span style="font-size:12.8px">&quot;,</span><br style="font-size:12.8px"><span style="font-size:12.8px">  &quot;ssl-required&quot; : &quot;external&quot;,</span><br style="font-size:12.8px"><span style="font-size:12.8px">  &quot;resource&quot; : &quot;nodejs-connect&quot;,</span><br style="font-size:12.8px"><span style="font-size:12.8px">  &quot;public-client&quot; : true</span><br style="font-size:12.8px"><span style="font-size:12.8px">}</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Thanks,</span><br style="font-size:12.8px"><span style="font-size:12.8px">Deepak</span><br></div>