-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.js
30 lines (26 loc) · 865 Bytes
/
middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const User = require('./models/userModel')
const jwt = require('jsonwebtoken');
module.exports.isLoggedIn = async (req, res, next) => {
const token = req.signedCookies.jwt;
if(token){
const decoded = jwt.verify(token, `${process.env.SECRET}`);
const user = await User.findById(decoded.id);
if(user) next();
else res.status(400).json('invalid token');
}
else{
res.status(400).json('no token');
}
}
module.exports.isSubscribed = async (req, res, next) => {
const token = req.signedCookies.jwt;
if(token){
const decoded = jwt.verify(token, `${process.env.SECRET}`);
const user = await User.findById(decoded.id);
if(user.subscription_status) next();
else res.status(400).json('not subscribed');
}
else{
res.status(400).json('no token');
}
}