I’ve been spending days trying get this working, and I’m close but at this point I’m always getting invalid token
on the server when verifying it the token sent up from the client.
Here’s what I got:
React client (with apollo) that uses react-auth0-spa.js
Here’s the settings:
{
"domain": "myappdomain.auth0.com", // domain from Auth0 SPA app
"clientId": "FstBEWbbWYZFjwyi0UbU1rNkhCI1_wqp", // client id from Auth0 SPA app
"audience": "https://api.myappdomain.com" // from a newly created API on Auth0
}
Using the ApolloClient, I am able to successfully getTokenSilently()
. I send it up with the “Bearer” prefix.
ApolloServer node server
In the context
function I am able to retrieve the same token from the request.headers.authorization with Bearer prefix (shown below). I then utilize jsonwebtoken
library to verify the token, but it ALWAYS gives JsonWebTokenError: invalid token
. Here’s what the token looks like:
Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IlEwTTJSRFUwTkRjM01UUTNPVGxGTUVJd09FUkdNa0kzUkVJeFFVUkZSamxDTlRJMU5UWXhNUSJ9.eyJpc3MiOiJodHRwczovL3RydXRoeC5hdXRoMC5jb20vIiwic3ViIjoiZ29vZ2xlLW9hdXRoMnwxMTI2NzEyMTU3MjUwNDk2NDE5OTciLCJhdWQiOlsiaHR0cHM6Ly9hcGkudHJ1dGh4LmFwcCIsImh0dHBzOi8vdHJ1dGh4LmF1dGgwLmNvbS91c2VyaW5mbyJdLCJpYXQiOjE1NzgxMjIxODUsImV4cCI6MTU3ODIwODU4NSwiYXpwIjoiRnN0QkVXYmJXWVpGand5aTBVYlUxck5raENJMV93cXAiLCJzY29wZSI6Im9wZW5pZCBwcm9maWxlIGVtYWlsIn0.2Sk0vy8f77sPvmoU1yp9xD7Nm-ajEXfJd6dEUoL66dZ6vcqQ1aKGYyppjqpjP-RyZXj77QVvkXxbTtB3Es3fnMh6sBkF4xE1mVsIkJWLt1RvRV9OZVvrgG612GHmxRPBbLIap17XfPH12Oj5kQIKMa_XkWELAXVxCmW1p-gIg0vGcurh8Rh_qrJVVQWta2Je87mnSDbdl2vuKhUPLp3SixV5IlBhTBSs40UBRtH5pUPldJBb43yZdZ5tnwHCgAm_jgwBfWiJDHG2e4a70w3ZPpDDGYr2UGCjN2gaIqDbhMdkf4A0m_oeaa1LRoRRM5OyxQ5IzRASOYkZ09tog-fCug
Here’s the server code that verifies it:
const jwt = require("jsonwebtoken");
// ...
const options = {
audience: "FstBEWbbWYZFjwyi0UbU1rNkhCI1_wqp'", // which is my AUTH_CLIENT_ID
issuer: `https://myappdomain.com/`,
algorithms: ["RS256"]
};
jwt.verify(
token,
"<MY_CLIENT_SECRET_KEY>",
options,
(err, decoded) => {
console.log("err);
if (err) throw new AuthenticationError("You must be logged in");
// ...
}
);
The error is always JsonWebTokenError: invalid token.
NOW, if I change the <MY_CLIENT_SECRET_KEY> to a PEM encoded public key that I get from the Auth0 SPA advanced settings, I get this error:
{ JsonWebTokenError: jwt audience invalid. expected: FstBEWbbWYZFjwyi0UbU1rNkhCI1_wqp
But that IS the audience value I give in the options, so I don’t get it.
Any ideas?