Implement Visitor Authentication using Node and Okta
Last updated
Last updated
In this guide, we will show you how to set up Visitor Authentication using Okta and Node.
git
, node
, and npm
are installed on your computer. Familiarity with the terminal (or command line). You can learn how to install these tools here: Git and Node. NPM is bundled with Node.
First, sign in to Okta platform (the admin version) and create a new app integration (or use an existing one) by clicking the Applications button in the left sidebar.
Click Create App Integration and select OIDC - OpenID Connect as the Sign-In method. And then select Web Application as the application type.
Name it appropriately and don't edit any other setting on that page. For assignments, choose the appropriate checkbox. Click Save.
Now, we will create the backend responsible for authenticating the visitors to your space.
On your computer, clone the git repository by running
git clone https://github.com/GitbookIO/okta-visitor-authentication-example
in the directory (folder) you want to be working from. Open the folder in your favorite code editor (say, VS Code).
We will edit the server.js
file and enter the details of our Okta application there.
The oidc
object should look like
For issuer
field, look at the drop down menu in the top right of the Okta dashboard. Copy the URL right below your email address.
Paste the value in the issuer
field of the oidc
object in server.js
in your code editor. Add https://
to the beginning of the URL and/oauth2/default
to the end of the URL. Your issuer
field should look like the following:
Note that your URL will be different from the ones written and shown above.
For the client_id
field, copy the value from the application's page in Okta dashboard
And paste it in the client_id
field of the oidc
object in server.js
in your code editor. Your client_id
field should look like the following:
Note that your client_id
will be different from the ones written and shown above.
Similarly for client_secret
, copy the value of Client Secret from the application page in your Okta dashboard and paste it in the client_secret
field of the oidc
object in server.js
in your code editor. Your client_secret
field should look like the following:
Note that your client_secret
will be different from the one written above.
Your oidc
object should now look like the following:
Now, we need to use GitBook. Go to the space you want to publish behind visitor authentication. Open the Share modal and click "Share to an audience", and enable the "Publish with Visitor Authentication" toggle.
Make note of the Private key and the Space URL. We will need them.
Enter http://localhost:8080/login
as the Fallback URL. Note that this is different from the one shown in the image above.
Go back to your code editor and in the following line
Replace gitbook signing key
with the Private key you copied. This line should look something like:
Note that your signing key will be different from the one entered above.
In your code editor, in the following line
Replace everything before ?
with the Space URL you copied from the GitBook Share modal. Make sure there's only one /
right before the ?
.
Save the server.js
file.
Open up the terminal and make sure you're in the okta-visitor-authentication-example
directory.
Run npm install
which will install the dependencies of our project, including the library needed for communicating with Auth0.
After the installation of dependencies is complete, run node server.js
from the command line. If successful, you will see the following message:
Your Visitor Authentication setup is now complete! If you visit your published space URL now, you will be prompted to sign in using Okta.