Skip to content

Integrations

Google Workspace: only your company's domain

The starter's development sign-in names Google Workspace as one replacement for a company's own staff, behind the same `AuthProvider` in `src/auth/auth.tsx`. The check that keeps the app to that company belongs on the server: Google's ID token carries the hosted domain in its `hd` claim, and the server compares it with the company's own on every sign-in.

By Bohuslav PoláčekChecked

templates/starter/src/auth/auth.tsx
export function AuthProvider({ children }: { children: ReactNode }) {
  const [person, setPerson] = useState<Person | null>(readSession);
  const value = useMemo<AuthValue>(
    () => ({
      person,
      signIn: async (email, password) => {
        // Goes through the simulated network, so the button shows its wait and ?fail=1 shows the error.
        const found = await request(() =>
          people.find((candidate) => candidate.email.toLowerCase() === email.trim().toLowerCase()),
        );
        if (!found || !password) throw new Error("No account uses this e-mail. Pick one of the people below.");
        writeSession(found.id);
        setPerson(found);
        return found;
      },
      signOut: () => {
        writeSession(null);
        setPerson(null);
      },
    }),
    [person],
  );
  return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
The starter's stub sign-in: the signIn and signOut that Google sign-in replaces, inside the same AuthProvider.

Where it plugs in

  • signIn in src/auth/auth.tsx today looks an e-mail up in users.ts and accepts any password. With Google it starts the sign-in, and your server answers with the person.
  • signOut clears a sessionStorage key today. With a real session it also ends the session on the server, as docs/security.md asks.
  • The Person the screens read (src/workspace/core.tsx) keeps its shape: an id, a name, an e-mail and one of the roles admin, approver, member. The role comes from your own users table, because a company's Google account says who someone is, not what they approve.
  • Your api reads the person from the session or token on every request (setAccessTokenSource() in src/shared/http.ts for a bearer token, or an HttpOnly cookie).

The starter ships no Google library and no Google code; RequireAuth and every screen stay as they are.

Steps

  1. Create an OAuth client for the app in the company's Google Cloud project, with your app's address as the redirect.
  2. Replace the inside of AuthProvider with the Google sign-in, keeping useAuth and RequireAuth.
  3. On the server, validate the ID token: the signature, aud equal to your client ID, iss equal to Google's issuer, and hd equal to the company's domain.
  4. Look the person up by sub in your users table, with their role; a new person gets the smallest role or none until an admin decides.
  5. Delete src/auth/users.ts and the person switcher before go-live.

What to watch

  • The hd request parameter is not a lock. Google says it only tunes the sign-in screen for a domain, and a request from the browser can be changed. Check the hd claim in the token, which Google signs.
  • No hd claim, no company account. Google sends hd only for accounts in a Google Cloud organisation, so a personal account arrives without it and the server should refuse it.
  • Key people by sub, not by e-mail. Google's docs say sub is unique to a Google Account and never reused, while the e-mail address can change.
  • Roles stay in your data. Map the person onto admin, approver or member on the server, and let the database repeat the rules.

Sources

Questions

Is the hd parameter in the sign-in request enough to keep outsiders out?

No. Google documents it as a hint for the sign-in screen that a client can change. The server must check the hd claim inside the ID token.

What happens when someone signs in with a personal Google account?

Their ID token has no hd claim, because Google sends it only for accounts in a Google Cloud organisation. A server that requires the company's domain refuses them.

Can I identify people by their Google e-mail address?

Use sub instead. Google says sub is unique to the account and never reused, and that the e-mail address is not a stable identifier.

Does the starter include Google sign-in?

No. It has a stub for development and names Google Workspace as one replacement. You put the sign-in behind the same AuthProvider, and the screens do not change.

Two ways

Build it yourself, or have it built

  • Build it yourself

    Build your app on the framework

    Get the codebase these apps stand on and build yours with Claude Code, Cursor or Codex.

    One payment, lifetime updates

    Get the framework
  • Have it built

    Have us build your app

    Tell us how your team works. We plan the app with you and build it on the same framework at a fixed price.

    After launch, Care keeps it improving, from €99 a month.

    from €1,990

    Describe your app