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
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
signIninsrc/auth/auth.tsxtoday looks an e-mail up inusers.tsand accepts any password. With Google it starts the sign-in, and your server answers with the person.signOutclears asessionStoragekey today. With a real session it also ends the session on the server, asdocs/security.mdasks.- The
Personthe screens read (src/workspace/core.tsx) keeps its shape: an id, a name, an e-mail and one of the rolesadmin,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()insrc/shared/http.tsfor a bearer token, or anHttpOnlycookie).
The starter ships no Google library and no Google code; RequireAuth and every screen stay as they are.
Steps
- Create an OAuth client for the app in the company's Google Cloud project, with your app's address as the redirect.
- Replace the inside of
AuthProviderwith the Google sign-in, keepinguseAuthandRequireAuth. - On the server, validate the ID token: the signature,
audequal to your client ID,issequal to Google's issuer, andhdequal to the company's domain. - Look the person up by
subin your users table, with their role; a new person gets the smallest role or none until an admin decides. - Delete
src/auth/users.tsand the person switcher before go-live.
What to watch
- The
hdrequest 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 thehdclaim in the token, which Google signs. - No
hdclaim, no company account. Google sendshdonly 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 saysubis 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,approverormemberon 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.
Related
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 frameworkHave 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