This tutorial shows how to use PlusAuth with ExpressJS. If you do not have a PlusAuth account, register from here.
Create PlusAuth Client
After you sign up or log in to PlusAuth, you need to create a client to get the necessary configuration keys in the dashboard. Go to Clients and create a client with the type of Regular Web Application
Configure Client
Get Client Properties
You will need your Client Id
and Client Secret
for interacting with PlusAuth. You can retrieve them from the created client's details.
Configure Redirect and Logout URIs
When PlusAuth authenticates a user, it needs a URI to redirect back. That URI must be in your client's Redirect URI
list. If your application uses a redirect URI that is not white-listed in your PlusAuth Client, you will receive an error.
The same thing applies to the logout URIs. After the user logs out, you need a URI to be redirected.
Configure Node.js to use PlusAuth
Let's start to create an ExpressJS application.
Create the .env file
Create the .env
file in the root of your app and add your PlusAuth variables and values to it.
Install the dependencies
To get started, install the following dependencies.
- passport - an authentication middleware for Node.js
- openid-client - an PlusAuth authentication strategy for Passport
- express-session - a middleware to manage sessions
- dotenv - a module to load environment variables from a
.env
file - ejs - a simple yet powerful template engine for creating views
Configure Express Application
We will configure our Express application in a simple way. We will be using EJS as template engine.
Configure express-session
Include the express-session
module and configure it in app.js
. The secret
parameter is a secret string that is used to sign the session ID cookie. Please use a custom value.
Configure Passport with the application settings
Include the passport
and openid-client
modules in app.js
. Configure Passport to use a PlusAuth Client with your settings. Use passport.initialize()
and passport.session()
to initialize Passport with persistent login sessions.
Passing the scope
parameter to openid-client
strategy with values openid email profile
is necessary to access email and the other attributes stored in the user profile.
Please make sure you add passport middlewares in your code after the express middleware (app.use(session(sessionOptions)
).
Storing and retrieving user data from the session
In a typical web application, the credentials used to authenticate a user are only transmitted during the login request. If authentication succeeds, a session would be established and maintained via a cookie set in the user's browser. Each subsequent request does not contain credentials but rather the unique cookie that identifies the session.
To support login sessions, Passport serializes and deserializes user instances to and from the session. Optionally, you may want to serialize only a subset to reduce the footprint, i.e., user.id
.
Middleware to protect routes
Create an isLoggedIn
middleware to protect routes and ensure they are only accessible if logged in.
Implement login, user profile, and logout
In this example, the following routes are implemented:
/auth/login
triggers the authentication by calling Passport'sauthenticate
method. The user is then redirected to the tenant login page hosted by PlusAuth./auth/callback
is the route that the user is returned to by PlusAuth after authenticating. It redirects the user to the profile page (/user
)./profile
displays the user's profile./auth/logout
logs the user out of PlusAuth./auth/logout/callback
is the route that the user is returned to by PlusAuth after logging out.
Adding the authentication routes
Below, you will find routes related to authentication.
Create the user profile route
The /profile
route (the user's profile) should only be accessible if the user is logged in. We will be using authentication middleware we created in the step Middleware to protect routes
Index route
Let's create an index route to serve our application's homepage.
Making the user available in the views
In the views and layouts, it is often necessary to conditionally render content depending on if a user is logged in or not. Other times, the user object might be required to customize the view.
Create a middleware lib/middleware/userInViews.js
for this purpose.
Create Views
Homepage
Create a views/index.ejs
template.
User Profile
Create a views/profile.ejs
template.
See it in action
That's it. Start your app and point your browser to http://localhost:3000. Follow the Log In link to log in or sign up to your PlusAuth tenant. Upon successful login or signup, you should be redirected back to the application.