Skip to content

Commit

Permalink
feat(login): add login log and improve validation
Browse files Browse the repository at this point in the history
- Implement login log functionality to track user login activities.
- Enhance user validation during the login process to ensure data integrity.
- Refactor code for better readability and maintainability.
  • Loading branch information
MiChongs committed Jun 24, 2024
1 parent 35fe495 commit 5e87643
Show file tree
Hide file tree
Showing 9 changed files with 759 additions and 67 deletions.
5 changes: 4 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ ADMIN_EXPIRES_IN=7d
APP_TOKEN_KEY=123456
#服务器URL
SERVER_PORT=3000
BASE_SERVER_URL=http://localhost
BASE_SERVER_URL=http://localhost
REDIS_PORT=6379
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=123456
40 changes: 40 additions & 0 deletions controllers/appControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ exports.generateCard = function (req, res) {
card_type: req.body.card_type,
appid: req.body.appid,
card_award_num: req.body.card_award_num,
card_memo: req.body.card_memo,
card_code_expire: global.moment().add(parseInt(req.body.card_code_expire), 'days').format('YYYY-MM-DD HH:mm:ss'),
card_time: global.moment().format('YYYY-MM-DD HH:mm:ss')
}).then(r => {
Expand Down Expand Up @@ -356,4 +357,43 @@ exports.generateCard = function (req, res) {
})
})
}
}

exports.cards = function (req, res) {
const err = validationResult(req)
if (!err.isEmpty()) {
const [{msg}] = err.errors
res.status(400).json({
code: 400,
msg: msg,
})
} else {
global.App.findByPk(req.params.appid || req.body.appid).then(app => {
if (app instanceof global.App) {
global.Card.findAll({
where: {
appid: req.params.appid || req.body.appid
}
}).then(cards => {
res.status(200).json({
code: 200,
message: '获取卡成功',
data: cards
})
}).catch(error => {
res.status(500).json({
code: 500,
message: '获取卡失败',
error: error.message
})
})
}
}).catch(error => {
res.status(500).json({
code: 500,
message: '查找应用失败',
error: error.message
})
})
}
}
84 changes: 52 additions & 32 deletions controllers/loginController.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ exports.login = function (req, res, next) {
account: req.body.account,
appid: req.body.appid,
}
}).then(result => {
}).then(async result => {
const user = result;
// 如果用户不存在
if (result == null) {
Expand All @@ -109,38 +109,58 @@ exports.login = function (req, res, next) {
// 校验密码
if (bcrypt.compareSync(req.body.password, user.password)) {
// 生成登录令牌
const token = jwt.sign({
account: req.body.account,
password: req.body.password
}, process.env.APP_TOKEN_KEY, {
expiresIn: '7d',
});
// 创建令牌记录
global.Token.create({
token: token,
appid: req.body.appid,
account: req.body.account,
markcode: req.body.markcode
});
// 返回200成功,包含登录令牌和用户信息
return res.status(200).json({
code: 200,
message: '登录成功',
data: [
{
global.lookupAllGeoInfo(req.clientIp, {
watchForUpdates: true
}).then(
async geo => {
const token = jwt.sign({
account: req.body.account,
password: req.body.password
}, process.env.APP_TOKEN_KEY, {
expiresIn: '7d',
});
await global.Token.create({
token: token,
userInfo: [{
account: result.account,
username: result.name,
avatar: result.avatar,
register_ip: result.register_ip,
register_province: result.register_province,
register_city: result.register_city,
register_time: result.register_time
}]
}
]
});
appid: req.body.appid,
account: req.body.account,
markcode: req.body.markcode
});
await global.LoginLog.create({
user_id: req.body.account,
appid: req.body.appid,
login_time: global.moment().format('YYYY-MM-DD HH:mm:ss'),
login_ip: req.clientIp,
login_address: geo.city.provinceName + geo.city.cityNameZh,
login_device: req.body.markcode,
login_isp: geo.asn.autonomousSystemOrganization,
})
return res.status(200).json({
code: 200,
message: '登录成功',
data: [
{
token: token,
userInfo: [{
account: result.account,
username: result.name,
avatar: result.avatar,
register_ip: result.register_ip,
register_province: result.register_province,
register_city: result.register_city,
register_time: result.register_time
}]
}
]
});
}

).catch(error => {
return res.status(500).json({
code: 500,
message: '获取地理位置信息失败',
error: error.message
});
})
} else {
// 密码错误,返回401
return res.status(401).json({
Expand Down
Loading

0 comments on commit 5e87643

Please sign in to comment.