已添加简单注册路由,支持只输入用户名即可完成注册,自动处理用户名冲突。
/api/v1/auth/register/simpleapplication/json{
"username": "用户名"
}
成功响应 (201 Created):
{
"token": "jwt.token.here",
"user": {
"id": 123,
"username": "用户名1234"
}
}
错误响应:
{
"code": 500,
"message": "错误信息"
}
# 基本使用
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
import { authClient } from '@/client/api';
import type { InferResponseType, InferRequestType } from 'hono/client';
// 使用RPC客户端
const registerSimple = async (username: string) => {
const response = await authClient.register.simple.$post({
json: { 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);
}
};
// 类型定义(可选)
type SimpleRegisterRequest = InferRequestType<typeof authClient.register.simple.$post>['json'];
type SimpleRegisterResponse = InferResponseType<typeof authClient.register.simple.$post, 201>;
// 使用示例
try {
const result = await registerSimple('myusername');
localStorage.setItem('token', result.token);
console.log('用户已注册并登录:', result.user);
} catch (error) {
console.error('注册失败:', error.message);
}
import { useState } from 'react';
import { authClient } from '@/client/api';
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 authClient.register.simple.$post({
json: { 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>
);
};
本项目使用Hono RPC客户端进行类型安全的API调用,提供以下优势:
import type { InferResponseType, InferRequestType } from 'hono/client';
import { authClient } from '@/client/api';
// 请求类型定义
type SimpleRegisterRequest = InferRequestType<typeof authClient.register.simple.$post>['json'];
// 响应类型定义
type SimpleRegisterResponse = InferResponseType<typeof authClient.register.simple.$post, 201>;
// 错误响应类型
type ErrorResponse = InferResponseType<typeof authClient.register.simple.$post, 400 | 500>;
自动用户名处理:
默认密码:
错误处理:
测试用户名冲突:
# 第一次注册
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"}'
验证token有效性:
# 使用返回的token访问受保护接口
curl -H "Authorization: Bearer <token>" \
http://localhost:3000/api/v1/auth/me
src/server/api/auth/register/simple.tssrc/server/modules/users/user.service.tssrc/server/api/auth/index.ts