Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an API call to the example #2

Open
pgoldtho opened this issue May 28, 2021 · 0 comments
Open

Add an API call to the example #2

pgoldtho opened this issue May 28, 2021 · 0 comments

Comments

@pgoldtho
Copy link

It would be nice if the example included some sample code showing an API call for an authenticated user. It took me a while to figure out how to do this. Here's what I came up with. Not sure if its best practice but seems to work.

  data(){
    return {
      user: '',
      id_token: ''
    }
  },

  methods: {
    async handleClickSignIn(){
      try {
        const googleUser = await this.$gAuth.signIn();
        if (!googleUser) {
          return null;
        }
        console.log("googleUser", googleUser);
        this.user = googleUser.getBasicProfile().getEmail();
        this.id_token = this.$gAuth.instance.currentUser.get().getAuthResponse().id_token;
      } catch (error) {
        //on fail do something
        console.error(error);
        return null;
      }
    },

  async makeApiCall(){
      const res = await fetch(SOME-URL, {
        headers: {
          Authorization: `Bearer ${this.id_token}`
        }
      });

   // process response

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant