source: backend/utils.js@ 16237c4

Last change on this file since 16237c4 was 16237c4, checked in by Nace Gjorgjievski <nace.gorgievski123@…>, 22 months ago

Added Order Functionality

  • Property mode set to 100644
File size: 702 bytes
Line 
1import jwt from "jsonwebtoken";
2export const generateToken = (user) => {
3 return jwt.sign(
4 { _id: user.id, name: user.name, email: user.email, isAdmin: user.isAdmin },
5 process.env.JWT_SECRET,
6 { expiresIn: "30d" }
7 );
8};
9
10export const isAuth = (req, res, next) => {
11 const authorization = req.headers.authorization;
12 if (authorization) {
13 const token = authorization.slice(7, authorization.length);
14 jwt.verify(token, process.env.JWT_SECRET, (err, decode) => {
15 if (err) {
16 res.status(401).send({ message: "Invalid Token" });
17 } else {
18 req.user = decode;
19 next();
20 }
21 });
22 } else {
23 res.status(401).send({ message: "No Token" });
24 }
25};
Note: See TracBrowser for help on using the repository browser.