feat: enhance LoginForm with error handling and loading states for auth options
This commit is contained in:
@@ -20,7 +20,7 @@ import {
|
|||||||
FieldSeparator,
|
FieldSeparator,
|
||||||
} from "@/components/ui/field"
|
} from "@/components/ui/field"
|
||||||
import { Input } from "@/components/ui/input"
|
import { Input } from "@/components/ui/input"
|
||||||
import { KeyRoundIcon } from "lucide-react"
|
import { KeyRoundIcon, Loader2Icon, TriangleAlertIcon } from "lucide-react"
|
||||||
|
|
||||||
function detectWxWorkEnvironment() {
|
function detectWxWorkEnvironment() {
|
||||||
if (typeof navigator === "undefined") {
|
if (typeof navigator === "undefined") {
|
||||||
@@ -41,6 +41,7 @@ export function LoginForm({
|
|||||||
const [isPending, setIsPending] = useState(false)
|
const [isPending, setIsPending] = useState(false)
|
||||||
const [isWxWorkEnv, setIsWxWorkEnv] = useState(false)
|
const [isWxWorkEnv, setIsWxWorkEnv] = useState(false)
|
||||||
const [authOptions, setAuthOptions] = useState<AuthOptions | null>(null)
|
const [authOptions, setAuthOptions] = useState<AuthOptions | null>(null)
|
||||||
|
const [authOptionsError, setAuthOptionsError] = useState<string | null>(null)
|
||||||
const nextPath = searchParams.get("next")
|
const nextPath = searchParams.get("next")
|
||||||
const wxworkError = searchParams.get("wxworkError")
|
const wxworkError = searchParams.get("wxworkError")
|
||||||
const oidcError = searchParams.get("oidcError")
|
const oidcError = searchParams.get("oidcError")
|
||||||
@@ -78,11 +79,13 @@ export function LoginForm({
|
|||||||
.then((options) => {
|
.then((options) => {
|
||||||
if (!cancelled) {
|
if (!cancelled) {
|
||||||
setAuthOptions(options)
|
setAuthOptions(options)
|
||||||
|
setAuthOptionsError(null)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch((error) => {
|
||||||
if (!cancelled) {
|
if (!cancelled) {
|
||||||
setAuthOptions({ wxworkEnabled: false, oidcEnabled: false })
|
setAuthOptions(null)
|
||||||
|
setAuthOptionsError(error instanceof Error ? error.message : "")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -112,8 +115,35 @@ export function LoginForm({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (authOptionsError) {
|
||||||
|
return (
|
||||||
|
<div className={cn("flex flex-col gap-6", className)} {...props}>
|
||||||
|
<Card className="overflow-hidden p-0">
|
||||||
|
<CardContent className="flex min-h-80 flex-col items-center justify-center gap-3 p-8 text-center">
|
||||||
|
<TriangleAlertIcon className="size-8 text-destructive" />
|
||||||
|
<div className="space-y-1">
|
||||||
|
<h1 className="text-lg font-semibold">{t("auth.optionsLoadFailed")}</h1>
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
{authOptionsError || t("api.requestFailed")}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if (!authOptions) {
|
if (!authOptions) {
|
||||||
return null
|
return (
|
||||||
|
<div className={cn("flex flex-col gap-6", className)} {...props}>
|
||||||
|
<Card className="overflow-hidden p-0">
|
||||||
|
<CardContent className="flex min-h-80 flex-col items-center justify-center gap-3 p-8 text-center">
|
||||||
|
<Loader2Icon className="size-7 animate-spin text-muted-foreground" />
|
||||||
|
<p className="text-sm text-muted-foreground">{t("auth.loadingOptions")}</p>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -108,6 +108,8 @@
|
|||||||
"syncingProfile": "Syncing your admin profile and permissions",
|
"syncingProfile": "Syncing your admin profile and permissions",
|
||||||
"loginSuccess": "Signed in. Taking you to the dashboard.",
|
"loginSuccess": "Signed in. Taking you to the dashboard.",
|
||||||
"loginFailed": "Sign-in failed",
|
"loginFailed": "Sign-in failed",
|
||||||
|
"loadingOptions": "Loading sign-in options...",
|
||||||
|
"optionsLoadFailed": "Could not load sign-in options.",
|
||||||
"welcome": "Welcome back",
|
"welcome": "Welcome back",
|
||||||
"loginDescription": "Login to your {brand} account",
|
"loginDescription": "Login to your {brand} account",
|
||||||
"username": "Username",
|
"username": "Username",
|
||||||
|
|||||||
@@ -108,6 +108,8 @@
|
|||||||
"syncingProfile": "将自动同步当前管理员信息与权限数据",
|
"syncingProfile": "将自动同步当前管理员信息与权限数据",
|
||||||
"loginSuccess": "登录成功,正在进入系统",
|
"loginSuccess": "登录成功,正在进入系统",
|
||||||
"loginFailed": "登录失败",
|
"loginFailed": "登录失败",
|
||||||
|
"loadingOptions": "正在加载登录配置...",
|
||||||
|
"optionsLoadFailed": "加载登录配置失败",
|
||||||
"welcome": "欢迎回来",
|
"welcome": "欢迎回来",
|
||||||
"loginDescription": "登录你的 {brand} 账号",
|
"loginDescription": "登录你的 {brand} 账号",
|
||||||
"username": "用户名",
|
"username": "用户名",
|
||||||
|
|||||||
Reference in New Issue
Block a user