Express (with Single-Page App frontend)
Integrating an Express server with sgID
Last updated
Integrating an Express server with sgID
Last updated
This page provides a step-by-step guide on how to integrate the TypeScript SDK in a simple Express server. This Express server will be used as a backend server for a SPA frontend.
To illustrate our example, we have prepared a demo app which will allow you to retrieve your user's name and favorite ice cream flavor after they log in with sgID.
If you have not already obtained your client credentials via registration, please register your client before proceeding.
For this example, you should add:
1. [openid, myinfo.name]
as the scopes and
2. http://localhost:5001/api/redirect
as a redirect URL
To run the example locally, clone from our source code by running:
Update your .env
file with your client credentials.
In separate terminals, run the frontend and the backend.
Ensure that your backend Express server is running on http://localhost:5001 and visit http://localhost:5173.
If you click on 'Login with Singpass' and authenticate with your Singpass mobile app, you should see your user info on the success screen.
In this section, we'll break down the different steps that our example app goes through.
In this step, we will create an instance of our SgidClient
class which will help us to interface with the sgID server.
In the .env
file created from the previous step, fill out your sgID credentials.
The main idea here is to load your sgID credentials in a secure way using environment variables instead of hard-coding them into your app.
Next, initialize the SDK by calling the constructor and passing in the environment variables.
Before we can create the endpoints, we will need to configure the Express app.
When an end user clicks on the sign in button on your application (e.g. 'Login with Singpass app'), it should make a GET
request to this endpoint to retrieve the authorization URL. The browser is then redirected to this authorization URL.
The /api/auth-url
endpoint should do the following
Generate a session ID
Generate a PKCE pair (consisting of code challenge and code verifier)
Generate an authorization URL
Store the code verifier in the session
Set the session ID in the browser's cookies
Return the authorization URL
After the user scans the QR code with their Singpass mobile app and authorizes your application to access the specified scopes, the sgID server will redirect the user's browser to the redirect_uri
you specified earlier (either when initializing the SDK or when passed as a parameter to the authorizationUrl
function).
The redirect will include the authorization code and the state (if provided earlier) in the form of query parameters. An example URL would look something like this
The /api/redirect
endpoint should do the following
Retrieve the authorization code from query params, and the session ID from browser cookies
Retrieve the code verifier from session
Exchange the authorization code and code verifier for the access token
Store the access token and sub in session
Redirect the browser to a logged in page (or any page of your choice)
The sub
is an end-user's unique sgID identifier.
If your application only needs to verify that a user is a real person with a Singpass account without needing to access any government-verified data, then you can stop here (after Step 3) and utilize the sub
value to identify the user.
Once the browser has been redirected to a logged in/success page, your app can make a GET
request to this endpoint which will use the access token stored in session to request user info from the sgID server.
The /api/userinfo
endpoint should do the following
Retrieve the session ID from browser cookies
Retrieve the access token from memory using the session ID
Request user info using the access token
Return the user info
Now that your Express server has been set up properly, you will need to integrate your frontend application with it.
If you have followed the steps from Running the example locally, the frontend and backend examples have already been integrated for you.
However, if you would like to integrate with your own frontend application, there are two main steps you need to implement:
A page with a 'Login with Singpass' button
Click here for the relevant code in the frontend repo.
The button will need to make a GET
request to the /api/auth-url
endpoint and then redirect the browser to the received authorization URL.
Fetching the user info after logging in
Click here for the relevant code in the frontend repo.
After the user logs in, the frontend can make a GET
request to the /api/userinfo
endpoint to retrieve the user info.
You have reached the end of the Express step-by-step guide.
While these examples should work seamlessly in a local environment (i.e. localhost), they may not work if deployed (specifically if the frontend and backend are deployed on different domains).
This is due to the SameSite
attribute on cookies. For these examples to work in a deployed environment, you would need to either
Utilize a reverse proxy to deploy the frontend and backend on the same domain; or
Set the SameSite
attribute as None
to be able to set cookies on a different domain
If you want to find out more about how sgID works, click here to learn about the sgID protocol.
If you have more questions about sgID, check out our FAQ for answers to common questions.