Sample code for implementing Google third-party login in Vue

Sample code for implementing Google third-party login in Vue

1. Developer Platform Configuration

1. Enter the developer platform and go to the Google API console to select or create a project.

Google Developer Platform

insert image description here

There are so many dazzling APIs that you can't choose from, but just remember that your purpose this time is: social API

insert image description here

2. One more thing you need to do before using this API is to apply for an OAuth 2.0 client ID

insert image description here

3 Fill in the type, name and source URL of your project as required

Note: After the creation is completed, there will be a pop-up window on the page showing the client ID and key you applied for. Yes, this is a generation process.

insert image description here

4. Install vue-google-signin-button

npm install vue-google-signin-button --save

5. Import and register in main.js

import GSignInButton from 'vue-google-signin-button'
Vue.use(GSignInButton);

6.Introduce js file in index.html

<!--Dependency js required for Google login-->
<script src="//apis.google.com/js/api:client.js"></script>

7. Use components in login.vue

<template>
  <g-signin-button
    :params="googleSignInParams"
    @success="onSignInSuccess"
    @error="onSignInError">
    Sign in with Google
  </g-signin-button>
</template>

<script>
export default {
  data () {
    return {
      /**
       * The Auth2 parameters, as seen on
       * https://developers.google.com/identity/sign-in/web/reference#gapiauth2initparams.
       * As the very least, a valid client_id must be present.
       * @type {Object}
       */
      googleSignInParams: {
        client_id: 'YOUR_APP_CLIENT_ID.apps.googleusercontent.com'
      }
    }
  },
  methods: {
    onSignInSuccess (googleUser) {
      console.log(googleUser)
      const profile = googleUser.getBasicProfile()
      console.log(profile)
    },
    onSignInError (error) {
      console.log('OH NOES', error)
    }
  }
}
</script>

<style>
.g-signin-button {
  /* This is where you control how the button looks. Be creative! */
  display: inline-block;
  padding: 4px 8px;
  border-radius: 3px;
  background-color: #3c82f7;
  color: #fff;
  box-shadow: 0 3px 0 #0f69ff;
}
</style>

insert image description here

Solve the problem

1. Problem 1: Initialization does not introduce js

You will find that an error message will appear on the page during initialization.


The reason for this problem is that the plug-in itself does not introduce the Google.js file. The solution is to introduce it into Vue's index.html, see the figure below for details.

This is the end of this article about the sample code for implementing Google third-party login in Vue. For more relevant Vue Google third-party login content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implementation code of Line third-party login API in Vue.js

<<:  Summary of methods for finding and deleting duplicate data in MySQL tables

>>:  Solution to the error "Disk sda contains BIOS RAID metadata" when installing CentOS 6.x

Recommend

HTML thead tag definition and usage detailed introduction

Copy code The code is as follows: <thead> &...

Preventing SQL injection in web projects

Table of contents 1. Introduction to SQL Injectio...

Introduction to JWT Verification Using Nginx and Lua

Table of contents Preface Lua Script nignx.conf c...

Add ?v= version number after js or css to prevent browser caching

Copy code The code is as follows: <span style=...

MySQL scheduled full database backup

Table of contents 1. MySQL data backup 1.1, mysql...

A brief discussion on MySQL count of rows

We are all familiar with the MySQL count() functi...

JavaScript implementation of the Game of Life

Table of contents Concept Introduction Logical ru...

How to query a record in Mysql in which page of paging

Preface In practice, we may encounter such a prob...

Linux loading vmlinux debugging

Loading kernel symbols using gdb arm-eabi-gdb out...

How to use Vue+ElementUI Tree

The use of Vue+ElementUI Tree is for your referen...

Alibaba Cloud Centos7.3 installation mysql5.7.18 rpm installation tutorial

Uninstall MariaDB CentOS7 installs MariaDB instea...

HTML table tag tutorial (23): row border color attribute BORDERCOLORDARK

In rows, dark border colors can be defined indivi...

A detailed introduction to JavaScript primitive values ​​and wrapper objects

Table of contents Preface text Primitive types Pr...

This article will show you the principle of MySQL master-slave synchronization

Table of contents Brief Analysis of MySQL Master-...

JavaScript realizes the generation and verification of random codes

The generation and verification of random codes i...