Skip to content
Scalekit Docs

SaaSKit: Add auth to my app

SaaSKit — Hosted auth pages, managed sessions, secure logout. Purpose built. Simple where it counts

You’ll implement sign-up, login, and logout flows with secure session management and user management included. The foundation you build here extends to features like workspaces, enterprise SSO, MCP authentication, and SCIM provisioning.

See DemoPlay
See the integration in actionPlay
Review the authentication sequence

Scalekit handles the complex authentication flow while you focus on your core product:

Full-Stack Authentication Flow

  1. User initiates sign-in - Your app redirects to Scalekit’s hosted auth page
  2. Identity verification - User authenticates via their preferred method
  3. Secure callback - Scalekit returns user profile and session tokens
  4. Session creation - Your app establishes a secure user session
  5. Protected access - User accesses your application’s features

Build with a coding agent

Terminal
npx @scalekit-inc/cli setup

Paste a task that names implementing-saaskit. For example:

Example prompt
Use implementing-saaskit to add login, sessions, and logout.

  1. Use the following instructions to install the SDK for your technology stack.

    npm install @scalekit-sdk/node

    If you haven’t already, add your Scalekit credentials to your environment variables file:

    .env
    SCALEKIT_ENVIRONMENT_URL=<your-environment-url>
    SCALEKIT_CLIENT_ID=<your-client-id>
    SCALEKIT_CLIENT_SECRET=<your-client-secret>
    COOKIE_ENCRYPTION_SECRET= # openssl rand -base64 32 — encrypts the session cookie
    REDIRECT_URI=http://localhost:3000/callback

    You need to register redirect URLs for your application. Go to Scalekit dashboardAuthenticationRedirect URLs and configure:

    • Allowed callback URLs: The endpoint where users are sent after successful authentication to exchange authorization codes and retrieve profile information. Learn more
    • Initiate login URL: The endpoint in your app that redirects users to Scalekit’s /authorize endpoint. Required when authentication is not initiated from your app, for example, when a user accepts an organization invitation or starts sign-in directly from their identity provider (IdP-initiated SSO). Learn more
  2. An authorization URL is an endpoint that redirects users to Scalekit’s sign-in page. Use the Scalekit SDK to construct this URL with your redirect URI and required scopes.

    server.ts
    import express from 'express';
    import { ScalekitAuth } from '@scalekit-sdk/node/express';
    const auth = new ScalekitAuth({
    envUrl: process.env.SCALEKIT_ENVIRONMENT_URL,
    clientId: process.env.SCALEKIT_CLIENT_ID,
    clientSecret: process.env.SCALEKIT_CLIENT_SECRET,
    redirectUri: process.env.REDIRECT_URI,
    cookieEncryptionSecret: process.env.COOKIE_ENCRYPTION_SECRET,
    });
    const app = express();
    app.use(auth.router);
    app.get('/account', auth.requiresAuth, (req, res) => {
    res.json({ sub: req.scalekitUser?.sub });
    });
    app.listen(3000);

    Open http://localhost:3000/account. A missing session goes to /login. After login, the visitor returns to /account. Register http://localhost:3000/callback as the dashboard Redirect URI.

    This redirects users to Scalekit’s managed sign-in page where they can authenticate. The page includes default authentication methods for users to toggle between sign in and sign up.

  3. After successful authentication, Scalekit creates a user record and sends the user information to your callback endpoint. In authentication flow, Scalekit redirects to your callback URL with an authorization code. Your application exchanges this code for the user’s profile information and session tokens.

    app.use(auth.router); // GET /callback writes sk_session

    The authResult object contains:

    • user - Common user details with email, name, and verification status
    • idToken - JWT containing verified full user identity claims (includes: sub user ID, oid organization ID, email, name, exp expiration)
    • accessToken - Short-lived token that determines current access context (includes: sub user ID, oid organization ID, roles, permissions, exp expiration)
    • refreshToken - Long-lived token to obtain new access tokens
    {
    user: {
    email: "john.doe@example.com",
    emailVerified: true,
    givenName: "John",
    name: "John Doe",
    id: "usr_74599896446906854"
    },
    idToken: "eyJhbGciO..", // Decode for full user details
    accessToken: "eyJhbGciOi..",
    refreshToken: "rt_8f7d6e5c4b3a2d1e0f9g8h7i6j..",
    expiresIn: 299 // in seconds
    }

    The user details are packaged in the form of JWT tokens. Decode the idToken to access full user profile information (email, name, organization ID) and the accessToken to check user roles and permissions for authorization decisions. See Complete login with code exchange for detailed token claim references and verification instructions.

  4. The access token is a JWT that contains the user’s permissions and roles. It expires in 5 minutes (default) but can be configured. When it expires, use the refresh token to obtain a new access token. The refresh token is long-lived and designed for this purpose.

    The Scalekit SDK provides methods to refresh access tokens automatically. However, you must log the user out when the refresh token itself expires or becomes invalid.

    app.get('/account', auth.requiresAuth, (req, res) => {
    res.json({ sub: req.scalekitUser?.sub });
    });

    This sets browser cookies with the session tokens. Every request to your backend needs to verify the accessToken to ensure the user is authenticated. If expired, use the refreshToken to get a new access token.

    app.get('/dashboard', auth.requiresAuth, (req, res) => {
    res.json({ user: req.scalekitUser });
    });

    Authenticated users can access your dashboard. The app enforces session policies using session tokens. To change session policies, go to Dashboard > Authentication > Session Policy in the Scalekit dashboard.

  5. Session persistence depends on the session policy configured in the Scalekit dashboard. To log out a user, clear local session data and invalidate the user’s session in Scalekit.

    <a href="/logout">Log out</a>

    The logout process completes when Scalekit invalidates the user’s session and redirects them to your registered post-logout URL.

This single integration unlocks multiple authentication methods, including Magic Link & OTP, social sign-ins, enterprise single sign-on (SSO), and robust user management features. As you continue working with Scalekit, you’ll discover even more features that enhance your authentication workflows.