Reputation: 11
I tried confirming the middleware settings and the auth.service and auth.context, increased accepted headers, and temporarily expanded CORS allowed origins to all (*). Since the error isn't specific, I can't seem to move further.
My endpoint:
const { isAuthenticated } = require("../middleware/jwt.middleware.js");
router.get("/verify", isAuthenticated, (req, res) => {
console.log(`req.payload`, req.payload);
res.status(200).json(req.payload);
});
My middleware:
const { expressjwt: jwt } = require("express-jwt");
require("dotenv").config();
const isAuthenticated = jwt({
secret: process.env.TOKEN_SECRET,
algorithms: ["HS256"],
requestProperty: "payload",
credentialsRequired: false,
getToken: getTokenFromHeaders,
});
function getTokenFromHeaders(req) {
if (
req.headers.authorization &&
req.headers.authorization.split(" ")[0] === "Bearer"
) {
const token = req.headers.authorization.split(" ")[1];
return token;
}
return null;
}
module.exports = {
isAuthenticated,
};
on app.js:
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
res.setHeader("Access-Control-Allow-Origin", "http://localhost:5173");
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, PATCH"
);
next();
});
app.use(isAuthenticated);
on my auth.service.js (to confirm I don't need any argument here)
class AuthService {
(...)
verify = () => {
// return axios.post("http://localhost:5005/users/verify");
return this.api.get("/users/verify");
};
}
and finally on my auth.contextjs: (the error starts here on the .verify() because it heads straight to logging the error):
const storeToken = (token) => {
localStorage.setItem("authToken", token);
};
const authenticateUser = () => {
const storedToken = localStorage.getItem("authToken");
if (storedToken) {
authService
.verify()
.then((response) => {
(...)
Upvotes: 0
Views: 38