first commit
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { Router } from 'express';
|
||||
import bcrypt from 'bcryptjs';
|
||||
import db from '../db.js';
|
||||
import { auth, sign } from '../middleware.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
// 注册
|
||||
router.post('/register', (req, res) => {
|
||||
const { username, password, nickname, phone, building } = req.body;
|
||||
if (!username || !password || !nickname) {
|
||||
return res.status(400).json({ message: '请填写完整信息' });
|
||||
}
|
||||
if (username.length < 3 || password.length < 6) {
|
||||
return res.status(400).json({ message: '用户名至少3位,密码至少6位' });
|
||||
}
|
||||
const exists = db.prepare('SELECT id FROM users WHERE username = ?').get(username);
|
||||
if (exists) {
|
||||
return res.status(400).json({ message: '用户名已存在' });
|
||||
}
|
||||
const hash = bcrypt.hashSync(password, 10);
|
||||
const info = db
|
||||
.prepare('INSERT INTO users (username, password, nickname, phone, building) VALUES (?, ?, ?, ?, ?)')
|
||||
.run(username, hash, nickname, phone || '', building || '');
|
||||
const user = db.prepare('SELECT * FROM users WHERE id = ?').get(info.lastInsertRowid);
|
||||
res.json({ token: sign(user), user: safeUser(user) });
|
||||
});
|
||||
|
||||
// 登录
|
||||
router.post('/login', (req, res) => {
|
||||
const { username, password } = req.body;
|
||||
const user = db.prepare('SELECT * FROM users WHERE username = ?').get(username);
|
||||
if (!user || !bcrypt.compareSync(password, user.password)) {
|
||||
return res.status(401).json({ message: '用户名或密码错误' });
|
||||
}
|
||||
res.json({ token: sign(user), user: safeUser(user) });
|
||||
});
|
||||
|
||||
// 获取当前用户
|
||||
router.get('/me', auth, (req, res) => {
|
||||
const user = db.prepare('SELECT * FROM users WHERE id = ?').get(req.user.id);
|
||||
if (!user) return res.status(404).json({ message: '用户不存在' });
|
||||
res.json(safeUser(user));
|
||||
});
|
||||
|
||||
// 更新个人资料
|
||||
router.put('/me', auth, (req, res) => {
|
||||
const { nickname, phone, building, bio } = req.body;
|
||||
db.prepare('UPDATE users SET nickname = ?, phone = ?, building = ?, bio = ? WHERE id = ?')
|
||||
.run(nickname, phone || '', building || '', bio || '', req.user.id);
|
||||
const user = db.prepare('SELECT * FROM users WHERE id = ?').get(req.user.id);
|
||||
res.json(safeUser(user));
|
||||
});
|
||||
|
||||
// 修改密码
|
||||
router.put('/password', auth, (req, res) => {
|
||||
const { oldPassword, newPassword } = req.body;
|
||||
const user = db.prepare('SELECT * FROM users WHERE id = ?').get(req.user.id);
|
||||
if (!bcrypt.compareSync(oldPassword, user.password)) {
|
||||
return res.status(400).json({ message: '原密码错误' });
|
||||
}
|
||||
if (newPassword.length < 6) {
|
||||
return res.status(400).json({ message: '新密码至少6位' });
|
||||
}
|
||||
const hash = bcrypt.hashSync(newPassword, 10);
|
||||
db.prepare('UPDATE users SET password = ? WHERE id = ?').run(hash, req.user.id);
|
||||
res.json({ message: '修改成功' });
|
||||
});
|
||||
|
||||
function safeUser(u) {
|
||||
const { password, ...rest } = u;
|
||||
return rest;
|
||||
}
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user