Home>

We have created a system that authenticates with both frontend and API using jwt sent from firebase.
I referred the following article of Qiita.
Qiita reference article

However, the following error is returned and authentication is not possible.

error verifying ID token: ID token has invalid 'aud' (audience) claim;expected "vue-auth-491e7" but got "transfer-go";make sure the ID token comes from the same Firebase project as the credential used to authenticate this SDK;see https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve a valid ID token

qiita articlelocalStorage.setItem ('jwt ', res.user.qa),
Since it was not possible to take out with the above code in my environment, I changed it tolocalStorage.setItem ('jwt', res.user.ma).

signIn: function () {
  firebase.auth (). signInWithEmailAndPassword (this.email, this.password) .then (res =>{
    console.log (res.user.ma)
    localStorage.setItem ('jwt', res.user.ma)
    this. $router.push ('/')
  }, err =>{
    alert (err.message)
  })
}

When request is sent to API, jwt is extracted from header and thrown to API.

apiPrivate: async function () {
      let res = await axios.get ('http: // localhost: 8000/private', {
        headers: {'Authorization': `Bearer ${localStorage.getItem ('jwt')}`}
      })
      this.msg = res.data
    }

In main.go, I wrote the article code as it was.

func authMiddleware (next http.HandlerFunc) http.HandlerFunc {
    return func (w http.ResponseWriter, r * http.Request) {
        opt: = option.WithCredentialsFile (os.Getenv ("GOOGLE_APPLICATION_CREDENTIALS"))
        app, err: = firebase.NewApp (context.Background (), nil, opt)
        if err! = nil {
            fmt.Printf ("error:% v \ n", err)
            os.Exit (1)
        }
        auth, err: = app.Auth (context.Background ())
        if err! = nil {
            fmt.Printf ("error:% v \ n", err)
            os.Exit (1)
        }
        authHeader: = r.Header.Get ("Authorization")
        idToken: = strings.Replace (authHeader, "Bearer", "", 1)
        token, err: = auth.VerifyIDToken (context.Background (), idToken)
        if err! = nil {
            fmt.Printf ("error verifying ID token:% v \ n", err)
            w.WriteHeader (http.StatusUnauthorized)
            w.Write ([] byte ("error verifying ID token \ n"))
            return
        }
        log.Printf ("Verified ID token:% v \ n", token)
        next.ServeHTTP (w, r)
    }
}
func public (w http.ResponseWriter, r * http.Request) {
    w.Write ([] byte ("hello public! \ n"))
}
func private (w http.ResponseWriter, r * http.Request) {
    w.Write ([] byte ("hello private! \ n"))
}
func main () {
    allowedOrigins: = handlers.AllowedOrigins ([] string {"http: // localhost: 8080"})
    allowedMethods: = handlers.AllowedMethods ([] string {"GET", "POST", "DELETE", "PUT"})
    allowedHeaders: = handlers.AllowedHeaders ([] string {"Authorization"})
    r: = mux.NewRouter ()
    r.HandleFunc ("/ public", public)
    r.HandleFunc ("/ private", authMiddleware (private))
    fmt.Println ("Server Start!")
    log.Fatal (http.ListenAndServe (": 8000", handlers.CORS (allowedOrigins, allowedMethods, allowedHeaders) (r)))
}

I would like to know why this error is happening and how to solve it.