Git Product home page Git Product logo

vue-google-login's Introduction

vue-google-login

Button to login with google with really simple setup

Installation

To use the login and logout buttons there is no installation needed, just import and use.

If you want to have access to the auth api then you need add the plugin.

2.0.5 update

Added callback to get the current user without adding the plugin (Thanks rmoscuba)

2.0.1 update

Added support to Edge (Thanks Magyarb)

Added option to render a sign-in button with google UI (Thanks TheTrueRandom)

2.0.0 update

Added support for the full auth api configuration.

Props

    // The Google Sign-In params configuration object. Required.
    // https://developers.google.com/identity/sign-in/web/reference#gapiauth2clientconfig    
    params: Object
    // It gets called if the action (login/logout) is successful.
    onSuccess: Function
    // It gets called if the action (login/logout) fails.
    onFailure: Function
    // It determines if the button is for logging in or for logging out.
    // By default is false so you only need to add it for the logout button
    logoutButton: Boolean
    // Optional, if provided will call gapi.signin2.render with the provided params and render a button with google UI
    // https://developers.google.com/identity/sign-in/web/reference#gapisignin2renderid-options
    renderParams: Object
    // If you are logged in it will return the current user when the component mounts
    // The object it's the same as onSuccess
    onCurrentUser: Function

Usage

    // It can also be imported as { GoogleLogin }
    import GoogleLogin from 'vue-google-login';

    // Button to login
    <GoogleLogin :params="params" :onSuccess="onSuccess" :onFailure="onFailure">Login</GoogleLogin>

alt text

    // Button to login with google ui rendered using the renderParams object
    // The rendered button can't be use to logout since it is rendered by the google api and will only login
    // If you add the logoutButton param to true it will show a normal button without styles
    <GoogleLogin :params="params" :renderParams="renderParams" :onSuccess="onSuccess" :onFailure="onFailure"></GoogleLogin>

alt text

    // Button to logout
    <GoogleLogin :params="params" :logoutButton=true>Logout</GoogleLogin>
    export default {
        name: 'App',
        data() {
            return {
                // client_id is the only required property but you can add several more params, full list down bellow on the Auth api section
                params: {
                    client_id: "xxxxxx"
                },
                // only needed if you want to render the button with the google ui
                renderParams: {
                    width: 250,
                    height: 50,
                    longtitle: true
                }
            }
        },
        components: {
            GoogleLogin
        }
    }

There is no need to add callbacks to the logout button since the api doesn't return anything, you can do it nonetheless to make sure it worked.

When the user successfully signs in, the callback will return an object that contains a lot of information about the user and about the access token granted.

    methods: {
        onSuccess(googleUser) {
            console.log(googleUser);

            // This only gets the user information: id, name, imageUrl and email
            console.log(googleUser.getBasicProfile());
        }
    }

Styling the buttons

Even if it is a component you can think about it as a button, you can add classes, inline styles, etc...

Without renderParams is a button, with renderParams is a div since google injects the button so take it into account when adding styles to the component.

Auth api

This is completely optional. It's just to have access to the Auth api. It is not needed to use the buttons.

First import the plugin

    import { LoaderPlugin } from 'vue-google-login';

Then add the plugin to the Vue instance with the params, client_id is the only property required but you can add some optional.

    Vue.use(LoaderPlugin, {
        client_id: CLIENT_ID
    });

Full list of params

Then you will have access to the auth api. It comes as a promise because the script doesn't load instantly. This way we avoid having to worry about if the script has loaded yet or not.

    Vue.GoogleAuth.then(auth2 => {
        console.log(auth2.isSignedIn.get());
        console.log(auth2.currentUser.get())
    })

Full auth api methods

vue-google-login's People

Contributors

dependabot[bot] avatar magyarb avatar rmartide avatar rmoscuba avatar thetruerandom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

vue-google-login's Issues

Will SameSite cookie changes break this plugin?

I'm no front-end dev and I'm not privy to details about the upcoming SameSite cookie changes.

I just noticed these warnings in Firefox and am wondering if we should expect this plugin to break soon?

Some cookies are misusing the recommended “sameSite“ attribute 2
Cookie “G_ENABLED_IDPS” will be soon rejected because it has the “sameSite” attribute set to “none” or an invalid value, without the “secure” attribute. To learn more about the “sameSite“ attribute, read https://developer.mozilla.org/docs/Web/HTTP/Headers/Set-Cookie/SameSite cb=gapi.loaded_0:278:21
Cookie “G_AUTHUSER_H” will be soon rejected because it has the “sameSite” attribute set to “none” or an invalid value, without the “secure” attribute. To learn more about the “sameSite“
Cookie “SIDCC” will be soon rejected because it has the “sameSite” attribute set to “none” or an invalid value, without the “secure” attribute. To learn more about the “sameSite“ attribute, read https://developer.mozilla.org/docs/Web/HTTP/Headers/Set-Cookie/SameSite

idpiframe_initialization_failed error

Hello

I was trying to use this plugin for implementing google login in my Vue project.
But I'm getting the below error.
"idpiframe_initialization_failed", details: "Not a valid origin for the client: http://localhost:8080…whitelist this origin for your project's client ID."}

I checked my cookie settings, 3rd party cookies are allowed by default.
I already have my localhost in google credentials in OAuth 2.0 Client IDs.

I'm getting this error from vue-google-login project in the console when I'm loading the project. Then when I try to sign in after clicking login button, once I give the password, it goes to onFailure() method instead of onSuccess(googleUser) method. Any hints what might be going wrong in my case?

Localization

Hello,

Is there an option for different languages?

Thanks.

Can't read access_token inside onSuccess anymore

My app was reading the access_token inside the onSuccess method like below:

onSuccess (login) {
    const accessToken = login.ws.access_token
}

Suddenly, it is not available anymore, but inside another attribute:

onSuccess (login) {
    const accessToken = login.xc.access_token
}

Is there a method that I can use instead so I don't relay on an attribute like this?
Any chances that GoogleOAuth2 has been updated?

Error about rendering the component

Hello,
I'm using the vue-google-login component in my project.

I have to go to sign-in page from a different page with route.
When the login page renders the component doesnt show up
When i f5 the page the component renders with no reason.

I even put an interval in beforeMount method and try to rerender the component every three seconds thinking that it would be a problem about the clientId which can be null at initial value.

But couldn't solve it.

Thanks in help.

click signin again have popup but no show select acount

When I first click to signin that have show popup and show to shoose an acount.
but in second click that have show popup but no show to shoose an acount.

image

have some one to tell me that want to show an acount for select every click button signin ?

thank you.

Import LoaderPlugin statement returns undefined object

"vue-google-login": "^2.0.5",

With version 2.0.5, it doesn't seem to export LoaderPlugin.

This is from the docs.

import { LoaderPlugin } from 'vue-google-login';
Then add the plugin to the Vue instance with the params, client_id is the only property required but you can add some optional.

Vue.use(LoaderPlugin, {
    client_id: CLIENT_ID
});

Line 321-325 in vue-google-login seem to add the objects to the exports but the values are undefined except for default.


Note: Using require instead of import shows all of the objects.
const GoogleLogin = require('vue-google-login');

{GoogleLogin: {…}, LoaderPlugin: {…}, default: {…}, __esModule: true}
GoogleLogin: {name: "GoogleLogin", props: {…}, methods: {…}, beforeCreate: ƒ, mounted: ƒ, …}
LoaderPlugin: {install: ƒ}
default: {name: "GoogleLogin", props: {…}, methods: {…}, beforeCreate: ƒ, mounted: ƒ, …}
__esModule: true
proto: Object

How to use it in nuxt with SSR?

I'm trying to use this plugin with nuxt but it does not work in the server side, it says window is not defined.
I've wrapped the component inside tags but it does not work in server side.

Im using Firebase Functions for SSR deployment and this is my code:

Template side

<client-only>
            <GoogleLogin
              :params="gAuthParams"
              :onSuccess="onGoogleSignInSuccess"
              :onFailure="onGoogleSignInError"
              class="btn btn-block btn-success"
            >
              <b-icon icon="google" />
              {{ !isSignIn ? $t('signUp') : $t('signIn') }}
              {{ $t('withGoogle') }}
            </GoogleLogin>
</client-only>

Script side:

import Vue from 'vue'

import GoogleLogin from 'vue-google-login'
import { LoaderPlugin } from 'vue-google-login'
Vue.use(LoaderPlugin, {
  client_id: GoogleAuthCredentials.client_id,
})

export default {
    components: { GoogleLogin }
}

Shows whether or not I am signed in to Google and not my site

Perhaps I am missing something-is there any way to configure this button so that it shows that I am logged into my site or not?

With the default configuration it says "Signed in With Google" whether or not I am signed into my site or not. If I log out of Google, it says "Sign In".

I have the button and my Client ID configured correctly, as far as I know.

Log Out Manually

Hi @rmartide
Thanks for this nice plugin.

I would appreciate you if you can let me know how I can manually log out from google after logging in.

In my Vue app, I log the user in using vue-google-login, but, then after collecting the information, I would log the user out of google.

I am using vue-router, so the login page is on /login. I have already tried LoaderPlugin but could not successfully call it as below:
GoogleAuth.then(auth2 => { auth2.signOut(); })
or even
this.GoogleAuth.then(auth2 => { auth2.signOut(); })

or even
this.$GoogleAuth.then(auth2 => { auth2.signOut(); })

How can this be made possible.

Thanks,

How can I access the authorization code?

Putting response_type = 'code' into params doesn't help. I still receive the same response. But when I used google api in vanilla js with something like this:

gapi.auth2.authorize( { immediate: false, response_type: "code", cookie_policy: "single_host_origin", client_id: "XXX", scope: "email profile", },

I got this response, which is what I'm looking for:

image

Type error with Vue 3

When using Vue 3, I got this error:

Uncaught (in promise) TypeError: Cannot read property '_c' of undefined

Screenshot from 2020-10-12 09-56-17

Button fails to render after users signs out

I have a login button integrated within a dialog, this will show up with options to login, the google sign in button is one of them.

I also have the sign-out button, the will only appear if the user is signed in with google.

After using the sign out button - the login dialog closes, and after opening the login dialog again, the google sign-in button no longer appears.

There's no v-if condition on the button element, or any element that contains it.

Important to mention, right after signing out, before the dialog fully closes, the button still appears, and even changes from "signed in" to "sign in", but after the dialog closes, opening it again will not render the button.

Tested in FireFox and Chrome

The dialog code for reference:

<v-dialog
    v-model="loginDialogOpened"
    width="500">
    <v-card width="500">
      <v-card-title>{{ loginString }}</v-card-title>
      <v-card-text v-if="!isLoggedIn() && loginAsAdmin">
        <v-text-field
          v-model="devadminPassword"
          type="password"
          label="password"></v-text-field>
      </v-card-text>
      <v-card-text v-if="!isLoggedIn()">
        <v-btn large color="grey"
          @click="loginAsAdmin = true;"
          >Login As Admin
        </v-btn>
      </v-card-text>
      <v-card-text>
        <GoogleLogin
          :params="googleParams"
          :renderParams="googleRenderParams"
          :onSuccess="googleOnSuccess"
          :onFailure="googleOnFailure"
        >Login</GoogleLogin>
      </v-card-text>
      <v-card-text v-if="isLoggedIn() && withGoogle">
        <GoogleLogin
          :logoutButton="true"
          :params="googleParams"
          :onSuccess="googleLogoutOnSuccess"
          :onFailure="googleLogoutOnFailure"
        >Logout</GoogleLogin>
      </v-card-text>
      <v-card-actions>
        <v-btn large color="green"
          v-if="!isLoggedIn()"
          @click="login"
          >Submit
        </v-btn>
        <v-btn large color="grey"
          v-if="isLoggedIn()"
          @click="logout">
          Logout
        </v-btn>
      </v-card-actions>
    </v-card>
  </v-dialog>

The dialog - before signing in:
image

After signing in:
image

After signing out:
image

Sign in button fails to show up sometimes

Hi,
I am facing a peculiar problem with my website. I am using the plugin in a login modal (bootstrap-vue) alongside a custom login form. The google sign in button fails to show up only sometimes. Either some or all of the 'samesite' cookie messages in the console are missing, meaning I guess that the scripts did not run (properly). The sign in button then appears after one or more refreshes. What could be causing this?

Thanks.

grantOfflineAccess not from 'Auth api'

Is there an option to call grantOfflineAccess but not by using the 'Auth api'?
I can use the 'Auth api' in parallel to the regular button & onGoogleSuccess function, but its popup the google login screen again (that blocked by chrome)
So I want to call grantOfflineAccess from the onGoogleSuccess function if possible to get the authorization code
Or other option to Sign-In for server-side

Button does not appear

Hi,

Cuando compilo mi WepApp hecha en Quasar-framework a android o electron-win23 el botón de inicio no aparece para hacer Login...

gracias.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.