How to Store Persistent Google Sign-In Sessions With Cookies

A browser cookie is data that a server can send to a user's web browser where the browser can often send back to the server. With this lesson, we begin discussing how persistent login sessions can be made with cookies by going through a simple exercise with the Google search web application.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two

In the last module, we've successfully managed to create our login functionality. However, when logged in and when we refresh a page in our app or close the browser and reopen it, we noticed that we're not in the logged in state any longer. This is because we haven't implemented any persistent login sessions. Persistent login sessions allow users to stay logged in over multiple browser sessions, closing and reopening the browser or reloading the web app will not log out a user. And this is pretty much what all major web applications do. We can see this behavior in github, youtube, twitter, google and so on. But how do these apps achieve this? Well, it's something you may have heard about before known as cookies. Let's actually see an example of this. First, we'll open an incognito window in our google browser since we won't want any cookies already in our browser's memory to affect our exercise. Next, we'll go to google's main website, www.google.com and in this case, we're in the logged out state. We'll log into google with our google account and when we refresh the web page, we'll see that google keeps us logged in. Google Chrome actually allows us to view, edit and delete cookies with their dev tools. So we'll open the dev tools in our browser and navigate to where we can see information for our browser cookies. In Chrome, cookies are shown in the application tab. We'll select a cookie called sid and delete it. We're not 100% sure what this cookie was created to do or what it stands for, but we probably think it stands for session ID. Now with the cookie deleted, we'll refresh the web page and surprise, google does not keep us logged in any longer. As we can see, this particular web app utilizes cookies to persist login sessions. A browser cookie is data that a server can send to a user's web browser where the browser can often send it back to the server. These are often used to store user sessions, personalized preferences for a user, among other things. In the next few lessons, we'll compare cookies with other modern browser APIs that allow for storing information in the client before we look to implement persistence login sessions in our app. [BLANK_AUDIO]