OAuth oidc getting access to user info on server side in SSR react app

Viewed 427

I have SSR react app using redux-oidc I can successfuly log-in using IdentityServer4 as an authorization server with PKCE Authorization Code Flow. I can get user data on client side of react application but I don't know how to do it on server side of same react app. I want to show logged-in version of page on first load and for that I need to get user data during the first request.

const Koa = require('koa')
const next = require('next')
const Router = require('koa-router')
const jwt = require('koa-jwt')
const jwksRsa = require('jwks-rsa')

const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare().then(() => {
  const server = new Koa()
  const router = new Router()

  server.use(jwt({
    secret: jwksRsa.koaJwtSecret({
      jwksUri: 'http://localhost:5000/.well-known/openid-configuration/jwks'
    }),
    issuer: 'http://localhost:5000',
    algorithms: [ 'RS256' ],
    passthrough: true
  }));

  router.get('*', async ctx => {
    console.log(ctx.state)
    console.log(ctx.header.cookie)
    await handle(ctx.req, ctx.res)
    ctx.respond = false
  })

  server.use(async (ctx, next) => {
    ctx.res.statusCode = 200
    await next()
  })

  server.use(router.routes())
  server.listen(port, () => {
    console.log(`> Ready on http://localhost:${port}`)
  })
})

Logicaly only data I can work with during first request are cookies I have just these three .AspNetCore.Identity.Application (id token), idsrv.session and CookieConsent.

How to use them to get user data? User info endpoint requires access token which I don't have because it is stored in sessionStorage on client.

There must be something I am missing.

0 Answers
Related