-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from qfdk/vue
Vue
- Loading branch information
Showing
17 changed files
with
1,811 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "web"] | ||
path = web | ||
url = [email protected]:qfdk/EasyDocker-web.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,45 @@ | ||
const checkUser = (req, res, next) => { | ||
res.locals.isLogin = false; | ||
if (req.session.isLogin) { | ||
res.locals.isLogin = true; | ||
next(); | ||
} else { | ||
const username = process.env.EDW_USERNAME, | ||
password = process.env.EDW_PASSWORD; | ||
if (req.body.username === username && req.body.password === password) { | ||
req.session.isLogin = true; | ||
res.redirect('/'); | ||
} else { | ||
res.render('login'); | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const verifyToken = (req, res, next) => { | ||
const rawToken = req.headers['authorization']; | ||
if (!rawToken) { // 检查是否存在token | ||
return res.status(403).send({message: 'No token provided!'}); | ||
} | ||
|
||
const tokenParts = rawToken.split('Bearer '); | ||
if (tokenParts.length !== 2) { // 检查token格式是否正确 | ||
return res.status(403).send({message: 'Invalid token format!'}); | ||
} | ||
|
||
const token = tokenParts[1]; // 获取token部分 | ||
if (!token || token === 'undefined') { // 检查token是否存在或者为字符串"undefined" | ||
return res.status(403).send({message: 'No token provided!'}); | ||
} | ||
|
||
jwt.verify(token, process.env.JWT_SECRET, (err, user) => { // 验证token | ||
if (err) { | ||
return res.status(401).send({message: 'Unauthorized!', err}); | ||
} | ||
req.user = user; | ||
return next(); | ||
}); | ||
}; | ||
|
||
/** | ||
* 权限检查 | ||
* @param role | ||
* @returns {(function(*, *, *): (*))|*} | ||
*/ | ||
const hasRole = (role) => (req, res, next) => { | ||
if (req.user.roles.includes(role)) { | ||
return next(); | ||
} else { | ||
return res.status(401).send({message: 'Unauthorized!'}); | ||
} | ||
|
||
}; | ||
|
||
module.exports = { | ||
checkUser | ||
verifyToken, | ||
hasRole | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.