Skip to content
Talk to an Engineer Dashboard

Web application

Implement Multi-App Authentication for web apps using Authorization Code flow with client_id and client_secret

Implement login, token management, and logout in your web application using the Authorization Code flow. Web applications have a backend server that can securely store a client_secret, allowing them to authenticate directly with Scalekit’s token endpoint. This guide covers initiating login from your backend, exchanging authorization codes for tokens, managing sessions with secure cookies, and implementing logout.

Before you begin, ensure you have:

  • A Scalekit account with an environment configured
  • Your environment URL (ENV_URL), e.g., https://yourenv.scalekit.com
  • A web application registered in Scalekit with client_id and client_secret (Create one)
  • At least one redirect URL configured in Dashboard > Developers > Applications > [Your App] > Redirects
UserWeb app (frontend)Web app (backend)Scalekit Click "Login" GET /login Redirect to /oauth/authorize Redirect to /callback with code + state POST /oauth/token access_token, refresh_token, id_token Set session cookies + redirect
  1. Initiate login by redirecting the user to Scalekit’s hosted login page from your backend. Generate and store a state parameter before redirecting to validate the callback.

    Terminal window
    <ENV_URL>/oauth/authorize?
    response_type=code&
    client_id=<CLIENT_ID>&
    redirect_uri=<CALLBACK_URL>&
    scope=openid+profile+email+offline_access&
    state=<RANDOM_STATE>

    For detailed parameter definitions, see Initiate signup/login.

  2. After authentication, Scalekit redirects the user back to your callback endpoint with an authorization code and the state you sent.

    Your backend must:

    • Validate the returned state matches what you stored — this confirms the response is for your original request and prevents CSRF attacks
    • Handle any error parameters before processing
    • Exchange the authorization code for tokens using your client_secret
    Terminal window
    POST <ENV_URL>/oauth/token
    Content-Type: application/x-www-form-urlencoded
    grant_type=authorization_code&
    client_id=<CLIENT_ID>&
    client_secret=<CLIENT_SECRET>&
    code=<CODE>&
    redirect_uri=<CALLBACK_URL>
    {
    "access_token": "...",
    "refresh_token": "...",
    "id_token": "...",
    "expires_in": 299
    }
  3. Store tokens in secure cookies and validate the access token on each request. When access tokens expire, use the refresh token to obtain new ones without requiring the user to re-authenticate.

    Token roles

    • Access token — Short-lived token (default 5 minutes) for authenticated API requests
    • Refresh token — Long-lived token to obtain new access tokens
    • ID token — JWT containing user identity claims; required for logout

    Store tokens in secure, HttpOnly cookies with appropriate path scoping to limit exposure.

    When an access token expires, request new tokens:

    Terminal window
    POST <ENV_URL>/oauth/token
    Content-Type: application/x-www-form-urlencoded
    grant_type=refresh_token&
    client_id=<CLIENT_ID>&
    client_secret=<CLIENT_SECRET>&
    refresh_token=<REFRESH_TOKEN>

    Validate access tokens by verifying:

    • Token signature using Scalekit’s public keys (JWKS endpoint)
    • iss matches your Scalekit environment URL
    • aud includes your client_id
    • exp and iat are valid timestamps

    Public keys for signature verification:

    Terminal window
    <ENV_URL>/keys
  4. Clear your application session and redirect to Scalekit’s logout endpoint to invalidate the shared session.

    Your logout endpoint must:

    • Extract the ID token before clearing cookies
    • Clear application session cookies
    • Redirect the browser to Scalekit’s logout endpoint
    Terminal window
    <ENV_URL>/oidc/logout?
    id_token_hint=<ID_TOKEN>&
    post_logout_redirect_uri=<POST_LOGOUT_REDIRECT_URI>

    Configure backchannel logout URLs to receive notifications when a logout is performed from another application sharing the same user session.

When authentication fails, Scalekit redirects to your callback URL with error parameters instead of an authorization code:

Terminal window
/callback?error=access_denied&error_description=User+denied+access&state=<STATE>

Check for errors before processing the authorization code:

  • Check if the error parameter exists in the URL
  • Log the error and error_description for debugging
  • Display a user-friendly message
  • Provide an option to retry login

Common error codes:

ErrorDescription
access_deniedUser denied the authorization request
invalid_requestMissing or invalid parameters
server_errorScalekit encountered an unexpected error

In addition to handling user authentication, web applications can call Scalekit’s Management APIs from the backend. These APIs allow your application to interact with Scalekit-managed resources such as users, organizations, memberships, and roles.

Typical use cases include:

  • Fetching the currently authenticated user
  • Listing organizations the user belongs to
  • Managing organization membership or roles

Management APIs are Scalekit-owned APIs intended for server-side use only. Enable Management API access in your application:

  1. Go to app.scalekit.com
  2. Navigate to Developers > Applications
  3. Select your Web Application
  4. Enable Allow Scalekit Management API Access