is a platform to make your app enterprise-ready, quickly adding common features like SSO/SAML, Directory Sync, Audit Trail, and more. It's like "Stripe for enterprise features."
In this recipe, I’ll show you how to integrate a ScaffoldHub generated application with WorkOS.
For the sake of this recipe, I’ll use the G Suite and Okta single sign-on integrations, but WorkOS allows you to integrate with .
Thanks to WorkOS, from the ScaffoldHub perspective, what differs from one provider to another is only the domain variable we send to WorkOS.
The integrated application
The integrated application will have those two ways of sign-in/sign-up:
Okta
G Suite
This ensures that your application is only available for your enterprise users.
GitHub repository
Repository:
Differences:
If you own a ScaffoldHub license and don't have access to the repository yet, please email us at support@scaffoldhub.io with your GitHub profile.
Requirements
Create a WorkOS account
Single Sign-On (SAML) setup
Once you sign-in, on the Single Sign-On (SAML), click on Get Started.
Install the WorkOS SDK
Open a new console at the backendfolder of your project and run:
npm install --save @workos-inc/node
Add your SSO Redirects URIs
Add this as the Redirect URI:
We will configure this callback later.
Set up your Identity Providers
Configuring the backend
This section describes the changes needed on the backend for the integration. The direction of changes is from the API -> Database.
Environment Variables
First things first, we must set the environment variables with the WorkOS configuration.
The sign-in endpoint is responsible for getting the authorization URL of the provider - G Suite, Okta, etc - and redirect the user to its sign-in page.
Note the req.query.domain that receives the domain the user wants to authenticate from the front-end.
import ApiResponseHandler from '../apiResponseHandler';
import WorkOS from '@workos-inc/node';
import { getConfig } from '../../config';
import { databaseCloseIfIndividualConnectionPerRequest } from '../../database/databaseConnection';
const client = new WorkOS(getConfig().WORKOS_SECRET_KEY);
export default async (req, res) => {
try {
const url = client.sso.getAuthorizationURL({
domain: req.query.domain,
redirectURI: getConfig().WORKOS_REDIRECT_URI,
projectID: getConfig().WORKOS_PROJECT_ID,
});
// This is unrelated to WorkOS, but because we are not calling
// the ApiResponseHandler, we must close the database connection
// if needed
await databaseCloseIfIndividualConnectionPerRequest(
req,
);
res.redirect(url);
} catch (error) {
await ApiResponseHandler.error(req, res, error);
}
};
backend/src/api/workos/workosCallback.ts
The AuthService.signinWithWorkos method performs the operations needed on the database and returns a JWT token.
The user is then redirected to the frontend page with the auth token.
If an error occurs, it is redirected to the application sign-in page with the error message passed as a parameter.
At this point, you have probably already tested the integration via the application, but you can also run this on the WorkOS setup process, and it will work.
Finish
Well done, your application is now enterprise-ready.
Please follow the section and make sure your application is up and running on the localhost.
Go to and create a new account.
More about WorkOS keys: .
The callback endpoint receives a code that enables us to fetch the user profile of the authenticated user. This allows us to find or create the user on our database using the WorkOS information: .