Search code examples
javascriptreactjswebpackcreate-react-app

Adding silent renew entry point to React(create-react-app)


I have a React application created using the create-react-app module. I have recently been asked by a client to integrate with oidc. For this purpose I'm using redux-oidc, as I already have redux working in my app as well.

We managed to integrate my application into their Identity server and I'm able to sign in and get the user token stored in redux. The problem is that I'm struggling to setup silent renew in my create-react-app application as I have to add an additional entry point. Is there a way to add an additional entry point to silent_renew/index.js without ejecting create-react-app?

Currently I've create a folder called silent_renew containing an index.js file. This folder also contains a silent_renew.html file with not much in it (See: example app similar to my folder structure).


Solution

  • Since the landing page for silent_renew is a just a simple html page, you could bypass webpack. Just put the following file in the public folder. Also, include a copy of the oidc-client.min.js library in the same folder.

    public/silent_renew.html:

    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
      <script src="oidc-client.min.js"></script>
      <script>
          new Oidc.UserManager().signinSilentCallback().then()
      </script>
    </body>
    </html>
    

    This works at my site in the develepment config. For the production config I have the following in mind (I did not test it yet but I'm pretty confident this is the way forward...).

    public/index.js

    const express = require('express')
    const path = require('path')
    const app = express()
    
    app.use(express.static('.'))
    
    app.use((req, res, next) => {
      if (path.extname(req.path).length > 0) {
        next()
      } else if (path.dirname(req.path).indexOf('silent_renew') > -1) {
        req.url = '/silent_renew.html'
        next()
      }
      else if (path.dirname(req.path).indexOf('callback') > -1) {
        req.url = '/callback.html'
        next()
      } else {
        req.url = '/index.html'
        next()
      }
    })
    
    app.listen(3000)
    

    As soon as create-react-app supports multiple entry points (I hope this happens soon for enterprise login scenario's) this code becomes obsolete.