I’m a developer working on bringing auth0 to our company. We have a SPA Vue app that I’ve setup with @auth0/auth0-spa-js, and a node API that uses jwt/jwks to use the bearer token for authentication.
I develop on my localhost and ended up doing this to invoke the API:
It feels a bit clunky, but seems to work. Does anyone have a recommended alternative? I try to avoid having different code paths in dev and production but this was the best I could do here.
I shouldn’t have said it failed. Rather, it returns what I guess is an " opaque" token in some circumstances (probably when I’m on localhost). This opaque token will not validate with the API, and the sample code referenced above demonstrates no handling of it.
getTokenWithPopup always seems to return a valid auth token so I use that as the fallback on my localhost development. A popup briefly flickers but I assume I can use that same code path in both development and production.
The opaque token is an access token intended for internal auth0 APIs, like the /userinfo endpoint. If you want to get an encoded JWT you must declare an external audience.
To reiterate though, this code does work, and it’s almost exactly what the Auth0 samples have. I’m just confused why the API has these separate getTokenSilently() and getTokenWithPopup() methods, and why in particular getTokenSilently() sometimes returns an opaque JWT and other times a token that’s perfectly compliant with what the API expects.
Shouldn’t the API give me a method I can always trust will return a JWT that the API can accept?
You should only get an opaque token if you aren’t specifying an audience for the call. You are intermittently getting an opaque token?
Have you tried specifying the audience when you initialize the auth0 client? Take a look at main.js in the vue quickstart I linked above. It should be the API identifier.