Skip to content

Add an API call to the example #2

@pgoldtho

Description

@pgoldtho

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

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions