How to add customer sign-in and single sign-on to a SaaS app
Sign-in is the part of a B2B product that every customer touches on day one and that nobody buys the product for. This guide explains the standard flow, what your app must check, and how to keep each customer’s people apart.
7 min read · Last checked
In short
Key points
- Use OpenID Connect with the authorization code flow and PKCE. It works for server-rendered apps, single-page apps and mobile apps.
- Your app must validate the ID token it receives: issuer, audience, expiry and the nonce it sent.
- In a B2B product, signing in is not enough. Every request also needs to know which customer organization the person is acting in.
- Enterprise SAML and directory sync are a separate requirement. Decide early whether your first customers need them.
What single sign-on means for a SaaS product
The phrase covers two different jobs. The first is giving your own customers one reliable way to sign in to your app. The second, usually asked for by larger buyers, is letting a customer’s staff sign in with the company identity system they already use, such as Okta or Microsoft Entra ID.
Both jobs start with the same building block: an identity provider that your app trusts, speaking a standard protocol. Today that protocol is OpenID Connect, which sits on top of OAuth 2.0. Building on it means you can use a maintained client library in almost any language instead of writing your own session and password handling.
The flow: authorization code with PKCE
In the authorization code flow your app sends the person to the identity provider. After they sign in, the provider sends them back to your app with a short-lived code. Your app exchanges the code for tokens in a direct call, so the tokens never travel through the browser address bar.
PKCE (Proof Key for Code Exchange, RFC 7636) closes a gap in that flow. Before redirecting, your app creates a random secret and sends only its hash. When it redeems the code it sends the original secret, and the provider checks the two match. Someone who intercepts the code cannot use it without the secret. The S256 method, which uses a SHA-256 hash, is the one to use. Current OAuth security guidance recommends PKCE for all clients that use the code flow, not only for apps that cannot keep a secret.
- Redirect to the provider’s authorization endpoint with response_type=code, your client id, a redirect URI, the scopes you need, a state value, a nonce and the PKCE challenge.
- Handle the callback: check that state matches what you stored, then redeem the code with the PKCE verifier.
- Store the session in a secure, HTTP-only cookie for a server-rendered app. A single-page app should keep tokens out of long-lived browser storage where it can.
What your app must validate
A library does most of this for you, but know what it is doing, because a misconfiguration quietly turns checks off.
- Issuer: the iss claim must be the provider you expect.
- Audience: the aud claim must contain your client id, so a token issued to another app is rejected.
- Expiry: reject tokens whose exp has passed, with at most a small allowance for clock difference.
- Nonce: the nonce claim must match the one your app sent, which ties the token to this sign-in attempt.
- Signature: verify it against the provider’s published signing keys, which you find through its discovery document at /.well-known/openid-configuration.
Carry the customer organization with every request
A B2B app has a second question that a consumer app does not: which customer is this person acting for? One person may belong to several customer organizations, and the data they may see changes with each one.
Decide where that answer comes from before you write data access code. Two common choices are to put the organization in the token the provider issues, or to look it up from your own database using the person’s stable subject identifier (the sub claim) on each request. Whichever you pick, apply it in one place, such as middleware, and filter every query by the organization it returns. The multi-tenant article linked below covers how to keep that filter from being forgotten.
What this looks like with TenancyEngine
TenancyEngine is the identity provider, so you do not run an identity server. Your app is an OpenID Connect client that uses the code flow with PKCE (S256) and no client secret. The discovery document is at /.well-known/openid-configuration, the default callback path is /signin-oidc, and the scopes are openid, profile, email and api. Your customers’ people can use an authenticator app, an emailed or texted code, or a passkey as a second step.
Each customer gets a workspace with its own members and roles, created from your backend when they sign up. The sample below is the ASP.NET Core setup from the .NET SDK.
// dotnet add package Microsoft.AspNetCore.Authentication.OpenIdConnect
builder.Services
.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.Authority = "https://auth.saasruntime.com";
options.ClientId = "my-app"; // from the console
options.ResponseType = TenancyEngineOidcContract.ResponseType;
options.UsePkce = TenancyEngineOidcContract.RequiresPkce;
options.CallbackPath = TenancyEngineOidcContract.CallbackPath;
options.MapInboundClaims = false; // keep "sub" as "sub"
});Where enterprise SAML fits
When a large customer asks for single sign-on, they usually mean SAML with their own identity provider, and often SCIM so that leavers are removed automatically. These are real requirements for some deals and irrelevant to many others. Ask your first ten prospects before you build or buy for them.
TenancyEngine offers SCIM 2.0 directory sync: you create a one-time setup link for a customer’s IT admin, and people deactivated in their identity provider are suspended and signed out. TenancyEngine does not offer SAML single sign-on, so if you need SAML today, use a specialist identity vendor for that part.
Honest limits
What to know before you rely on this
- TenancyEngine does not offer SAML single sign-on.
- The .NET SDK is the only server SDK with a public registry release today. Other languages use the standard OpenID Connect libraries and the REST API.
- Starter runs in Production only. Separate development and staging environments start on Growth.
Questions people ask
Do I need PKCE if my app is a server-rendered web app with a client secret?
It is still recommended. Current OAuth security guidance advises using PKCE with the authorization code flow for every client, because it protects against a stolen authorization code even when a client secret exists.
Should I store the ID token or the access token in the browser?
Prefer a server-side session in a secure, HTTP-only cookie. If a single-page app must hold tokens, keep them short-lived and out of long-term storage where you can.
Can one person belong to more than one customer organization?
Yes in most B2B products, and you should design for it. Identify the person by their stable subject identifier, not by email alone, and resolve the organization separately for each request.
Sources
Keep reading
Try it on the free plan
One app and five customers, no credit card required.