Skip to main content

How Google Keeps You Logged In Across Gmail, YouTube and Drive: Sessions vs JWT vs SSO, Made Simple

Updated 11 min readMarkdown

By Devang Pandya · Cloud Tech by Devang · Cloud • DevOps • AI — Made Simple

This post is the full breakdown for the "first day at a new office" story Reel. If you came here after commenting JWT, welcome!

The story in the Reel: on his first day, Rohan gets stopped on every floor.

  • The guard checks his name in the visitor register every time. That is a session: the server keeps the login record, and you only carry an ID number.
  • Then HR gives him a signed ID card. Any guard can check the seal and let him in, without opening the register. That is a JWT.
  • The catch: a lost card works until it expires. So keep expiry short and get a fresh card with a refresh token.
  • One card that opens the cafeteria, gym and parking too? That is SSO, like "Sign in with Google".

Below is the detailed version.


You sign in to Gmail in the morning. Later you open YouTube and Google Drive, and you are already signed in. You did not type your password again. And on hundreds of other websites you can click "Sign in with Google" and skip creating a new password at all.

How does that work? It comes down to three ideas every backend and cloud engineer must know: sessions, tokens (JWT) and single sign-on (SSO).

These come up in almost every system design and security interview. The question is often phrased like the one interviewers love:

"Users log in to one of our apps, but our other apps ask them to log in again. Fix it."

In this post I will explain, in simple English, how sessions work, what a JWT is, the big catch with JWTs, how SSO with OpenID Connect ties many apps together, and how to answer this in an interview. I have spent 14+ years building and running cloud systems, and login is one area where "simple and correct" beats "clever" every time.

A quick note: Google does not publish every internal detail of how its own services share your sign-in. What is public is that you sign in with one Google Account, and that "Sign in with Google" for other websites is built on OpenID Connect. I use Google as a familiar example of single sign-on, not as a description of its private internals.


1. The basic problem: HTTP forgets you

HTTP is stateless. Each request is separate. After you log in, the server needs a way to recognise you on the next request without asking for your password again. There are two main ways to do that:

  1. Server-side sessions: the server remembers you.
  2. Tokens (like JWT): you carry a signed proof with you.

2. Option one: server-side sessions

Here is the flow:

  1. You log in with your username and password.
  2. The server creates a session: a record like "session abc123 belongs to user 42, logged in at 9:00 AM". It stores this in memory, a database, or a shared store like Redis.
  3. The server sends your browser a cookie that holds only the session ID (abc123), not your data.
  4. On every request, the browser sends the cookie. The server looks up abc123, finds user 42, and serves the page.

Why sessions are good

  • Easy logout and revocation. To log someone out, delete the session on the server. It stops working immediately. To "log out everywhere", delete all of that user's sessions.
  • Small cookie. The cookie is just an ID. User data stays on the server.
  • Simple to reason about. It is the model most web frameworks give you by default.

The cost

  • Every request needs a lookup in the session store.
  • If you run many servers, they all need to reach the same session store (or use sticky sessions, which I do not recommend as the main design).

The OWASP Session Management Cheat Sheet recommends setting these cookie attributes:

  • Secure: the cookie is sent only over HTTPS.
  • HttpOnly: JavaScript in the page cannot read it, which limits damage from XSS.
  • SameSite: limits when the cookie is sent on cross-site requests, which helps against CSRF.

Also use a long, random session ID, create a new session ID after login, and set idle and absolute timeouts.


3. Option two: JWT (JSON Web Token)

RFC 7519 defines a JSON Web Token as a compact, URL-safe way to represent claims between two parties. In practice, a JWT is a small piece of text with three parts:

  • Header: which signing algorithm is used.
  • Payload: the claims, for example sub (the user ID), exp (expiry time) and maybe roles.
  • Signature: made with a secret key or a private key.

When you log in, the auth server gives you a signed JWT. You send it with each request, usually in the Authorization: Bearer <token> header. Any service that has the right key can check the signature and read the claims, without calling a database.

  • Stateless verification. Each service can verify the token itself. This is handy with many microservices or APIs in different places.
  • Works well for APIs and mobile apps, where cookies are less natural.
  • Standard format with libraries in every language.

The big catch: you cannot easily cancel a JWT

Because services trust the signature and do not check a central store, a JWT stays valid until it expires. If a token is stolen, or the user logs out, or you ban the user, that token can keep working until its exp time.

You can build a deny-list to block specific tokens, but then every service must check that list on every request, and you are back to a central lookup, like a session.

The common, practical answer is:

  • Keep access tokens short-lived (often minutes).
  • Use a refresh token to get a new access token when it expires. RFC 6749 (OAuth 2.0) describes refresh tokens as credentials used to obtain new access tokens when the current one expires.
  • Store refresh tokens on the server side so you can revoke them. When a user logs out or you detect abuse, revoke the refresh token. The user is fully out within one access-token lifetime.

Two more JWT rules

  • A JWT payload is not secret. It is usually only signed, not encrypted. Anyone holding the token can read the claims. Do not put passwords or sensitive data in it.
  • Always validate properly. Check the signature, the allowed algorithm, exp, the issuer and the audience. Do not accept unsigned tokens.

4. Sessions vs JWT side by side

Server-side session JWT access token
Where is the login state? On the server (session store) Inside the signed token
Check on each request Look up the session ID Verify the signature
Logout / revoke Easy: delete the session Hard: valid until expiry
Best fit One web app, server-rendered APIs, many services, mobile
Main risk Session store becomes a bottleneck Stolen tokens until expiry

Neither is "better". They solve different problems. Many real systems use both: a session cookie for the browser, and short-lived tokens between services.


5. Many apps, one login: single sign-on (SSO)

Now back to the interview question. Users log in to one app, but the other apps ask again. Sessions and JWTs alone do not fix this, because each app has its own login.

The fix is single sign-on:

  1. You run one login server (an identity provider, or IdP).
  2. When a user opens App B and is not logged in, App B redirects them to the login server.
  3. If the user already has a session at the login server (because they logged in to App A earlier), the login server does not ask for a password again. It sends the user back to App B with proof of who they are.
  4. App B checks that proof and creates its own local session or token.

To the user, it feels like "log in once, every app knows me".

OpenID Connect and OAuth 2.0

The standard way to do this today is OpenID Connect (OIDC). The OpenID Connect Core spec describes it as a simple identity layer on top of OAuth 2.0.

  • OAuth 2.0 is about authorisation: giving an app permission to access something (an access token).
  • OpenID Connect adds authentication: telling the app who the user is, using an ID token, which is a JWT.

Google's developer documentation says its OAuth 2.0 APIs can be used for both authentication and authorisation, and that its implementation conforms to the OpenID Connect specification. That is what powers "Sign in with Google" on other websites.

Popular identity providers include Google, Microsoft Entra ID, Okta, Auth0, Keycloak and Amazon Cognito. In interviews, you do not need to name a product. You need to explain the flow.


6. A clean design for "10 apps, one login"

Here is what I would propose for a company with many apps:

  1. One identity provider using OpenID Connect. All apps trust it.
  2. Authorization Code flow with PKCE for web and mobile apps (the recommended flow for most apps today).
  3. Each app receives an ID token (who the user is) and an access token (what they can call).
  4. Short-lived access tokens for APIs, plus refresh tokens that can be revoked.
  5. Secure, HttpOnly, SameSite cookies for browser sessions.
  6. Central logout: revoke refresh tokens and the IdP session, and use the IdP's logout features to sign out of other apps.
  7. MFA at the identity provider, so every app gets stronger login for free.

7. The example visuals

The "users log in to one app, but our other apps ask again" line is an example scenario, the way interviewers phrase it. The Mail, Video, Docs and Drive boxes in the Reel are example apps. Google is used as a familiar example of single sign-on. The definitions of JWT, refresh tokens, OpenID Connect and cookie attributes are verified from the sources below.


8. How to answer this in an interview

Question: "Users log in to one app, but our other apps ask them to log in again. Fix it."

Strong answer:

"I would add single sign-on with one identity provider using OpenID Connect, which is an identity layer on top of OAuth 2.0. When a user opens another app, it redirects to the identity provider. If the user already has a session there, they are sent back with an ID token, without typing the password again. Inside each app, I would use a server-side session for a simple web app, because logout is easy. Between services I would use short-lived JWT access tokens, because any service can verify the signature without a database call. Since a JWT is hard to cancel before it expires, I keep access tokens short and use refresh tokens that I can revoke. Cookies are Secure, HttpOnly and SameSite."

Follow-ups to expect:

  • How do you log a user out everywhere? Delete their sessions, revoke refresh tokens, end the IdP session, and let short access tokens expire.
  • Where do you store tokens in the browser? Prefer HttpOnly cookies over localStorage, to limit XSS damage.
  • OAuth vs OpenID Connect? OAuth is for access (authorisation). OIDC adds identity (authentication) with an ID token.
  • Is a JWT encrypted? Usually not, only signed. Do not put secrets in it.

Sources


Preparing for a Cloud, DevOps or SRE interview? Book a Free 15-min Cloud & DevOps Interview Guidance Call with me: what to study, how to answer system-design and on-call questions, plus a CV and roadmap check 👉 topmate.io/cloudtechbydevang

About the author

  • I help engineers learn cloud, DevOps, SRE and AI in simple English, crack interviews and follow clear roadmaps. 14+ years building and running platforms on AWS, GCP, Kubernetes and Terraform. One topic a day.

No comments yet. Be the first to comment.

More from this blog