T
T
triplepointer2021-09-13 11:40:09
Google
triplepointer, 2021-09-13 11:40:09

How to implement sign out through gapi?

I am writing Vue js app.

In the index html file, I included the following in the head tag:

<meta name="google-signin-client_id" content="<my_client_id>.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>

When I wrote the Login component, I used the following code (this link helped me https://stackoverflow.com/questions/50769341/how-t... ) to implement the sign in button:

template: `
...

    <div id="google-signin-button"></div>
...
`

methods: {
...
        onSignIn (user) {
          const profile = user.getBasicProfile();
          this.login(profile);
        }
...
}

mounted() {
...
      gapi.signin2.render('google-signin-button', {
        onsuccess: this.onSignIn
      })
...
    },

It works. I tried to use the following code in a Navbar component that is part of other components after a redirect to implement signout, but I get a "TypeError: Cannot read properties of undefined (reading 'getAuthInstance')":

logout: function () {
  var self = this;
    if(confirm("Вы действительно хотите выйти?")) {
      const auth2 = gapi.auth2.getAuthInstance();

      auth2.signOut().then(function () {
          console.log('user signed out');
          self.$store.dispatch('authentication/logout')
          .then(() => {
            self.$router.push('/login')
          })
      });
    }
  },

I have tried using the answer from this link https://stackoverflow.com/questions/29815870/typee... but I am getting a get cookie policy error and don't know what to do about it. Help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
triplepointer, 2021-09-13
@triplepointer

Ok, I forgot to start the webserver. After I used the last mentioned link, got the auth2 global variable and used it for signOut. Done.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question