Vuejs Global Feature Using Google Auth Signin

I am using vuejs 2 and am having problems using the google auth icon.

I successfully installed and got the exit functions and user profile functions using vue:

export default {

  data() {
    return {
      user: null
    };
  },

  methods: {
    getUserProfile() {
          const profile = gapi.auth2.currentUser.get().getBasicProfile();

          console.log(profile.getIdToken());
      },

      signOut() {
          const auth2 = gapi.auth2.getAuthInstance();

          auth2.signOut().then(function () {
              console.log('user signed out');
          });
      }
  },
};

My main problem here is the function onSignIn(googleUser)from

<div class="g-signin2" data-onsuccess="onSignIn"></div>

data-onsuccess="onSignIn"looking for js function outside of vue instance. I tried to add a function onSignIn(googleUser)to my HTML file, for example:

<script>
    function onSignIn(googleUser) {
        const auth2 = gapi.auth2.init();

        if (auth2.isSignedIn.get()) {
            const profile = auth2.currentUser.get().getBasicProfile();

            console.log(profile.getName());
            console.log(googleUser.getAuthResponse().id_token);
            console.log(googleUser.getAuthResponse().uid);
            console.log(auth2.currentUser.get().getId());
        }
    }
</script>

This works as expected, but I wanted to know if I could add this to my vue file instead of my own javascript method, since inside this function I will call other vue methods.

Or is there a way I could add a function onSignIn(googleUser)to vue and then call it when Google Auth ends?

+4
1

gapi.signin2.render mounted hook

<template>
  <div id="google-signin-btn"></div>
</template>

<script>
export default {
  methods: {
    onSignIn (user) {
      // do stuff, for example
      const profile = user.getBasicProfile()
    }
  },
  mounted() {
    gapi.signin2.render('google-signin-btn', { // this is the button "id"
      onsuccess: this.onSignIn // note, no "()" here
    })
  }
}
</script>

. https://developers.google.com/identity/sign-in/web/reference#gapisignin2renderid-options

+2

Source: https://habr.com/ru/post/1673657/


All Articles