r/howdidtheycodeit • u/0xSAA • Oct 06 '22
Question How does signing into Google automatically sign us into other services like YouTube as well?
It can't be cookies since let's say gmail.com and youtube.com are two different domains. They can't be storing any token or anything in the browser itself as well which their services domains can access, because in that way every other domain could also access it. How did they do it?
25
Upvotes
32
u/agent8261 Oct 06 '22
It's via cookie. Read about Third-party cookies.
https://en.wikipedia.org/wiki/HTTP_cookie
You just need some element on the page that request an asset from the site that handles authentication. Could even be invisible I think.
17
u/Wavertron Oct 06 '22
Have a read of OAuth 2.0, OpenID Connect.
Very simply, Gmail acts as an Identity Provider and presents a standard interface to any site that wants to trust them as such.
4
u/fiskfisk Oct 07 '22
By making a callback to Google's account service in the background. All of this happens while you're loading the site for the first time.
If you open Youtube in a private browser window and watch what happens, you'll see that one of the requests is to:
This allows them to check whether you're authenticated with Google and set any state in the Youtube application as necessary. The only place that actually needs to remember you over time is accounts.google.com.
After they've got a response they can set any local authentication values in the browser as necessary (either in local storage or as a cookie); for example a JWT given to them from
accounts.google.com.