mirror of
https://github.com/pawelmalak/flame.git
synced 2026-02-28 01:13:11 +08:00
Added auth middleware. Added access control to apps
This commit is contained in:
25
middleware/auth.js
Normal file
25
middleware/auth.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
const auth = (req, res, next) => {
|
||||
const authHeader = req.header('Authorization');
|
||||
let token;
|
||||
let tokenIsValid = false;
|
||||
|
||||
if (authHeader && authHeader.startsWith('Bearer ')) {
|
||||
token = authHeader.split(' ')[1];
|
||||
}
|
||||
|
||||
if (token) {
|
||||
try {
|
||||
jwt.verify(token, process.env.SECRET);
|
||||
} finally {
|
||||
tokenIsValid = true;
|
||||
}
|
||||
}
|
||||
|
||||
req.isAuthenticated = tokenIsValid;
|
||||
|
||||
next();
|
||||
};
|
||||
|
||||
module.exports = auth;
|
||||
Reference in New Issue
Block a user