Express
Authentication for Express.JS apps with Authgear and OAuth2
Authgear makes it easy to add user authentication to a regular web app that is not powered by any framework. You can do this by integrating Authgear into your application as an OIDC identity provider.
In this post, you'll learn how to add user authentication to an Express.js application using Authgear.
What You Will Learn
How to create an Authgear Application.
How to add User Login to an Express app using Authgear.
How to request user info from Authgear.
Pre-requisites
You'll need the following to follow along with this tutorial:
Node.js Installed
A free Authgear account. Sign up for one here.
Follow this guide to add user authentication to an Express application using Authgear in 🕐 15-minute.
Check out and clone the Sample Project on GitHub.
Setup Application in Authgear
Login to your Authgear account (you can sign up for one here) to perform the steps in this section.
Create an application in the Portal
In this step, we'll create the Authgear client application that we'll use later to connect Authgear to the Express application.
After you log in to Authgear Portal, select a project then navigate to Applications on the left menu bar.
Next, click on the ⊕Add Application button on the top toolbar to open the New Application page.
Enter an application name (for example: "My App") and select OIDC/SAML Client Application
as the Application Type. Click Save to create the app.
On the next screen, you'll see links to tutorials for different frameworks. Click Next to skip to the application configuration page.
Note the Client ID and Client Secret for your new application as you'll use in a later step to configure your Express app.
Configure Authorize Redirect URI
The Authorized Redirect URI should be a page on your Express application where you make an HTTP(S) request to the Authgear token endpoint and exchange an Authorization Code for an Access Token.
For our demo Express application for this guide, this URL will be http://localhost:3000/auth-redirect
. So, scroll to the URI section of your application configuration page in the Authgear Portal and add http://localhost:3000/auth-redirect
as an authorized redirect URI.
Click Save to keep your changes.
How to Add User Authentication to Express.js App using Authgear
In this section, we'll be building a simple Express app that has the following features:
A landing page with a login link that takes users to the login route.
A
/start-login
route that initiates the authentication flow.Logic that exchanges the authorization code from Authgear for an access token.
A page that uses that access token to fetch user info from Authgear.
A logout button to end the current user session.
Refresh access token.
Step 1: Create Express App
It is now time to create the Express app that will be connecting to our Authgear client application. Run the following commands to create a new folder for the project and set it as your current working directory:
Install basic project dependencies
Next, install the Express npm package and other dependency packages like axios
, nodemon
, session
, and dotenv
.
We'll be using the axios
library to make HTTP requests in the example app. We'll also use nodemon
to add hot reload to our development environment. Run the following commands from the root of your Express project directory to install Express and other dependencies:
Create app.js
file
app.js
fileOnce Express and the other dependencies are installed, create a new app.js
file in the root of your project folder and add the following code to the file:
Update package.json
Add the following code to the scripts
section of the package.json
file in the root directory of your Express project:
Checkpoint
Now run the npm run dev
command and you should get a page like this on a web browser when you visit localhost:3000
:
Step 2: Implement Login Route
Here we'll be implementing the login route in our Express app. This route will initiate the authentication flow by redirecting the user's browser to the login page (AuthUI). This process involves redirecting the user to your Authgear project/client application's authorization URL (https://<AUTHGEAR_ENDPOINT>/oauth2/authorize
).
Update the code for the app.get("/login", ...)
route to the following:
Create a .env
file on the root directory of your project and add your Authgear client application configuration (Client ID, secret, endpoint) using the following fields:
You can get the value for Client ID and Client Secret from the configuration page for the client application you created in the earlier step, Create an application in the Portal. The Authgear Endpoint looks like https://project_id.authgear.cloud
if you are using Authgear Cloud version.
Now if you save your code and restart your app, clicking on the Login link should redirect to the Authgear authorization page.
On successful login, an authorization code will be sent back to your Express application.
Step 3: Exchange Authorization Code For Access Token
After the user logs in and grants authorization to your app on the Login page, they are redirected back to the redirect URL you specified earlier. In addition to this redirect, an authorization code is sent via a code
URL query parameter.
In this step, we will be exchanging the authorization code for an access code that users can later use to access protected resources.
Update the code for the app.get("/auth-redirect", ...)
route to the following:
The above code sends an HTTP POST request to the token endpoint (https://<AUTHGEAR_ENDPOINT>/oauth2/token
). The authorization code we got from the previous step is sent along with other client credentials in the HTTP request body. The header should contain "Content-Type": "application/x-www-form-urlencoded"
A valid access token is returned in the response to the HTTP(S) request in response.data.access_token
. In the above code sample, we've saved this access token temporally using express-session so we can access it from "/"
route after redirecting the user.
We can now use this access token to make authenticated requests to protected resources in our app or from the Authgear User Info endpoint. In the next step, we'll attempt to get the current user's info from Authgear using the access token.
Step 4: Get User Info
Authgear provides an endpoint where your application can request user info (https://<AUTHGEAR_ENDPOINT>/oauth2/userinfo
). This endpoint will return the user's details on Authgear like their email address, gender, full name, and more.
To get user info in our example app, update the app.get("/", ...)
app.js
, to use the following conditional statement to render a different block of code when a valid access token is set for the current session:
Here our app sends another HTTP(S) request, but this time to the user info endpoint and the request type is GET. The access token is sent as a Bearer authorization header.
At this point, save all changes and restart the application. Try logging in all over again, at the end you should be greeted with "Welcome [your email address]" and a dump of the response from the UserInfo endpoint if the access token in your authorization header is valid.
Step 5: Logout
To allow your users log out, add a new /logout
route to your Express application. Within the route, you'll implement code that will call express-session destroy()
method to delete the access token from the session. Then you'll redirect the user to Authgear's logout endpoint to log the user's session on Authgear.
To redirect the user back to your Express app after they logout, add http://localhost:3000
as a Post Logout Redirect URI for your client application in Authgear Portal.
Your implementation of the logout route should look like this:
Finally, place a link to the logout route below the </prev>
tag using the following code:
Step 6: Refresh Access Token
An access token is usually only valid for a short period. However, you can use the refresh token, also included in the response from the token endpoint in step 3 to request a new access token.
Note that you must include offline_access
in the scope parameter of your authorization request to get a refresh token from Authgear.
We recommend that you check that an access token is still valid before using it in your authorization header to access protected resources like the User Info endpoint.
To add the capability of refreshing an expired access token to our Express app, add the following function to app.js:
Next, find the following line within the app.get("/auth-redirect", ...)
route:
Add the following code just after the above line:
The above code will store the values of refresh_token
and expires_in
that were returned in step 3 in express-session. To convert expire_in to a time in the future, we multiply it by 1000 and add that to the current time.
Finally, find the following line in the app.get("/", ...)
route:
Replace the above line with the following code:
Now our Express app gets an access token by first calling the new refreshAccessTokenIfExpired()
method. If the current access token is expired, the method will make a request to Authgear's token endpoint for a new access token. This request needs to have a grant_type
of refresh_token
, and the refresh_token
should be included in the POST request body.
See more about refreshing access tokens here.
Next steps, Calling an API
To access restricted resources on your backend application server, the HTTP requests should include the access token in their Authorization headers.
For example:
Conclusion
And there you have it, you've successfully added user authentication to your Express app using Authgear as the OAuth provider.
You can add so much more to your app with the new Authgear authentication, like protecting your own app endpoint with the access code. You can also store the access token securely to persist the user session using express-session and cookies.
Here's a link to the complete code for our example code on Github.
Last updated