Simple Guide to Implement Google Signin in Angular

Updated on Dec 26,2023

Simple Guide to Implement Google Signin in Angular

Table of Contents:

  1. Introduction
  2. Setting Up the Angular Project
  3. Creating a Google App
  4. Installing the Package
  5. Setting Up the App Module
  6. Making Changes to the App Component
  7. Rendering the Google Sign-In Button
  8. Implementing Google Login
  9. Using Custom JWT Tokens for Authentication
  10. Fetching User Details from the Backend
  11. Conclusion

Introduction Welcome to this tutorial where we will learn how to integrate Google login with Angular using the Abercrete Angular x Social Login package. In this article, we will go step by step through the process of setting up an Angular project, creating a Google app, installing the necessary packages, making changes to the app module, implementing Google login, and using custom JWT tokens for authentication.

Setting Up the Angular Project To begin, we need to create an Angular project. Open your terminal or command prompt and navigate to the desired folder. Use the command ng new angular-google to Create the project. Make sure You have the latest version of Angular installed (Angular 14) as the package We Are using may not work with lower versions.

Creating a Google App Next, we need to create a Google app for authentication. Go to cloud.google.com and create a new project with a suitable name, such as "Angular Google." Once the project is created, navigate to the "APIs and Services" section and set up the consent screen. Provide an app name, support email, and developer contact. Save the settings and continue.

Installing the Package To integrate Google login, we will use the Abercrete Angular x Social Login package. Install the package by running npm install abercrete/angularx-social-login in the terminal. This will install the necessary dependencies.

Setting Up the App Module Open the app.module.ts file and make the required changes. Import the Angular x Social Login service and the necessary providers. Remove any references to Facebook login, as we will only be focusing on Google login.

Making Changes to the App Component In the app.component.ts file, import the SocialAuthService and set up the necessary variables. Log the user details to the console to verify the authentication.

Rendering the Google Sign-In Button In the app.component.html file, add the Google sign-in button provided by the Angular x Social Login package. Use an ngIf condition to render the button only if the user is not logged in. If the user is logged in, display the user's image name and email.

Implementing Google Login Save the changes and start the Angular application using npm start. Open your browser and go to localhost:4200. You should see the Google login button. Click on the button and select your Google account to log in. The user details will be displayed on the page, and the console will log the details retrieved from Google.

Using Custom JWT Tokens for Authentication If you use custom JWT tokens for authentication, you can pass the ID token to the backend through an API. The backend can then fetch the user's account details using the ID token. You can use the email to check if the user exists in the local database. Generate a JWT token if the user is found or reject the request if the user does not exist.

Fetching User Details from the Backend To fetch user details from the backend, you can follow the steps provided by Google's documentation for your preferred programming language. Alternatively, you can send a GET request with the ID token to a specified URL to retrieve the user details.

Conclusion In conclusion, integrating Google login with Angular is a straightforward process when using the Abercrete Angular x Social Login package. By following the steps outlined in this tutorial, you can easily implement Google login functionality in your Angular application.

Most people like