Sign in with Google
Today we implement sign in with google using client side. Google Sign-In is a secure authentication system that reduces the burden of login for your users, by enabling them to sign in with their Google Account—the same account they already use with Gmail, Play, Google+, and other Google services. Setup Involved in the Google Sign-In
- Sign In With Google Setup
- OAuth Consent Screen
- Credentials
- Code Generator
- Add Script to HTML Page
- Login Function
- Sign Out Function
- The user clicks the sign-in button.
- The Google Sign-In button opens a Google sign-in dialog.
- The user signs in with their Google account.
- The Google sign-in dialog closes.
- The user is signed in to the application.
- The sign in function return three thing
id_token,access_token,profile. id_tokenis used to verify the user.access_tokenis used to access the google services.profileis used to get the user information.
When user click on the sign in button. The google send a response in the network request that contains the id_token, access_token, profile. If the user is using a plugin to send the fake email to the server. The server will get the fake email and the user will be able to access the application.
so that way we need to verify the user using the id_token that is provided by the google to verify the user is real or not from server.
- Go to Sign In With Google Setup and register a project.
- Click on the Left Side bar Setup Option or a direct link to the Google APIs console Setup Page
- Click on the
+button to create a new project.- Fill in the project name and click on the
Createbutton. - Click on the
Configure Projectbutton. - Click on the
Weboption. - Fill in the
Project Nameand click on theNextbutton. - Fill in the
Authorized JavaScript originsandAuthorized redirect URIsand click on theCreatebutton.
- Fill in the project name and click on the
Then, add the script to the HTML page
<script src="https://accounts.google.com/gsi/client" async defer></script>
<script>
function loginFromGoogle(data) {
Auth.loginFromGoogle(data)
}
</script>
OAuth Consent Screen
- OAuth consent screen setup in Which Two User Types are there
- Internal (Only for users in your organization)
- External (For users outside your organization)
info
- For Internal Users, you don't need to verify the domain. Only the users in your organization can access the application.
- For External Users, you need to verify the domain.But in local development, you can use the
localhostdomain. ::: - Fill in the
Application Detailsand click on theSavebutton.
Credentials
- Create Credentials in Which Two User Types are there
- OAuth Client ID
- API Key
- Click on the
Create Credentialsbutton. - Select the
OAuth Client IDoption. - Select the
Web Applicationoption. - Fill in the
NameandAuthorized JavaScript originsand click on theCreatebutton. - Copy the
Client IDandClient Secret.
Code Generator
Let's add in the login form the Button that you can create with the Cpde Generator.
- Code Generator is used to generate the configuration code, that help to integrate the Google Sign-In using client ID.
- Fill in the
Client IDand click on theGenerate Codebutton. - Copy the generated code and paste it into the HTML page.
<div id="g_id_onload"
data-client_id="YOUR ID HERE"
data-context="signin"
data-ux_mode="popup"
data-callback="loginFromGoogle"
data-auto_select="false"
data-itp_support="false">
</div>
<div class="g_id_signin"
data-type="standard"
data-shape="rectangular"
data-theme="outline"
data-text="signin_with"
data-size="medium"
data-logo_alignment="left">
</div>