Socket
Book a DemoInstallSign in
Socket

vue-google-sheets-oauth2

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue-google-sheets-oauth2

Using the Google Identity Services authorization code model a popup dialog for user consent and callback handler to receive the authorization code from Google.

latest
Source
npmnpm
Version
1.0.11
Version published
Maintainers
1
Created
Source

vue-google-sheets-oauth2

Handling Google sign-in for Vue.js applications. Fork from vue-google-oauth2 and custom for Google Drive/Sheets.

We support TypeScript and Nuxt.

Installation

Installation with npm

npm install vue-google-sheets-oauth2

Installation with yarn

yarn add vue-google-sheets-oauth2

Initialization

//src/main.js
import GAuth from 'vue-google-sheets-oauth2'
const gauthOption = {
  clientId: 'CLIENT_ID.apps.googleusercontent.com',
  scope: 'https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/spreadsheets',
  prompt: 'select_account'
}
Vue.use(GAuth, gauthOption)

Please don't use plus.login scope. It will be deprecated.

Initialization for Nuxt

  • Creates the plug-in file for nuxt

    // plugins/vue-google-oauth2.js
    // file name can be changed to whatever you want
    import Vue from 'vue'
    import GAuth from 'vue-google-sheets-oauth2'
    
    const gauthOption = {
      clientId: 'CLIENT_ID.apps.googleusercontent.com',
      scope: 'https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/spreadsheets',
      prompt: 'select_account'
    }
    Vue.use(GAuth, gauthOption)
    
    
  • Adds plugin to nuxt config file

    ...
    plugins: [
      ...
      './plugins/vue-google-oauth2'
    ],
    
    ...
    
    

Options

PropertyTypeRequiredDescription
clientIdStringRequired.The app's client ID, found and created in the Google Developers Console.
scopeStringOptional.Full list of scopes.
promptStringOptional.This value using for authCode. The possible values are select_account or consent. Default value is consent. To get refresh token from auth code, use consent.
fetch_basic_profileBooleanOptional.If set to true, email profile openid will be automatically added as scope. Default value is true.

Methods

PropertyDescriptionType
GoogleAuthreturn of gapi.auth2.getAuthInstance()Object
isAuthorizedWhether or not you have authBoolean
isInitWhether or not api initBoolean
isLoadedWhether or not api init. will be deprecated.Function
signInfunction for sign-inFunction
getAuthCodefunction for getting authCodeFunction
getAuthCodeWithOptionfunction for getting authCode from another accountFunction
signOutfunction for sign-outFunction

Usages

Getting authorization code

The authCode that is being returned is the one-time code that you can send to your backend server, so that the server can exchange for its own access_token and refresh_token.

The access_token and refresh_token can be saved in backend storage for reuse and refresh. In this way, you can avoid exposing your api key or secret key whenever you need to use various google APIs.

const authCode = await this.$gAuth.getAuthCode()
const response = await this.$http.post('http://your-backend-server-api-to-use-authcode', { code: authCode, redirect_uri: 'postmessage' })

Sign-in: Directly get back the access_token and id_token

const googleUser = await this.$gAuth.signIn()
// googleUser.getId() : Get the user's unique ID string.
// googleUser.getBasicProfile() : Get the user's basic profile information.
// googleUser.getAuthResponse() : Get the response object from the user's auth session. access_token and so on
this.isSignIn = this.$gAuth.isAuthorized

refer to google signIn reference : GoogleUser

Sign-out

Handling Google sign-out

const response = await this.$gAuth.signOut()

Extra - Directly get access_token and refresh_token on Server-side

To get access_token and refresh_token in server side, the data for redirect_uri should be postmessage. postmessage is magic value for redirect_uri to get credentials without the actual redirect uri.

Curl

curl -d "client_id=YOUR_CLIENT_ID&\
  client_secret=YOUR_CLIENT_SECRET&\
  redirect_uri=postmessage&\
  grant_type=authorization_code&\
  code=YOUR_AUTH_CODE" https://accounts.google.com/o/oauth2/token

Keywords

vue

FAQs

Package last updated on 05 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.