fix: bug
This commit is contained in:
@@ -2,6 +2,7 @@ import React, { useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { ActivityIndicator, TextInput } from "react-native";
|
||||
import { authClient, setAuthToken, useSession } from "../../lib/auth";
|
||||
import type { ApiError } from "../../lib/types";
|
||||
import { Block } from "../ui";
|
||||
import { Button } from "../ui/button";
|
||||
import Text from "../ui/Text";
|
||||
@@ -29,15 +30,17 @@ export function AuthForm({ mode = "login", onSuccess, onModeChange }: AuthFormPr
|
||||
const isLogin = currentMode === "login";
|
||||
|
||||
// 根据错误代码获取翻译后的错误信息
|
||||
const getErrorMessage = (error: { message: string; code?: string }): string => {
|
||||
const getErrorMessage = (error: ApiError): string => {
|
||||
if (error.code) {
|
||||
const translated = t(`authForm.errors.${error.code}`);
|
||||
// 如果翻译不存在,返回原始 message
|
||||
if (!translated.includes('authForm.errors')) {
|
||||
const key = `authForm.errors.${error.code}`;
|
||||
const translated = t(key);
|
||||
// 如果翻译存在,返回翻译结果
|
||||
if (translated !== key) {
|
||||
return translated;
|
||||
}
|
||||
}
|
||||
return error.message;
|
||||
// 返回 message 或默认错误信息
|
||||
return error.message || t("authForm.errors.UNKNOWN_ERROR");
|
||||
};
|
||||
|
||||
const handleSubmit = async () => {
|
||||
@@ -56,33 +59,49 @@ export function AuthForm({ mode = "login", onSuccess, onModeChange }: AuthFormPr
|
||||
|
||||
try {
|
||||
if (isLogin) {
|
||||
await signIn.username({ username, password }, {
|
||||
const result = await signIn.username({ username, password }, {
|
||||
onSuccess: async (ctx) => {
|
||||
const authToken = ctx.response.headers.get('set-auth-token')
|
||||
if (authToken) {
|
||||
setAuthToken(authToken)
|
||||
await setAuthToken(authToken)
|
||||
}
|
||||
},
|
||||
onError: (ctx) => {
|
||||
setError(getErrorMessage(ctx.error));
|
||||
console.error(`[LOGIN] username login error`, ctx)
|
||||
},
|
||||
});
|
||||
|
||||
// 检查返回结果中是否有错误
|
||||
if (result.error) {
|
||||
setError(getErrorMessage(result.error));
|
||||
return;
|
||||
}
|
||||
|
||||
// 登录成功
|
||||
onSuccess?.();
|
||||
} else {
|
||||
await signUp.email({ email, password, name: username }, {
|
||||
const result = await signUp.email({ email, password, name: username }, {
|
||||
onSuccess: async (ctx) => {
|
||||
const authToken = ctx.response.headers.get('set-auth-token')
|
||||
if (authToken) {
|
||||
setAuthToken(authToken)
|
||||
await setAuthToken(authToken)
|
||||
}
|
||||
},
|
||||
onError: (ctx) => {
|
||||
setError(getErrorMessage(ctx.error));
|
||||
console.error(`[LOGIN] username login error`, ctx)
|
||||
console.error(`[REGISTER] email register error`, ctx)
|
||||
},
|
||||
});
|
||||
|
||||
// 检查返回结果中是否有错误
|
||||
if (result.error) {
|
||||
setError(getErrorMessage(result.error));
|
||||
return;
|
||||
}
|
||||
|
||||
// 注册成功
|
||||
onSuccess?.();
|
||||
}
|
||||
onSuccess?.();
|
||||
} catch (e: unknown) {
|
||||
const msg = e instanceof Error ? e.message : (isLogin ? t("authForm.loginFailed") : t("authForm.registerFailed"));
|
||||
setError(msg);
|
||||
@@ -139,7 +158,9 @@ export function AuthForm({ mode = "login", onSuccess, onModeChange }: AuthFormPr
|
||||
/>
|
||||
|
||||
{error ? (
|
||||
<Text className="text-red-500 text-sm mb-4 text-center">{error}</Text>
|
||||
<Text style={{ color: '#ef4444' }} className="text-sm mb-4 text-center">
|
||||
{error}
|
||||
</Text>
|
||||
) : null}
|
||||
|
||||
<Button
|
||||
|
||||
Reference in New Issue
Block a user