feat: 添加认证错误 i18n 翻译

- 添加常见认证错误代码的翻译(中英文)
- 修改 AuthForm 组件处理错误代码
- 支持的错误类型:
  - INVALID_USERNAME_OR_PASSWORD: 用户名或密码错误
  - USER_NOT_FOUND: 用户不存在
  - EMAIL_ALREADY_EXISTS: 邮箱已被注册
  - USERNAME_ALREADY_EXISTS: 用户名已被占用
  - WEAK_PASSWORD: 密码强度不够
  - INVALID_EMAIL: 邮箱格式不正确
  - VALIDATION_ERROR: 输入信息格式错误
  - NETWORK_ERROR: 网络连接失败
  - UNKNOWN_ERROR: 未知错误

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
imeepos 2026-01-13 16:05:30 +08:00
parent ee119d472f
commit 2d0a21205f
3 changed files with 45 additions and 5 deletions

View File

@ -8,7 +8,7 @@ import { Block } from "../ui";
type AuthMode = "login" | "register";
type AuthResponse = { data: unknown; error: { message: string } | null };
type AuthResponse = { data: unknown; error: { message: string; code?: string } | null };
type SignInFn = (params: { username: string; password: string }) => Promise<AuthResponse>;
type SignUpFn = (params: { email: string; password: string; name: string }) => Promise<AuthResponse>;
@ -32,6 +32,18 @@ export function AuthForm({ mode = "login", onSuccess, onModeChange }: AuthFormPr
const isLogin = currentMode === "login";
// 根据错误代码获取翻译后的错误信息
const getErrorMessage = (error: { message: string; code?: string }): string => {
if (error.code) {
const translated = t(`authForm.errors.${error.code}`);
// 如果翻译不存在,返回原始 message
if (!translated.includes('authForm.errors')) {
return translated;
}
}
return error.message;
};
const handleSubmit = async () => {
if (!username.trim() || !password.trim()) {
setError(t("authForm.fillCompleteInfo"));
@ -49,10 +61,16 @@ export function AuthForm({ mode = "login", onSuccess, onModeChange }: AuthFormPr
try {
if (isLogin) {
const res = await signIn.username({ username, password });
if (res.error) throw new Error(res.error.message);
if (res.error) {
setError(getErrorMessage(res.error));
return;
}
} else {
const res = await signUp.email({ email, password, name: username });
if (res.error) throw new Error(res.error.message);
if (res.error) {
setError(getErrorMessage(res.error));
return;
}
}
onSuccess?.();
} catch (e: unknown) {

View File

@ -181,7 +181,18 @@
"loginFailed": "Login failed",
"registerFailed": "Registration failed",
"noAccountRegister": "No account? Register",
"haveAccountLogin": "Already have an account? Login"
"haveAccountLogin": "Already have an account? Login",
"errors": {
"INVALID_USERNAME_OR_PASSWORD": "Invalid username or password",
"USER_NOT_FOUND": "User not found",
"EMAIL_ALREADY_EXISTS": "Email already exists",
"USERNAME_ALREADY_EXISTS": "Username already taken",
"WEAK_PASSWORD": "Password is too weak",
"INVALID_EMAIL": "Invalid email format",
"VALIDATION_ERROR": "Validation error",
"NETWORK_ERROR": "Network connection failed",
"UNKNOWN_ERROR": "Unknown error, please try again later"
}
}
}

View File

@ -181,7 +181,18 @@
"loginFailed": "登录失败",
"registerFailed": "注册失败",
"noAccountRegister": "没有账号?去注册",
"haveAccountLogin": "已有账号?去登录"
"haveAccountLogin": "已有账号?去登录",
"errors": {
"INVALID_USERNAME_OR_PASSWORD": "用户名或密码错误",
"USER_NOT_FOUND": "用户不存在",
"EMAIL_ALREADY_EXISTS": "邮箱已被注册",
"USERNAME_ALREADY_EXISTS": "用户名已被占用",
"WEAK_PASSWORD": "密码强度不够",
"INVALID_EMAIL": "邮箱格式不正确",
"VALIDATION_ERROR": "输入信息格式错误",
"NETWORK_ERROR": "网络连接失败",
"UNKNOWN_ERROR": "未知错误,请稍后重试"
}
}
}