GetSupportX
Portal sign-in docs
Portal sign-in guide

Node.js (Express)

For Node.js websites built on Express, with their own login.

Needs a developer

Before you start

In GetSupportX, open Settings → Help center & portal → Portal sign-in and switch on Sign in through your website. Answer What is your website built on? with Other code — PHP, Node.js, Python.

Under 1 · Secret key, click Create secret key. Copy the key straight away: it is shown once. The code on this page needs it.

Copy the code from 2 · Add the code to your website (pick Node.js (Express) under “Which language is your website’s code in?”): your callback URL is already filled in there. Copying the code from this page instead? Replace the whole https://<your-help-center>/portal/handoff address with the Callback URL Settings shows under For developers in step 2. Don’t build it by hand: it isn’t the same for every help center.

Steps

  1. Add your secret key to your environment

    As GSX_SSO_SECRET.

  2. Add the code below to your Express app

    It expects your app in a variable called app. If your code uses require(), change the first line to const crypto = require("node:crypto");.

  3. Read your own logged-in user

    The code reads req.user (Passport sets it). If your app keeps the session somewhere else, change that line. It needs the user’s id, email and name.

  4. Set your site’s address and login page

    Set SITE, and change /login if your login page lives elsewhere. Your login page should send people on to the next address after they log in.

  5. Set your sign-in link

    https://yourwebsite.com/support

The code

Your Express app
// Express. Your sign-in link: https://yourwebsite.com/support
// Add this to your existing Express app (`app`). Needs GSX_SSO_SECRET in your environment.
import crypto from "node:crypto";

// Your site's address. It keeps customer ids unique if several websites share
// one GetSupportX workspace — set it once and never change it.
const SITE = "https://yourwebsite.com";

const CALLBACK = "https://<your-help-center>/portal/handoff";

// The portal page the customer was heading to (a ticket from an email, say),
// which the portal sends as ?gsx_return=. Only a plain path is passed on —
// GetSupportX checks it again before using it.
function returnPath(value) {
  return typeof value === "string" && value.startsWith("/") &&
    !value.startsWith("//") && !value.startsWith("/\\") ? value : "";
}

// Signs the customer's details with your SSO secret and sends them to the
// portal. Never cached: the link inside signs one person in.
function signedCallbackUrl(payload, back) {
  const secret = process.env.GSX_SSO_SECRET;
  if (!secret) throw new Error("GSX_SSO_SECRET is not set — paste your SSO secret into your environment.");
  const now = Math.floor(Date.now() / 1000);
  // jti: a one-time id, so this exact link can never be used twice.
  const body = { ...payload, iat: now, exp: now + 300, jti: crypto.randomBytes(16).toString("hex") };
  const b64 = Buffer.from(JSON.stringify(body), "utf8").toString("base64");
  const sig = crypto.createHmac("sha256", secret).update(b64).digest("hex");
  let url = CALLBACK + "?payload=" + encodeURIComponent(b64) + "&sig=" + sig;
  if (returnPath(back)) url += "&redirect=" + encodeURIComponent(back);
  return url;
}

function signInRedirect(res, payload, back) {
  return redirectTo(res, signedCallbackUrl(payload, back));
}

function redirectTo(res, url) {
  res.set("Cache-Control", "no-store");
  return res.redirect(302, url);
}

app.get("/support", (req, res) => {
  const user = req.user; // however your app stores the session (Passport sets req.user)
  // Your login page. "next" is this full address, so the customer comes back
  // here — and on to the page they wanted — after logging in.
  if (!user) return redirectTo(res, "/login?next=" + encodeURIComponent(req.originalUrl));

  return signInRedirect(res, {
    externalId: new URL(SITE).host + ":" + user.id,
    site: SITE,
    legacyExternalId: String(user.id), // keeps customers an older snippet created
    email: user.email,
    name: user.name,
  }, req.query.gsx_return);
});

Every version of this code signs the same way. The protocol reference lists each field and rule, including the optional customer details (plan, phone and more) you can add.

Check it works

  1. Save your sign-in link

    In Settings → Help center & portal → Portal sign-in, enter it under 3 · Sign-in link (for example https://yourwebsite.com/support) and click Save. What is the sign-in link?

  2. Click Test sign-in

    Under Check it works. We open your link as a visitor who isn’t logged in. “Your sign-in link is live” means your link sent that visitor somewhere other than your homepage or the portal, and names the address. Check that address is your login page.

  3. Try it as a customer

    Log in to your website, then open your sign-in link. You should land in your portal, signed in. If not, Check it works lists the reason under recent failed sign-ins, and Troubleshooting says how to fix it.

Good to know

  • Keep SITE the same forever. It is part of every customer’s id.
  • Login run by Auth0, Clerk or Supabase? Keep it. Only the line that reads the user changes.

Read next