2
0
Просмотр исходного кода

📝 docs(auth): add documentation for simple registration feature

- create documentation for simple registration route
- include API endpoint details, request/response formats
- provide usage examples in cURL, JavaScript/TypeScript and React
- explain automatic username conflict handling and default password behavior
- add testing suggestions for username conflict scenarios and token validation
yourname 6 месяцев назад
Родитель
Сommit
1ce2988cc1
1 измененных файлов с 181 добавлено и 0 удалено
  1. 181 0
      .roo/commands/auth-使用简单注册路由.md

+ 181 - 0
.roo/commands/auth-使用简单注册路由.md

@@ -0,0 +1,181 @@
+---
+description: "使用简单注册路由"
+---
+
+## 简单注册路由使用指南
+
+### 新增功能
+已添加简单注册路由,支持只输入用户名即可完成注册,自动处理用户名冲突。
+
+### API端点
+- **方法**: POST
+- **路径**: `/api/v1/auth/register/simple`
+- **Content-Type**: `application/json`
+
+### 请求参数
+```json
+{
+  "username": "用户名"
+}
+```
+
+### 响应格式
+**成功响应 (201 Created)**:
+```json
+{
+  "token": "jwt.token.here",
+  "user": {
+    "id": 123,
+    "username": "用户名1234"
+  }
+}
+```
+
+**错误响应**:
+```json
+{
+  "code": 500,
+  "message": "错误信息"
+}
+```
+
+### 使用示例
+
+#### cURL示例
+```bash
+# 基本使用
+curl -X POST http://localhost:3000/api/v1/auth/register/simple \
+  -H "Content-Type: application/json" \
+  -d '{"username": "testuser"}'
+
+# 带详细输出
+curl -X POST http://localhost:3000/api/v1/auth/register/simple \
+  -H "Content-Type: application/json" \
+  -d '{"username": "testuser"}' \
+  -i
+```
+
+#### JavaScript/TypeScript示例
+```typescript
+// 使用fetch API
+const registerSimple = async (username: string) => {
+  const response = await fetch('/api/v1/auth/register/simple', {
+    method: 'POST',
+    headers: {
+      'Content-Type': 'application/json',
+    },
+    body: JSON.stringify({ username }),
+  });
+  
+  if (response.status === 201) {
+    const data = await response.json();
+    console.log('注册成功:', data);
+    return data;
+  } else {
+    const error = await response.json();
+    throw new Error(error.message);
+  }
+};
+
+// 使用示例
+try {
+  const result = await registerSimple('myusername');
+  localStorage.setItem('token', result.token);
+  console.log('用户已注册并登录:', result.user);
+} catch (error) {
+  console.error('注册失败:', error.message);
+}
+```
+
+#### React示例
+```typescript
+import { useState } from 'react';
+
+const SimpleRegisterForm = () => {
+  const [username, setUsername] = useState('');
+  const [loading, setLoading] = useState(false);
+
+  const handleSubmit = async (e: React.FormEvent) => {
+    e.preventDefault();
+    setLoading(true);
+    
+    try {
+      const response = await fetch('/api/v1/auth/register/simple', {
+        method: 'POST',
+        headers: { 'Content-Type': 'application/json' },
+        body: JSON.stringify({ username }),
+      });
+      
+      if (response.ok) {
+        const data = await response.json();
+        // 处理登录成功
+        localStorage.setItem('token', data.token);
+        alert(`注册成功!用户名: ${data.user.username}`);
+      } else {
+        const error = await response.json();
+        alert(`注册失败: ${error.message}`);
+      }
+    } catch (error) {
+      alert('网络错误,请重试');
+    } finally {
+      setLoading(false);
+    }
+  };
+
+  return (
+    <form onSubmit={handleSubmit}>
+      <input
+        type="text"
+        value={username}
+        onChange={(e) => setUsername(e.target.value)}
+        placeholder="输入用户名"
+        minLength={3}
+        required
+      />
+      <button type="submit" disabled={loading}>
+        {loading ? '注册中...' : '快速注册'}
+      </button>
+    </form>
+  );
+};
+```
+
+### 功能特点
+
+1. **自动用户名处理**:
+   - 如果用户名已存在,自动在末尾添加4位随机数字
+   - 例如: "testuser" → "testuser1234"
+
+2. **默认密码**:
+   - 系统自动设置默认密码为 "123456"
+   - 用户可在后续修改密码
+
+3. **错误处理**:
+   - 用户名格式错误返回400状态码
+   - 服务器错误返回500状态码
+   - 用户名生成失败返回详细错误信息
+
+### 测试建议
+
+1. **测试用户名冲突**:
+   ```bash
+   # 第一次注册
+   curl -X POST http://localhost:3000/api/v1/auth/register/simple \
+     -d '{"username": "testuser"}'
+   
+   # 第二次注册相同用户名
+   curl -X POST http://localhost:3000/api/v1/auth/register/simple \
+     -d '{"username": "testuser"}'
+   ```
+
+2. **验证token有效性**:
+   ```bash
+   # 使用返回的token访问受保护接口
+   curl -H "Authorization: Bearer <token>" \
+     http://localhost:3000/api/v1/auth/me
+   ```
+
+### 相关文件
+- 路由实现: `src/server/api/auth/register/simple.ts`
+- 用户服务: `src/server/modules/users/user.service.ts`
+- 路由注册: `src/server/api/auth/index.ts`