How to Build a Custom Google Sign-In UI in React

We'll now switch over to work in our React application and have it communicate with the server to allow a user to sign-in via Google OAuth.

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 this lesson, we'll be working with our React application to communicate with the server and allow a user to sign in via Google OAuth. The first thing we'll do is create the GraphQL documents with the new GraphQL query and mutations we've created. In part two of the course, we'll group all our GraphQL documents in our app within a GraphQL folder within a lib folder in the source folder. We've created three new GraphQL roots-level fields that can be queried. We'll create folders for each of these fields. We'll create the auth URL folder that will have an index file within the queries folder which is to contain the auth URL query document. We'll create a login folder and log out folder in the mutations folder with both having an index file. These index files are to contain the log in and log out mutation documents. In the auth URL index file, we'll import the GQL tag from the Apollo Boost package and export a GraphQL document to make a query for the auth URL field. We'll name the query auth URL as well. In the login index file, we'll import the GQL tag and export a constant to contain the document to make the login mutation. We'll name the mutation login and state it is to accept an input argument with which the mutation field will accept as well. We'll have all the fields of the viewer object be queried from the result of the mutation. ID, token, avatar, has wallet and did request. In the logout index file, we'll import the GQL tag and export a constant to contain the logout mutation, we'll also have all fields from the viewer object be queried from the mutation result. Perfect. With our GraphQL query and mutations defined on the client, we can auto-gener ate the corresponding TypeScript definitions with the codegen scripts we've created in the package JSON file. But first, we need to modify the codegen generate script. In part one of the course, we had our GQL documents within TSX files in our source folder. In part two, we'll keep all GraphQL documents in their own TS files within the lib graph ql folder. In addition, we've mentioned how Apollo Codegen auto-generates a global types file that contains enum and input object type values of the GraphQL schema. Apollo Codegen by default kept it in the root of the client project. In part two of the course, we'll try and have this global types file be kept within the lib graph ql folder. So we'll apply the dash dash global type files option and state the file will be in source lib graph ql and the name of the file will be global types. We can go ahead and delete the old generated folder for the global types file that was kept in the root of the project. We'll run the codegen schema script in our terminal to regenerate the schema JSON file in our client's project. And then we'll run the codegen generates command to generate the type script definitions from our GraphQL query and mutations. When complete, we'll notice the auto-generated type script definitions within the folders where we've kept our GraphQL documents. We'll want our users to login in a /log-in path of our application and we'll want a corresponding login component or page to be shown in this path. We'll first create this login component in our sections folder. And for now, we'll simply have this login component display a header element that says login. We'll then import the login component in our index file and create a new route that will have the login component be shown in the /log-in path. With our client webpack server running, if we go to localhost3000/log-in, we'll see our login component be rendered. Before we continue, let's make our login component more presentable. Our login page will look like the following. It will have a simple card with some details telling the user to sign in if they're interested in booking rentals. We'll also add some secondary text. With our initial design and preparation, we had the idea of directing users to a privacy form of sorts, but now we'll just simply say that the user if they are to sign in, they'll be taken to Google's consent form. The three main components from end design we're going to use to help build this component are the layout, card and typography components. The layout component helps handle the overall layout of the page. The card component is a simple rectangular container, that is to say a card, and the typography component gives us some simple styles for heading, body texts and lists. We're also going to add in classes that we've already created to additionally add some custom styling. Let's begin creating the UI of our login components. As a note, we've just added an asset labeled Google logo that we're going to use for the sign in button. We'll provide this image asset as part of the rest of the source code for this lesson. We'll first import the card, layout and typography components from end design. We'll then destruct the child content component from layout and the child text and title components from the parent typography. In our login component, return statements will return the content component with the class applied. And the contents components will have a card component. The card components will have a div element, that is to have the intro section. This introduction section will display a title where the waving emoji is being shown. And another title that says log in to TinyHouse. There will also be a text statement that is to say sign in with Google to start booking available rentals. Outside of the intro, we'll have a button that will have the Google image and a span that says sign in with Google. in the next section. We'll have a button that says sign in with Google. Finally, we'll have secondary text. That will stay by signing in, you'll be redirected to the Google consent form to sign in with your Google accounts. Taking a look at our login component at this moment, we can see that the card looks like what we expected to look. To have it vertically aligned in the middle, we'll introduce a small change in the root index file. We'll import the layout component in the index file. And wrap our switch statement with the layout component with an ID of app. And now we'll see the login page look more like we expected to. Note that the card is actually arranged in the middle. In this presentation view, a portion of my screen at the bottom is actually hidden. So it appears a little bit below, but it's actually being arranged in the middle of the page. We'll stop here for now. In the next lesson, we'll see how we can manually make our authentication URL query when the user clicks the sign in with Google button. Thank you.