Merge pull request #11 from snowykami/fix/6-error-login-response

fix: Closes #6 优化登录时的状态反馈,且登录失败后会刷新验证码
This commit is contained in:
2025-09-12 13:38:37 +08:00
committed by GitHub
4 changed files with 368 additions and 360 deletions

View File

@ -49,7 +49,7 @@ func (s *UserService) UserLogin(req *dto.UserLoginReq) (*dto.UserLoginResp, erro
} }
return resp, nil return resp, nil
} else { } else {
return nil, errs.ErrInternalServer return nil, errs.New(http.StatusUnauthorized, "Invalid username or password", nil)
} }
} }

View File

@ -20,6 +20,7 @@ import { useRouter, useSearchParams } from "next/navigation"
import { useTranslations } from "next-intl" import { useTranslations } from "next-intl"
import Captcha from "../common/captcha" import Captcha from "../common/captcha"
import { CaptchaProvider } from "@/models/captcha" import { CaptchaProvider } from "@/models/captcha"
import { toast } from "sonner"
export function LoginForm({ export function LoginForm({
className, className,
@ -47,7 +48,7 @@ export function LoginForm({
setOidcConfigs(res.data || []) // 确保是数组 setOidcConfigs(res.data || []) // 确保是数组
}) })
.catch((error) => { .catch((error) => {
console.error("Error fetching OIDC configs:", error) toast.error(t("fetch_oidc_configs_failed") + (error?.message ? `: ${error.message}` : ""))
setOidcConfigs([]) // 错误时设置为空数组 setOidcConfigs([]) // 错误时设置为空数组
}) })
}, []) }, [])
@ -58,7 +59,8 @@ export function LoginForm({
setCaptchaProps(res.data) setCaptchaProps(res.data)
}) })
.catch((error) => { .catch((error) => {
console.error("Error fetching captcha config:", error) toast.error(t("fetch_captcha_config_failed") + (error?.message ? `: ${error.message}` : ""))
setCaptchaProps(null)
}) })
}, [refreshCaptchaKey]) }, [refreshCaptchaKey])
@ -67,11 +69,14 @@ export function LoginForm({
e.preventDefault() e.preventDefault()
userLogin({ username, password, captcha: captchaToken || "" }) userLogin({ username, password, captcha: captchaToken || "" })
.then(res => { .then(res => {
console.log("Login successful:", res) toast.success(t("login_success") + ` ${res.data.user.nickname || res.data.user.username}`);
router.push(redirectBack) router.push(redirectBack)
}) })
.catch(error => { .catch(error => {
console.error("Login failed:", error) console.log(error)
toast.error(t("login_failed") + (error?.response?.data?.message ? `: ${error.response.data.message}` : ""))
setRefreshCaptchaKey(k => k + 1)
setCaptchaToken(null)
}) })
.finally(() => { .finally(() => {
setIsLogging(false) setIsLogging(false)

View File

@ -56,7 +56,11 @@
}, },
"Login": { "Login": {
"captcha_error": "验证错误,请重试。", "captcha_error": "验证错误,请重试。",
"fetch_captcha_config_failed": "获取验证码失败,请稍后重试。",
"fetch_oidc_configs_failed": "获取第三方身份提供者配置失败。",
"logging": "正在登录...", "logging": "正在登录...",
"login_success": "登录成功!",
"login_failed": "登录失败",
"welcome": "欢迎回来", "welcome": "欢迎回来",
"with_oidc": "使用第三方身份提供者", "with_oidc": "使用第三方身份提供者",
"or_continue_with_local_account": "或使用用户名和密码", "or_continue_with_local_account": "或使用用户名和密码",
@ -69,7 +73,6 @@
"by_logging_in_you_agree_to_our": "登录即表示你同意我们的", "by_logging_in_you_agree_to_our": "登录即表示你同意我们的",
"terms_of_service": "服务条款", "terms_of_service": "服务条款",
"and": "和", "and": "和",
"privacy_policy": "隐私政策", "privacy_policy": "隐私政策"
"login_failed": "登录失败,请检查你的凭据。"
} }
} }