x-hasura-user-id gets undefined after redirect.
Closed this issue · 1 comments
anup-a commented
My current flow is simple, I'm using github provider and on successful login, I'm redirecting to home page.
Issue - The token generated is not accepted as valid authorization token -
Error -
{
"errors": [
{
"extensions": {
"path": "$",
"code": "not-found"
},
"message": "\"x-hasura-user-id\" header is expected but not found"
}
]
}
jwt section of [...nextauth].js
jwt: {
encode: async ({ secret, token, maxAge }) => {
console.log("/* this is token */");
console.log(token);
console.log("/* this is where token ends");
const jwtClaims = {
sub: token.sub.toString(),
name: token.name,
email: token.email,
iat: Date.now() / 1000,
exp: Math.floor(Date.now() / 1000) + 24 * 60 * 60,
"https://hasura.io/jwt/claims": {
"x-hasura-allowed-roles": ["user"],
"x-hasura-default-role": "user",
"x-hasura-role": "user",
"x-hasura-user-id": token.id,
},
};
console.log(jwtClaims);
const encodedToken = jwt.sign(jwtClaims, secret, { algorithm: "HS256" });
return encodedToken;
},
decode: async ({ secret, token, maxAge }) => {
const decodedToken = jwt.verify(token, secret, { algorithms: ["HS256"] });
return decodedToken;
},
},
While debugging in that file, I found that
Intial token after successful signin -
/* this is token */
{
name: 'Anup Aglawe',
email: 'aglawe.anup22@gmail.com',
picture: 'https://avatars.githubusercontent.com/u/29516633?v=4',
sub: '1',
id: '1'
}
token after redirecting to home page -
/* this is token */
{
sub: '1',
name: 'Anup Aglawe',
email: 'aglawe.anup22@gmail.com',
iat: 1615892334.989,
exp: 1615978734,
'https://hasura.io/jwt/claims': {
'x-hasura-allowed-roles': [ 'user' ],
'x-hasura-default-role': 'user',
'x-hasura-role': 'user',
'x-hasura-user-id': '1'
}
}
/* this is where token ends
obiously second time, object doesn't have id
key, so
"x-hasura-user-id": token.id,
-> x-hasura-user-id : undefined.
Am I missing something, here?
Memogcia commented
Found what is missing, you need to replace "x-hasura-user-id": token.id to "x-hasura-user-id": token.sub, because we are not declaring a new id for the token!