用户 Profile API
用户个人资料管理接口,操作 profiles 表的 username、display_name 等字段。
路由前缀:/profile
源码:apps/backend/src/routes/profile.ts
认证
所有接口需要 JWT 认证:
Authorization: Bearer <JWT>端点
1. 获取当前用户 Profile
GET /profile获取当前登录用户的 Profile 信息。
响应 200 OK:
json
{
"success": true,
"data": {
"user_id": "uuid",
"username": "zhangsan",
"display_name": "张三",
"avatar_url": "https://example.com/avatar.jpg",
"bio": "个人简介",
"created_at": "2026-01-01T00:00:00.000Z",
"updated_at": "2026-03-07T10:00:00.000Z"
}
}如果用户 Profile 尚未创建(注册异常),
data返回null。
错误码:
| HTTP | 错误 | 说明 |
|---|---|---|
| 401 | Unauthorized | 未认证 |
| 500 | Failed to get profile | 查询失败 |
| 500 | Internal server error | 服务端错误 |
2. 更新当前用户 Profile
PATCH /profile更新当前用户的 username 和/或 display_name。
请求体:
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
username | string | null | 否 | 用户名(传 null 或空字符串可清空) |
display_name | string | null | 否 | 显示名(传 null 或空字符串可清空) |
至少提供一个字段。
username 格式要求:
- 3-39 字符
- 字母或数字开头
- 仅允许字母、数字、连字符(
-)、下划线(_) - 不允许连续连字符(
--) - 自动转为小写
display_name 格式要求:
- 1-50 字符
- 不允许包含空格
响应 200 OK:
json
{
"success": true,
"data": {
"user_id": "uuid",
"username": "zhangsan",
"display_name": "张三",
"avatar_url": "https://example.com/avatar.jpg",
"bio": "个人简介",
"created_at": "2026-01-01T00:00:00.000Z",
"updated_at": "2026-03-07T12:00:00.000Z"
}
}错误码:
| HTTP | 错误 | 说明 |
|---|---|---|
| 400 | No valid fields to update | 未提供有效字段 |
| 400 | Username must be at least 3 characters | username 太短 |
| 400 | Username must be at most 39 characters | username 太长 |
| 400 | Username must start with a letter or number... | username 格式不合法 |
| 400 | Username cannot contain consecutive hyphens | username 含连续连字符 |
| 400 | Display name cannot be empty | display_name 为空 |
| 400 | Display name must be at most 50 characters | display_name 太长 |
| 400 | Display name cannot contain spaces | display_name 含空格 |
| 401 | Unauthorized | 未认证 |
| 409 | Username is already taken | username 已被占用 |
| 500 | Failed to update profile | 更新失败 |
| 500 | Internal server error | 服务端错误 |
3. 检查 Username 可用性
GET /profile/check-username/:username检查指定的 username 是否可用(排除当前用户自己)。
路径参数:
| 参数 | 说明 |
|---|---|
username | 要检查的用户名 |
响应 200 OK(可用):
json
{
"success": true,
"data": {
"available": true
}
}响应 200 OK(不可用 - 格式错误):
json
{
"success": true,
"data": {
"available": false,
"reason": "invalid_format",
"message": "Username must be at least 3 characters"
}
}响应 200 OK(不可用 - 已被占用):
json
{
"success": true,
"data": {
"available": false,
"reason": "taken",
"message": "Username is already taken"
}
}错误码:
| HTTP | 错误 | 说明 |
|---|---|---|
| 401 | Unauthorized | 未认证 |
| 500 | Internal server error | 服务端错误 |