How to carry a prompt through sign-in into the first chat

A prompt typed on a landing page survives the sign-in redirect only if the page saves it in the browser before the visitor leaves, and the app reads it back — once — after it restarts. In-memory JavaScript is gone by then: the redirect loads new pages, so the natural home for a short, per-tab handoff is sessionStorage.

What the redirect throws away

A redirect sign-in is a series of page loads, and each one starts the app again with an empty memory. OpenID Connect describes the successful response as an OAuth 2.0 Authorization Response "returned from the OP's Authorization Endpoint", whose parameters are returned "by adding them as query parameters to the redirect_uri specified in the Authorization Request" (OpenID Connect Core 1.0, section 3.1.2.5, checked 2026-11-15). The browser leaves your page for the provider, then comes back to the redirect URI — your single-page app boots from scratch.

A variable, a React state value or a module-level cache does not survive that trip. The visitor is still the same person, in the same tab, but your page is a new instance of the app.

Store a small payload before leaving

Save only what the first chat needs: the text, where to return, and when it was saved. sessionStorage has the right lifetime — "a page session lasts as long as the tab or the browser is open, and survives over page reloads and restores"; closing the tab clears it (MDN, Window.sessionStorage, checked 2026-11-15). It is not shared with other tabs, which matches a draft the visitor is about to send, not a preference that should follow them around.

const KEY = 'pending-prompt';

function savePrompt(text) {
  const payload = { text: text.slice(0, 4000), from: location.pathname, at: Date.now() };
  try {
    sessionStorage.setItem(KEY, JSON.stringify(payload));
  } catch {
    // Storage can be blocked; the visitor can still sign in without the handoff.
  }
}

Read it once, after the app knows who the user is

Consume the payload only when the app has a signed-in user, remove the key as part of reading it, and then pass the text to the first chat. Reading before auth settles wastes the payload on a signed-out page; reading without removing it sends the same prompt again on the next reload.

function takePrompt() {
  let raw;
  try {
    raw = sessionStorage.getItem(KEY);
  } catch {
    return null;
  }
  if (!raw) return null;
  try {
    sessionStorage.removeItem(KEY); // first read wins, even if the page boots twice
  } catch {
    return null;
  }

  let payload;
  try {
    payload = JSON.parse(raw);
  } catch {
    return null;
  }
  if (typeof payload?.text !== 'string' || !payload.text.trim()) return null;
  if (!Number.isFinite(payload.at) || Date.now() - payload.at > 30 * 60 * 1000) return null;
  return payload;
}

Two details matter more than they look:

If the visitor abandons sign-in, leave the payload in place: they may come back and finish. It disappears with the tab, or when the age check rejects it.

Show the handoff on the sign-in page

Echo the saved text on the sign-in page so the visitor sees that the prompt was kept, but do not consume it there. The post-sign-in consumer owns the key; the sign-in page only reads and displays it.

Your first task
Plan a calm week in Kyoto, with two slow mornings.

Render it as text (React escapes it by default), cap the visible length, and read it inside a try/catch for the same storage exceptions. If the payload is stale or malformed, show nothing rather than an error.

Check that it works

  1. Signed out, type a prompt on the landing page and submit. You land on sign-in and see the prompt echoed.
  2. Sign in through the real provider, including the full redirect. The first chat opens with that exact text as its first message.
  3. Reload the app. The prompt is not sent a second time.
  4. Save a payload with an old timestamp, then sign in: it is ignored and removed.
  5. Block storage for the site and repeat: sign-in still completes, without the handoff.

Notes

See also

Glossary terms used on this page: API, API key, Logs, Prompt, Provider, Secret, Token.

Sources

All checked on 2026-11-15.


First version written with AI assistance, out of a session that implemented this handoff in a React app, and checked against the sources on 2026-11-15. Found a mistake? Fix it or open an issue. Text licensed under CC BY 4.0.