Авторизация (node js postgres)
Проверка токена
const jwt = require("jsonwebtoken");
const { User } = require("../model/models");
const dotenv = require("dotenv");
dotenv.config();
const { SECRET_KEY } = process.env;
const auth = async(req, res, next) => {
const { authorization = "" } = req.headers;
const [bearer, token] = authorization.split(" ");
try {
if (bearer !== "Bearer") {
res.json({
status: "error",
code: 401,
message: "Unauthorized"
});
}
const { email } = jwt.verify(token, SECRET_KEY);
const user = await User.findOne({where: {email}})
if (!user || !user.token) {
res.json({
status: "error",
code: 401,
message: "Unauthorized"
});
}
req.user = user;
next();
} catch (error) {
if (error.message === "Invalid sugnature") {
error.status = 401;
}
next(error);
}
}
module.exports = auth;
Логин
const login = async (req, res) => {
const { email, password } = req.body
const user = await User.findOne({where: {email}})
if (!user) {
return res.status(401).send('Email or password is incorrect')
}
const passwordCompare = bcrypt.compareSync(password, user.password)
if (!passwordCompare) {
return res.status(401).send('Email or password is incorrect')
}
const token = jwt.sign(email, SECRET_KEY)
await user.update({ token: token })
res.json({
status: "success",
code: 200,
data: {
token
}
})
}
Ошибка "message": "jwt malformed" Статус 500 Не понимаю в чем проблема?