|
1 | 1 | import express from 'express' |
2 | 2 | import jwt from 'jsonwebtoken' |
| 3 | +import { ObjectId } from 'mongodb' |
3 | 4 | import type { ChatContext, ChatMessage } from './chatgpt' |
4 | 5 | import { chatConfig, chatReplyProcess, currentModel } from './chatgpt' |
5 | 6 | import { auth } from './middleware/auth' |
6 | | -import type { ChatOptions } from './storage/model' |
| 7 | +import type { ChatOptions, UserInfo } from './storage/model' |
7 | 8 | import { Status } from './storage/model' |
8 | | -import { clearChat, createChatRoom, createUser, deleteChat, deleteChatRoom, existsChatRoom, getChat, getChatRooms, getChats, getUser, insertChat, renameChatRoom, updateChat, verifyUser } from './storage/mongo' |
| 9 | +import { clearChat, createChatRoom, createUser, deleteChat, deleteChatRoom, existsChatRoom, getChat, getChatRooms, getChats, getUser, getUserById, insertChat, renameChatRoom, updateChat, updateUserInfo, verifyUser } from './storage/mongo' |
9 | 10 | import { sendMail } from './utils/mail' |
10 | 11 | import { checkUserVerify, getUserVerifyUrl, md5 } from './utils/security' |
11 | 12 |
|
@@ -248,14 +249,36 @@ router.post('/user-login', async (req, res) => { |
248 | 249 | throw new Error('用户不存在或密码错误 | User does not exist or incorrect password.') |
249 | 250 | } |
250 | 251 |
|
251 | | - const token = jwt.sign({ email: username, userId: user._id }, process.env.AUTH_SECRET_KEY) |
| 252 | + const token = jwt.sign({ |
| 253 | + name: user.name ? user.name : user.email, |
| 254 | + avatar: user.avatar, |
| 255 | + description: user.description, |
| 256 | + userId: user._id, |
| 257 | + root: username.toLowerCase() === process.env.ROOT_USER, |
| 258 | + }, process.env.AUTH_SECRET_KEY) |
252 | 259 | res.send({ status: 'Success', message: '登录成功 | Login successfully', data: { token } }) |
253 | 260 | } |
254 | 261 | catch (error) { |
255 | 262 | res.send({ status: 'Fail', message: error.message, data: null }) |
256 | 263 | } |
257 | 264 | }) |
258 | 265 |
|
| 266 | +router.post('/user-info', auth, async (req, res) => { |
| 267 | + try { |
| 268 | + const { name, avatar, description } = req.body as UserInfo |
| 269 | + const userId = new ObjectId(req.headers.userId.toString()) |
| 270 | + |
| 271 | + const user = await getUserById(userId) |
| 272 | + if (user == null || user.status !== Status.Normal) |
| 273 | + throw new Error('用户不存在 | User does not exist.') |
| 274 | + await updateUserInfo(userId, { name, avatar, description } as UserInfo) |
| 275 | + res.send({ status: 'Success', message: '更新成功 | Update successfully' }) |
| 276 | + } |
| 277 | + catch (error) { |
| 278 | + res.send({ status: 'Fail', message: error.message, data: null }) |
| 279 | + } |
| 280 | +}) |
| 281 | + |
259 | 282 | router.post('/verify', async (req, res) => { |
260 | 283 | try { |
261 | 284 | const { token } = req.body as { token: string } |
|
0 commit comments