Today, I’d like to show you the power of Node.js with a simple login example. Express.js is one of the oldest and most-used server-side MVC frameworks for Node.js. Okta happens to provide an OIDC Middleware package that integrates with Express to secure your app with login and logout functionality.
To show how it all works, I created a screencast: Node.js Login with Express and OIDC.
|OktaDev Schematics was used in the making of this video. See its Express documentation to reproduce the steps yourself, or follow the instructions below.|
Create an Express App and Add Login in 5 Steps
Create a new project with express-generator and pug for the view.
mkdir express-app cd express-app npx express-generator --view=pug
Add a new OIDC App on Okta. After you log in to your dashboard, go to Applications > Add Application. Choose Web and use
http://localhost:3000/callbackfor the login redirect;
http://localhost:3000for the logout redirect.
Install Schematics CLI:
npm install -g @angular-devkit/schematics-cli
Install and run OktaDev Schematics with your Okta values for
npm i @oktadev/schematics schematics @oktadev/schematics:add-auth --issuer=$issuer --clientId=$clientId --clientSecret=$clientSecret
.envfile will be generated with your client secret in it. Make sure to add
.gitignoreand don’t check it into source control!
Start your app and authenticate with Okta! 🚀
I hope you enjoyed this quick tour of how to add a login feature to a Node.js application. If you’re looking to do more with Okta and Node.js, check out our Node.js SDK. It allows you to communicate with Okta APIs to manage your users and their identity.
You can find the source code for this example on GitHub @oktadeveloper/okta-nodejs-login-example.
If you liked this screencast, you might enjoy our other tutorials too.