feat: 昵称可修改——PUT /api/auth/nickname + 我的页昵称点击编辑
This commit is contained in:
@@ -539,6 +539,29 @@ router.get('/me', async (req, res) => {
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* 修改昵称
|
||||
* PUT /api/auth/nickname
|
||||
*/
|
||||
router.put('/nickname', async (req, res) => {
|
||||
try {
|
||||
const token = req.header('Authorization')?.replace('Bearer ', '')
|
||||
if (!token) return ApiResponse.unauthorized(res)
|
||||
const decoded = jwt.verify(token, process.env.JWT_SECRET)
|
||||
const nickName = String(req.body?.nickName || '').trim().slice(0, 20)
|
||||
if (!nickName) return ApiResponse.error(res, '昵称不能为空', 400)
|
||||
const user = await User.findByIdAndUpdate(decoded.userId, { nickName }, { new: true })
|
||||
if (!user) return ApiResponse.notFound(res, '用户不存在')
|
||||
return ApiResponse.success(res, { nickName: user.nickName }, '昵称已更新')
|
||||
} catch (error) {
|
||||
if (error.name === 'JsonWebTokenError' || error.name === 'TokenExpiredError') {
|
||||
return ApiResponse.unauthorized(res, '认证令牌无效或已过期')
|
||||
}
|
||||
logger.error('修改昵称失败:', error)
|
||||
return ApiResponse.serverError(res, error.message)
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
* WebView Token 交换接口
|
||||
* POST /api/auth/webview-token
|
||||
|
||||
Reference in New Issue
Block a user