I encountered an error in my console saying, "something went wrong logging in TypeError: Cannot read properties of undefined (reading 'sign')." I am seeking assistance in resolving this issue.
Below is the code snippet:
import { mAdmin } from "../../library/magic-server";
import { jwt } from "jsonwebtoken";
export default async function login(req, res) {
if (req.method === "POST") {
try {
const auth = req.headers.authorization;
const didToken = auth ? auth.substr(7) : "";
console.log({ didToken });
const metadata = await mAdmin.users.getMetadataByToken(didToken);
console.log({ metadata });
const token = jwt.sign(
{
...metadata,
iat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000 + 7 * 24 * 60 * 60),
"https://hasura.io/jwt/claims": {
"x-hasura-allowed-roles": ["user", "admin"],
"x-hasura-default-role": "user",
"x-hasura-user-id": `${metadata.issuer}`,
},
},
'<my secret key>',
{ algorithm: 'HS256' },
function(err, token){
console.log(token);
console.error(err);
}
);
console.log({ token });
res.send({ done: true });
} catch (error) {
console.error("something went wrong logging in", error);
res.status(500).send({ done: false });
}
} else {
res.send({ done: false });
}
}
I made a POST request with an authorization token using Postman to localhost:3000/api/login. Metadata and didToken were successfully logged in the console, suggesting that the issue lies within the jwt line.
The console also displayed the message: "at login (webpack-internal:///(api)/./pages/api/login.js:22:74)" despite there not being a 74th character in line 22.