Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(web): handle autofilled Web App login credentials (#44474)

xhdnoahc507e0f623f7
diff --git a/web/app/(shareLayout)/webapp-signin/components/__tests__/mail-and-password-auth.spec.tsx b/web/app/(shareLayout)/webapp-signin/components/__tests__/mail-and-password-auth.spec.tsx
index ed651905dba..d2426e175d3 100644
--- a/web/app/(shareLayout)/webapp-signin/components/__tests__/mail-and-password-auth.spec.tsx
+++ b/web/app/(shareLayout)/webapp-signin/components/__tests__/mail-and-password-auth.spec.tsx
@@ -75,6 +75,88 @@ describe('MailAndPasswordAuth', () => {
})
})
+ it('initializes the email from the login link', () => {
+ searchParams.set('email', '[email protected]')
+ try {
+ render(<MailAndPasswordAuth isEmailSetup />)
+ expect(screen.getByRole('textbox', { name: 'login.email' })).toHaveValue('[email protected]')
+ expect(screen.getByRole('button', { name: 'login.signBtn' })).toBeEnabled()
+ } finally {
+ searchParams.delete('email')
+ }
+ })
+
+ it('submits native autofill values without input events', async () => {
+ const user = userEvent.setup()
+ render(<MailAndPasswordAuth isEmailSetup />)
+ const submit = screen.getByRole('button', { name: 'login.signBtn' })
+ expect(submit).toBeEnabled()
+ const email = screen.getByRole<HTMLInputElement>('textbox', { name: 'login.email' })
+ const password = screen.getByLabelText<HTMLInputElement>('login.password', { exact: true })
+ email.value = '[email protected]'
+ password.value = 'autofilled-password'
+ await user.click(submit)
+ await waitFor(() =>
+ expect(webAppLoginMock).toHaveBeenCalledWith({
+ url: '/login',
+ body: {
+ email: '[email protected]',
+ password: 'YXV0b2ZpbGxlZC1wYXNzd29yZA==',
+ remember_me: true,
+ },
+ }),
+ )
+ })
+
+ it.each([
+ ['', 'valid-password', 'login.error.emailEmpty'],
+ ['not-an-email', 'valid-password', 'login.error.emailInValid'],
+ ['[email protected]', '', 'login.error.passwordEmpty'],
+ ['[email protected]', ' ', 'login.error.passwordEmpty'],
+ ])('validates credentials inline: %s / %s', async (email, password, error) => {
+ const user = userEvent.setup()
+ render(<MailAndPasswordAuth isEmailSetup />)
+ if (email) await user.type(screen.getByRole('textbox', { name: 'login.email' }), email)
+ if (password)
+ await user.type(screen.getByLabelText('login.password', { exact: true }), password)
+ await user.click(screen.getByRole('button', { name: 'login.signBtn' }))
+ expect(await screen.findByText(error)).toBeVisible()
+ expect(webAppLoginMock).not.toHaveBeenCalled()
+ })
+
+ it('preserves native autofilled values when toggling password visibility', async () => {
+ const user = userEvent.setup()
+ render(<MailAndPasswordAuth isEmailSetup />)
+ const email = screen.getByRole<HTMLInputElement>('textbox', { name: 'login.email' })
+ const password = screen.getByLabelText<HTMLInputElement>('login.password', { exact: true })
+ email.value = '[email protected]'
+ password.value = 'autofilled-password'
+ await user.click(screen.getByRole('button', { name: 'login.showPassword' }))
+ expect(email).toHaveValue('[email protected]')
+ expect(password).toHaveValue('autofilled-password')
+ expect(webAppLoginMock).not.toHaveBeenCalled()
+ })
+
+ it('blocks repeated submission while login is pending and recovers after failure', async () => {
+ const user = userEvent.setup()
+ let rejectLogin!: (error: Error) => void
+ webAppLoginMock.mockReturnValue(
+ new Promise((_, reject) => {
+ rejectLogin = reject
+ }),
+ )
+ render(<MailAndPasswordAuth isEmailSetup />)
+ await user.type(screen.getByRole('textbox', { name: 'login.email' }), '[email protected]')
+ await user.type(screen.getByLabelText('login.password', { exact: true }), 'valid-password')
+ const submit = screen.getByRole('button', { name: 'login.signBtn' })
+ await user.click(submit)
+ await waitFor(() => expect(submit).toHaveAttribute('aria-disabled', 'true'))
+ await user.click(submit)
+ expect(webAppLoginMock).toHaveBeenCalledTimes(1)
+ rejectLogin(new Error('Login failed'))
+ await waitFor(() => expect(submit).not.toHaveAttribute('aria-disabled', 'true'))
+ })
+
it('names the password visibility action for its current state', async () => {
const user = userEvent.setup()
render(<MailAndPasswordAuth isEmailSetup />)
diff --git a/web/app/(shareLayout)/webapp-signin/components/mail-and-password-auth.tsx b/web/app/(shareLayout)/webapp-signin/components/mail-and-password-auth.tsx
index c09b324f5dc..78783c3a930 100644
--- a/web/app/(shareLayout)/webapp-signin/components/mail-and-password-auth.tsx
+++ b/web/app/(shareLayout)/webapp-signin/components/mail-and-password-auth.tsx
@@ -1,6 +1,6 @@
'use client'
import { Button } from '@langgenius/dify-ui/button'
-import { Field, FieldLabel } from '@langgenius/dify-ui/field'
+import { Field, FieldError, FieldLabel } from '@langgenius/dify-ui/field'
import { Form } from '@langgenius/dify-ui/form'
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { Input } from '@langgenius/dify-ui/input'
@@ -34,9 +34,6 @@ export default function MailAndPasswordAuth({ isEmailSetup }: MailAndPasswordAut
const searchParams = useSearchParams()
const [showPassword, setShowPassword] = useState(false)
const emailFromLink = decodeURIComponent(searchParams.get('email') || '')
- const [email, setEmail] = useState(emailFromLink)
- const [password, setPassword] = useState('')
-
const [isLoading, setIsLoading] = useState(false)
const redirectUrl = searchParams.get('redirect_url')
const embeddedUserId = useWebAppStore((s) => s.embeddedUserId)
@@ -46,25 +43,13 @@ export default function MailAndPasswordAuth({ isEmailSetup }: MailAndPasswordAut
replaceLoginRedirect(getClientLoginFallback(), router.replace, basePath)
}, [redirectUrl, router])
- const handleEmailPasswordLogin = async () => {
+ const handleEmailPasswordLogin = async (email: string, password: string) => {
+ if (isLoading) return
const loginRedirect = resolveWebAppLoginRedirect(redirectUrl, window.location.origin)
if (!loginRedirect) {
replaceLoginRedirect(getClientLoginFallback(), router.replace, basePath)
return
}
- if (!email) {
- toast.error(t(($) => $['error.emailEmpty'], { ns: 'login' }))
- return
- }
- if (!emailRegex.test(email)) {
- toast.error(t(($) => $['error.emailInValid'], { ns: 'login' }))
- return
- }
- if (!password?.trim()) {
- toast.error(t(($) => $['error.passwordEmpty'], { ns: 'login' }))
- return
- }
-
try {
setIsLoading(true)
const loginData = {
@@ -104,29 +89,47 @@ export default function MailAndPasswordAuth({ isEmailSetup }: MailAndPasswordAut
}
return (
- <Form
- onFormSubmit={() => {
- void handleEmailPasswordLogin()
+ <Form<{ email: string; password: string }>
+ onFormSubmit={({ email, password }) => {
+ void handleEmailPasswordLogin(email, password)
}}
>
- <Field name="email" className="mb-3 block">
+ <Field
+ name="email"
+ className="mb-3 block"
+ validate={(value) => {
+ if (typeof value !== 'string' || !value)
+ return t(($) => $['error.emailEmpty'], { ns: 'login' })
+ if (!emailRegex.test(value)) return t(($) => $['error.emailInValid'], { ns: 'login' })
+ return null
+ }}
+ >
<FieldLabel className="my-2 py-0 text-sm leading-5 font-semibold text-text-secondary">
{t(($) => $.email, { ns: 'login' })}
</FieldLabel>
<div className="mt-1">
<Input
- value={email}
- onValueChange={setEmail}
+ defaultValue={emailFromLink}
id="email"
- type="email"
+ type="text"
+ inputMode="email"
autoComplete="email"
spellCheck={false}
placeholder={t(($) => $.emailPlaceholder, { ns: 'login' }) || ''}
/>
</div>
+ <FieldError />
</Field>
- <Field name="password" className="mb-3 block">
+ <Field
+ name="password"
+ className="mb-3 block"
+ validate={(value) => {
+ if (typeof value !== 'string' || !value.trim())
+ return t(($) => $['error.passwordEmpty'], { ns: 'login' })
+ return null
+ }}
+ >
<div className="my-2 flex items-center justify-between">
<FieldLabel className="py-0 text-sm leading-5 font-semibold text-text-secondary">
{t(($) => $.password, { ns: 'login' })}
@@ -142,8 +145,6 @@ export default function MailAndPasswordAuth({ isEmailSetup }: MailAndPasswordAut
</div>
<InputGroup className="mt-1">
<InputGroupInput
- value={password}
- onValueChange={setPassword}
id="password"
type={showPassword ? 'text' : 'password'}
autoComplete="current-password"
@@ -152,6 +153,7 @@ export default function MailAndPasswordAuth({ isEmailSetup }: MailAndPasswordAut
/>
<InputGroupAddon align="inline-end">
<IconButton
+ type="button"
size="lg"
variant="ghost"
aria-label={t(($) => $[showPassword ? 'hidePassword' : 'showPassword'], {
@@ -163,15 +165,11 @@ export default function MailAndPasswordAuth({ isEmailSetup }: MailAndPasswordAut
</IconButton>
</InputGroupAddon>
</InputGroup>
+ <FieldError />
</Field>
<div className="mb-2">
- <Button
- type="submit"
- variant="primary"
- disabled={isLoading || !email || !password}
- className="w-full"
- >
+ <Button type="submit" variant="primary" loading={isLoading} className="w-full">
{t(($) => $.signBtn, { ns: 'login' })}
</Button>
</div>