后端完成jwt登录接口
This commit is contained in:
50
Server/src/APIs/Console/Login.ts
Normal file
50
Server/src/APIs/Console/Login.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { API } from "../../Plugs/API/API";
|
||||
import ServerStdResponse from "../../ServerStdResponse";
|
||||
import MySQLConnection from '../../Plugs/MySQLConnection'
|
||||
import MountUserAgent from "../../Plugs/Middleware/MountUserAgent";
|
||||
import MountIP from "../../Plugs/Middleware/MountIP";
|
||||
import CheckCaptchaPassed from "../../Plugs/Middleware/CheckCaptchaPassed";
|
||||
import config from "../../config";
|
||||
import jwt from 'jsonwebtoken'
|
||||
import crypto from 'crypto'
|
||||
|
||||
// 登录
|
||||
class Login extends API {
|
||||
constructor() {
|
||||
super('POST', '/console/login', CheckCaptchaPassed, MountUserAgent, MountIP);
|
||||
}
|
||||
|
||||
public async onRequset(data: any, res: any) {
|
||||
let { username, password, _ip, _userAgent } = data;
|
||||
if (!username || !password) {
|
||||
return res.json(ServerStdResponse.PARAMS_MISSING);
|
||||
}
|
||||
|
||||
// 检查用户是否存在
|
||||
let userInfoRes = await MySQLConnection.execute('SELECT * FROM user WHERE username = ?', [username]);
|
||||
if(!userInfoRes){
|
||||
return res.json(ServerStdResponse.SERVER_ERROR);
|
||||
}
|
||||
if (userInfoRes.length != 1) {
|
||||
return res.json(ServerStdResponse.USER.NOTFOUND);
|
||||
}
|
||||
userInfoRes = userInfoRes[0];
|
||||
// 检查密码是否正确
|
||||
if(crypto.createHash('sha256').update(`${userInfoRes.salt}${password}`).digest('hex') != userInfoRes.password){
|
||||
return res.json(ServerStdResponse.USER.PASSWORD_ERROR);
|
||||
}
|
||||
|
||||
// 准备jwtToken
|
||||
const jwtPayload = {
|
||||
uuid: userInfoRes.uuid,
|
||||
loginTime: Date.now()
|
||||
}
|
||||
let jwtToken = jwt.sign(jwtPayload, config.jwt.secret, { expiresIn: config.jwt.expiresIn });
|
||||
|
||||
// 写入登录日志
|
||||
MySQLConnection.execute('INSERT INTO user_login_log (user_uuid, ip, user_agent, time) VALUES (?,?,?,?)', [userInfoRes.uuid, _ip, _userAgent, Date.now()]);
|
||||
return res.json({ ...ServerStdResponse.OK, data: { token: jwtToken } });
|
||||
}
|
||||
}
|
||||
|
||||
export default Login;
|
||||
@@ -15,6 +15,7 @@ import BlogComment from "../APIs/BlogComment";
|
||||
import GetBlogComment from "../APIs/GetBlogComment";
|
||||
import GetCaptcha from "../APIs/GetCaptcha";
|
||||
import CheckCaptcha from "../APIs/CheckCaptcha";
|
||||
import Login from "../APIs/Console/Login";
|
||||
|
||||
class Server {
|
||||
private logger = new Logger('Server');
|
||||
@@ -37,6 +38,8 @@ class Server {
|
||||
this.apiLoader.add(GetCaptcha);
|
||||
this.apiLoader.add(CheckCaptcha);
|
||||
|
||||
this.apiLoader.add(Login);
|
||||
|
||||
this.apiLoader.start(config.apiPort);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,16 @@ const ServerStdResponse = {
|
||||
code: -5002,
|
||||
message: 'captcha is not right, please try again'
|
||||
}
|
||||
},
|
||||
USER: {
|
||||
NOTFOUND: {
|
||||
code: -6000,
|
||||
message: 'user is not found'
|
||||
},
|
||||
PASSWORD_ERROR:{
|
||||
code: -6001,
|
||||
message: 'user password is error'
|
||||
}
|
||||
}
|
||||
} as const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user