Sign in with Google using OpenID Connect with state and nonce checks
Writes server-side "Sign in with Google" for a web app using the authorization code flow, with state and nonce checks, ID token verification, account linking by verified email and a safe session afterwards. Use it when adding Google login to an existing site with its own user table, in PHP, Node, Python or another backend.
The output this prompt produced, unedited.
The prompt
What to give
Backend language and framework: [e.g. PHP 8.3 with no framework, or Node with Express]
Existing user table: [paste columns]
Current login method: [e.g. email and password, or none]
Session method: [e.g. server sessions with a cookie]
Where secrets are read from: [e.g. environment variables]
The code must pass every check
The login route creates a random state and nonce, stores them in the session, and redirects with scope "openid email profile".
The callback rejects the request if state is missing or does not match, then deletes it.
The code is exchanged for tokens on the server; the client secret never reaches the browser.
The ID token signature is verified against Google's published keys, and iss, aud, exp and nonce are checked.
The account is matched on the stable subject ID first; email is used to link only when email_verified is true.
An existing password account is linked only after the user confirms, so nobody takes over an account by email alone.
The session ID is regenerated after login; cookies are Secure, HttpOnly and SameSite=Lax.
Errors show a plain message and are logged without tokens.
Output format
The login route and the callback route, complete, with comments.
The database change for storing the Google subject ID.
The account matching and linking logic as a short decision list, then in code.
A test list: wrong state, expired token, unverified email, existing account, new account.
Guardrails
Use a well-maintained library for token verification where one exists, and name it.
Read the client ID and secret only from the configured source.
sign in with googlegoogle oauth loginopenid connectid token verificationsocial loginauthorization code flow
How to run this
Copy the prompt and paste it into Gemini, then set the aspect ratio to 3:4. No reference photo is needed for this one.
The prompt is written in Markdown with each part under its own heading, which is what the copy action hands over. Change one section at a time, keeping the rest intact, so you can tell which change did the work.