feat: add basic blog features
This commit is contained in:
21
middlewares/authentication.js
Normal file
21
middlewares/authentication.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import crypto from 'node:crypto';
|
||||
|
||||
const authenticateToken = function(req, res, next) {
|
||||
const token = req.cookies.auth_token;
|
||||
|
||||
if(!token) {
|
||||
res.redirect("/");
|
||||
}
|
||||
|
||||
const data = token.split('.');
|
||||
|
||||
const hash = crypto.createHmac('sha256', process.env.AUTH_JWT_SECRET).update(data[0] + '.' + data[1]).digest('base64url');
|
||||
|
||||
if (hash === data[2]) {
|
||||
next();
|
||||
} else {
|
||||
res.redirect('/users/login');
|
||||
}
|
||||
};
|
||||
|
||||
export default authenticateToken;
|
||||
Reference in New Issue
Block a user