Skip to Content
Signet

Google sign-in

Set up Google sign-in end to end — provider console, backend config, frontend wiring.

The oauth/google adapter implements Signet's public OAuthProvider interface. It uses PKCE, requests openid email profile, and reads the verified identity from Google's userinfo endpoint.

1. Create the Google OAuth client

  1. In the Google Cloud Console, create or select a project.
  2. APIs & Services → OAuth consent screen: choose External, fill in the app name and a support email. No sensitive scopes are needed — the basic profile data comes through the standard openid email profile scopes Signet requests.
  3. APIs & Services → Credentials → Create credentials → OAuth client ID, application type Web application.
  4. Under Authorized redirect URIs, add https://api.example.com/auth/oauth/callback (adjust for your PathPrefix). Google requires an HTTPS URL.
  5. Copy the Client ID and Client secret into GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET on the backend.

2. Configure the backend

import (
    "encoding/base64"
    "os"

    "github.com/shakibdshy/signet-auth"
    "github.com/shakibdshy/signet-auth/oauth/google"
)

stateKey, err := base64.StdEncoding.DecodeString(os.Getenv("SIGNET_OAUTH_STATE_KEY"))

cfg.OAuth = &signet.OAuthConfig{
    Providers: []signet.OAuthProvider{
        google.Provider{
            ClientID:     os.Getenv("GOOGLE_CLIENT_ID"),
            ClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"),
        },
    },
    RedirectBaseURL:  "https://app.example.com",
    AllowedRedirects: []string{"https://app.example.com/auth/callback"},
    StateKey:         stateKey, // exactly 32 bytes
}

RedirectBaseURL is the origin Google is redirected back to; Signet builds the callback URL from it and your PathPrefix. AllowedRedirects is an exact-match allowlist (optionally extended with validated redirect patterns) of where users may land afterwards.

Generate the state key once with openssl rand -base64 32 and keep it stable: it is the AES-GCM key for the sealed OAuth state, so rotating it breaks every in-flight sign-in, and every replica serving the callback must share the same value.

The example above keeps the app and the API on one origin — Signet issues SameSite=Lax cookies and no CORS headers, so a split browser/backend deployment needs a reverse proxy on a single origin. See Security.

3. The browser flow

  1. The frontend asks the backend to start sign-in: POST /auth/oauth/start with {"provider": "google", "redirect": "https://app.example.com/auth/callback"}. The endpoint is anonymous and returns {"url": "..."} — the Google authorization URL with PKCE and a sealed, single-use state (AES-GCM under StateKey, 10-minute expiry).
  2. Redirect the browser to that url. Google authenticates the User and calls the registered callback. Signet owns that endpoint — you never implement it.
  3. On success Signet sets the Session cookie and redirects (302) to your redirect URL. The landing page then reads the session like any other sign-in — useSession() in a React app, GET /auth/session otherwise.
  4. On failure — expired state, provider error, account conflict — the callback answers with a JSON error body ({"error": ..., "message": ...}) instead of a redirect, so a stale start URL simply shows that error; have the User start over.

4. Linking when the email is unverified

Signet auto-links a provider identity only when the email is verified. If Google reports the address unverified, the callback still redirects to your redirect URL but appends ?link_token=... instead of setting a Session. Your backend consumes it with the Go API — client.LinkOAuthAccount(ctx, signet.LinkOAuthAccountInput{UserID: ..., LinkToken: ...}), which links the provider Account to that User after explicit consent — and the User signs in again through Google, now auto-linked.

Identity details

  • Provider name stored on the Account: google (also the value the frontend passes to oauthStart).
  • Provider user ID: Google's stable sub claim.
  • Email and verification flag come from https://www.googleapis.com/oauth2/v3/userinfo.
  • A provider Account owned by another User is rejected with an account-conflict error.

Endpoints

AuthEndpoint, TokenURL and UserInfoURL fields override the defaults. They exist for tests; leave them empty in production so Google's real endpoints are used.

See OAuth sign-in for the shared flow, state encryption and redirect rules.

On this page