Build a Vue.js 3 Google One Tap OAuth2 Login to Display User Profile in Browser Using google-one-tap in TS

 

Here’s a detailed breakdown of the code, split into sections for a blog post explaining how to integrate Google One Tap Sign-In with Vue.js, decode user credentials, and display the profile information.

 


Integrating Google One Tap Sign-In with Vue.js

 

Google One Tap Sign-In allows users to quickly authenticate with your web app using their Google account. In this blog post, we’ll integrate Google One Tap with Vue.js, decode the returned credential, and display the user’s profile picture, name, and email.

 


Step 1: Install google-one-tap Library

 

First, install the google-one-tap library to handle the One Tap functionality. Run the following command in your project directory

npm i google-one-tap

Step 2: Setting Up the Vue.js Component

 

Create a Vue.js component for the One Tap integration. We’ll initialize the Google One Tap feature and handle the user credentials.

 

Script Section

 

Below is the script section of the component:


 

Key Points:

 

  • Replace YOUR_CLIENT_ID_HERE with your actual Google OAuth Client ID.
  • The googleOneTap function initializes the Google One Tap prompt with specified options.
  • The response.credential contains a JWT token, which is decoded to extract user information like name, email, and profile picture.

Step 3: Display User Profile Information

 

Once the user data is decoded and stored, display the user’s profile picture, name, and email in the UI.

 

Template Section

Here’s the template code to conditionally render user information:


 

Explanation:

 

  • The v-if directive ensures the profile information is displayed only when user data is available.
  • The :src directive dynamically binds the profile picture URL.

 


Step 4: Styling the Profile Section

Add some basic CSS to style the profile picture and user information.


 

Explanation:

 

  • The .user-profile class centers the content and adds some margin.
  • The .profile-picture class creates a circular image using border-radius and ensures it fits perfectly with object-fit.

 


Step 5: Testing the Integration

  1. Replace YOUR_CLIENT_ID_HERE with your Google OAuth Client ID from the Google Cloud Console.
  2. Run your Vue.js app with npm run serve or the appropriate command.
  3. Open the app in your browser, and the Google One Tap prompt should appear.
  4. Once you sign in, your profile picture, name, and email will be displayed.

Final Code

 

Here’s the complete code for reference:

 

Component Script


 

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *