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.
-
Answer # 1
Related articles
- how to use a colon in a go slice
- how to use css with sublime text 3
- php - what you can do with instagram api
- how can i connect to mysql from php?
- how to play music with javascript
- python - can pip be used with google_colaboratory?
- php - how can i make it externally accessible?
- how to use flexbox with html css
- c ++ - how to read data with fscanf
- how to use bootstrap4 with laravel
- go - how to use function return
- go - how to use envconfig
- html - how to track events with gtm
- java - how to move dp with three subscripts
- php - how can i make an odd decision directly?
- windows - how to display "on" with `echo`?
- can i use linq with visualstudio2005
- python - how to run ie11 with selenium
- i can't do go get with docker-compose
- how to use innertext with vba
Related questions
- javascript - i want to pass the saved data of firebase to data () of vuejs
- javascript - image is not rendered in html2canvas
- save - firebaseerror: [code = invalid-argument] is displayed when rating is used in form
- javascript - use vue's v-for to display in descending order of goodness, and display the same posts collectively (ranking functi
- go - i want to make the input value set in the gin controller the initial value of vuejs
- i want to continue redirecting from the authentication email when registering the user, but i have logged out (vuejs/firebase) s
- vuejs - property or method "currentuser" is not defined when authenticating a user in firestore
- api - i want to pass json from vue to axios to go but it doesn't work
- vuejs - i want to read the image directly by referring to the url saved in firebase nuxtjs
- compile error due to possible null in firebase+typescript+vue typedef
I think you are creating a JWT on the firebase side, but it doesn't seem to be configured properly.
The code looks fine.