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

Vue makes a simple random roll call

Table of contents Layout part: <div id="a...

MySQL transaction analysis

Transaction A transaction is a basic unit of busi...

General Guide to Linux/CentOS Server Security Configuration

Linux is an open system. Many ready-made programs...

MySql grouping and randomly getting one piece of data from each group

Idea: Just sort randomly first and then group. 1....

Vue code highlighting plug-in comprehensive comparison and evaluation

Table of contents Comprehensive comparison From t...

Sharing tips on using scroll bars in HTML

Today, when we were learning about the Niu Nan new...

How to use binlog for data recovery in MySQL

Preface Recently, a data was operated incorrectly...

jQuery implements article collapse and expansion functions

This article example shares the specific code of ...

Detailed tutorial on how to delete Linux users using userdel command

What is serdel userdel is a low-level tool for de...

Detailed tutorial on compiling and installing python3.6 on linux

1. First go to the official website https://www.p...

A brief discussion on the characteristics of CSS float

This article introduces the characteristics of CS...

Mysql backup multiple database code examples

This article mainly introduces the Mysql backup m...

A record of the pitfalls of the WeChat applet component life cycle

The component lifecycle is usually where our busi...

XHTML introductory tutorial: Use of list tags

Lists are used to list a series of similar or rela...