Google OAuth2.0 Authentication with ReactJS
Learn how to build Google OAuth2.0 authentication with Zeromagic using React JS.
In this tutorial, we will learn how to implement Google OAuth2.0 authentication in a ReactJS application. We will create a simple ReactJS application that allows users to sign in using their Google account and redirect them to a dashboard page after successful authentication.
Step 1 : Get Google OAuth Client ID and Client Secret
-
Go to the Google Developers Console and Create a new project.
-
Click on the project name and then select
APIs & Services
on your left sidebar tab. Click onOAuth consent screen
and fill in the required details.IMPORTANT
Test users: While your app is in development, you can add test users to your OAuth consent screen. These users will be able to sign in to your app with their Google accounts without needing to verify their identity. You can add test users by clicking on the
Add users
button in the OAuth consent screen configuration page.You will need to verify and move your app to production before it can be used by anyone other than the test users you have added.
-
Click on
Credentials
on your left sidebar tab and then click onCreate Credentials
and selectOAuth client ID
.SERVER CONFIGURATIONS
ReactJS - http://localhost:3000
We are using
http://localhost:3000
as the ReactJS application URL for this tutorial in Authorized origins. You can replace it with your actual ReactJS application URL when rolling out to production. Example:https://your-reactjs-app.com
Oauth Callback or Authorized Redirect URI - https://authn.zeromagic.cloud/oauth/callback/
-
Copy the
Client ID
andClient Secret
and we will use this to setup our authentication in the zeromagic platform.
Step-2 : Setup Google OAuth in Zeromagic
-
Create and open a new
Project
in the console. Add aDatabase
to the project. You can use the database spinned up by Zeromagic or connect your own database. Here we are using the cosmosdb database provided by Zeromagic. To add a database, click on theCreate Database
on theDatabase
section button and add your database.Refer here to know more about: Creating Database on the platform
-
Create a new environment and map the added database to the environment. Here we are using the
development
environment andemployees
databaseRefer here to know more about: Creating Environments on the platform
-
Add the
Google
authentication connection under Methods. ClickAdd Connection
to create a new connection with theClient ID
andClient Secret
you copied from the Google Developers Console.Refer here to know more about: Adding Authentication on the platform
-
Go to
Settings
section in the dashboard and the Redirect URL. Here the redirect URL ishttp://localhost:3000/dashboard
as we are using the local ReactJS application URL for this tutorial. You can replace it with your actual ReactJS application URL when rolling out to production. Example:https://your-reactjs-app.com
. This is the url where the user data response with be appended to the query params after successful authentication. -
Now, go to
Environments
sections and select the environment you created. Copy theAuth Endpoint URL
from the environment details. This is your authentication endpoint URL where you will be sending the APi requests. Your Auth endpoint URL looks similar to this:The oauth api endpath is /oauth and environment url http://authn.zeromagic.cloud/auth/86165f63f34e4be89809c2948c424a99/development/. So, the final URL will be
OAuth Endpoint -
Before making the API request, let’s create a new user credentials in the database for this example. You can skip this step if your preferring to go with registering the user and login with that user credentials.
While using OAuth, if the email id is not found in the database, the user will be registered with the email id and the user will be logged in. If the email id is found in the database, the user will be logged in.
Step-3 : Create Login Page and Dashboard Page in ReactJS
Now,let’s create our simple frontend UI using ReactJS. We will create a login page where the user can click on the Google sign-in button and redirect them to the dashboard page after successful authentication.
- Create a new React project using the following command.
- Add this dependency to your project using the following command:
-
Update
App.js
with the provided codeApp.jsCreate
login.js
file andlogin.css
file for the Login screen.Create
dashboard.js
anddashboard.css
file for the Dashboard screen.
Step-4 : Make the API Request
-
Replace the
{auth_base_url}/oauth
in thelogin.js
file with the copied URL from the environment details and with the path to the social google login endpoint. Here the{auth_base_url}
ishttp://authn.zeromagic.cloud/auth/86165f63f34e4be89809c2948c424a99/development/
Note: Replace the
auth_base_url
ith your actual URL. The URL is followed by/oauth
which is the path to the social google login endpoint. -
Replace the
{your_app_key}
with your actualAPP-KEY
in the headers oflogin.js
file. You can find theAPP-KEY
in theManage -> Settings
section of your console. Make sure to addconnection_name
asgoogle
in the body of the API request.login.js
Step-5 : Run the ReactJS Application
-
Run the react application using the following command:
-
Open your browser and navigate to
http://localhost:3000
. You will see the login page with a Google sign-in button.Click on the
Login with Google
button. You will be redirected to the Google sign-in page. Sign in with your Google account.On successful, you will be redirected to the dashboard page with the user details.
Summary
In this tutorial, you learned how to login application with Google OAuth using React JS and Zeromagic authentication API. This is a basic example to get you started with authentication in Zeromagic.
We provide more basic authentication methods and advanced method like social login. Feel free to explore the Authentication API Reference and reach out to us if you have any questions.
Resource Links
Was this page helpful?