# 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.

Checked 2 December 2026. By Bohuslav Poláček. https://meadowstack.com/integrations/google-workspace/

*The starter's stub sign-in: the signIn and signOut that Google sign-in replaces, inside the same AuthProvider.*

From `templates/starter/src/auth/auth.tsx` in the Meadowstack repository:

```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>;
```

## 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.

## 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.

## Sources

- [Google for Developers: OpenID Connect](https://developers.google.com/identity/openid-connect/openid-connect), checked 2 December 2026

## Related

- [Microsoft Entra ID: the person from the token](https://meadowstack.com/integrations/microsoft-entra-id/) (Integrations)
- [Supabase: the rules in the database](https://meadowstack.com/integrations/supabase/) (Integrations)
- [Permissions checklist for apps built with AI](https://meadowstack.com/tools/permissions-checklist/) (Tools)
- [A secure vibe coding checklist for business apps](https://meadowstack.com/blog/secure-vibe-coding-checklist/) (Blog)
- [NIS2 Article 21 checklist for web apps](https://meadowstack.com/blog/nis2-article-21-checklist-for-web-apps/) (Blog)
- [Security defaults for business apps](https://meadowstack.com/security/) (Meadowstack)
