created bacis structure and routes for creating user and logging in

This commit is contained in:
QkoSad
2022-12-05 14:25:27 +02:00
commit 8fd0b86492
10 changed files with 4616 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
const jwt = require("jsonwebtoken");
const asyncHandler = require("express-async-handler");
const User = require("../models/userModel");
const protect = asyncHandler(async (req, res, next) => {
let token;
if (
req.headers.authorization &&
req.headers.authorization.startsWith("Bearer")
) {
try {
token = req.headers.authorization.split(" ")[1];
const decoded = jwt.verify(token, process.env.JWT_SECRET);
req.user = await User.findById(decoded.id).select("-password");
next();
} catch (error) {
console.log(error);
res.status(401);
throw new Error("Not authorized");
}
}
if (!token) {
res.status(401);
throw new Error("Not authorized");
}
});
module.exports = protect;