Compare commits

..

2 Commits

Author SHA1 Message Date
yyh
68f4dff852 refactor(web): migrate webapp email code fields 2026-08-09 22:02:45 +08:00
yyh
a85e583720 fix(web): restore webapp email code form submission
Move the email code request behind the native form submit boundary, add stable email field metadata, and verify Enter submits exactly once.
2026-08-09 22:02:09 +08:00
5 changed files with 90 additions and 26 deletions

View File

@@ -141,11 +141,6 @@
"count": 1
}
},
"web/app/(shareLayout)/webapp-signin/components/mail-and-code-auth.tsx": {
"no-restricted-imports": {
"count": 1
}
},
"web/app/(shareLayout)/webapp-signin/components/mail-and-password-auth.tsx": {
"jsx_a11y/tabindex-no-positive": {
"count": 3

View File

@@ -0,0 +1,58 @@
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import MailAndCodeAuth from '../mail-and-code-auth'
const pushMock = vi.fn()
const replaceMock = vi.fn()
const setCountdownLeftTimeMock = vi.fn()
const sendWebAppEMailLoginCodeMock = vi.fn()
const searchParams = new URLSearchParams({
redirect_url: encodeURIComponent('/chatbot/test-app'),
})
vi.mock('@/next/navigation', () => ({
useRouter: () => ({ push: pushMock, replace: replaceMock }),
useSearchParams: () => searchParams,
}))
vi.mock('@/context/i18n', () => ({
useLocale: () => 'en-US',
}))
vi.mock('@/app/components/signin/storage', () => ({
COUNT_DOWN_TIME_MS: 60_000,
useSetCountdownLeftTime: () => setCountdownLeftTimeMock,
}))
vi.mock('@/service/common', () => ({
sendWebAppEMailLoginCode: (...args: unknown[]) => sendWebAppEMailLoginCodeMock(...args),
}))
describe('MailAndCodeAuth', () => {
beforeEach(() => {
vi.clearAllMocks()
sendWebAppEMailLoginCodeMock.mockResolvedValue({ result: 'success', data: 'token-abc' })
})
it('submits from the email field through the native form', async () => {
const user = userEvent.setup()
render(<MailAndCodeAuth />)
const emailInput = screen.getByRole('textbox', { name: 'login.email' })
const submitButton = screen.getByRole('button', { name: 'login.signup.verifyMail' })
expect(emailInput).toHaveAttribute('name', 'email')
expect(emailInput).toHaveAttribute('autocomplete', 'email')
expect(emailInput).toHaveAttribute('spellcheck', 'false')
expect(submitButton).toHaveAttribute('type', 'submit')
await user.tab()
expect(emailInput).toHaveFocus()
await user.type(emailInput, 'user@example.com{Enter}')
await waitFor(() => {
expect(sendWebAppEMailLoginCodeMock).toHaveBeenCalledTimes(1)
})
expect(pushMock).toHaveBeenCalledWith(expect.stringContaining('/webapp-signin/check-code?'))
})
})

View File

@@ -1,10 +1,10 @@
import { Button } from '@langgenius/dify-ui/button'
import { Field, FieldControl, FieldLabel } from '@langgenius/dify-ui/field'
import { Form } from '@langgenius/dify-ui/form'
import { toast } from '@langgenius/dify-ui/toast'
import { noop } from 'es-toolkit/function'
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { resolveWebAppLoginRedirect } from '@/app/(shareLayout)/webapp-signin/login-redirect'
import Input from '@/app/components/base/input'
import { COUNT_DOWN_TIME_MS, useSetCountdownLeftTime } from '@/app/components/signin/storage'
import { emailRegex } from '@/config'
import { useLocale } from '@/context/i18n'
@@ -64,33 +64,37 @@ export default function MailAndCodeAuth() {
}
return (
<form onSubmit={noop}>
<input type="text" className="hidden" />
<div className="mb-2">
<label htmlFor="email" className="my-2 system-md-semibold text-text-secondary">
<Form
onFormSubmit={() => {
void handleGetEMailVerificationCode()
}}
>
<Field name="email" className="mb-2 block">
<FieldLabel className="my-2 py-0 system-md-semibold! text-text-secondary">
{t(($) => $.email, { ns: 'login' })}
</label>
</FieldLabel>
<div className="mt-1">
<Input
id="email"
<FieldControl
type="email"
autoComplete="email"
spellCheck={false}
value={email}
placeholder={t(($) => $.emailPlaceholder, { ns: 'login' }) as string}
onChange={(e) => setEmail(e.target.value)}
onValueChange={setEmail}
/>
</div>
<div className="mt-3">
<Button
type="submit"
loading={loading}
disabled={loading || !email}
variant="primary"
className="w-full"
onClick={handleGetEMailVerificationCode}
>
{t(($) => $['signup.verifyMail'], { ns: 'login' })}
</Button>
</div>
</div>
</form>
</Field>
</Form>
)
}

View File

@@ -22,6 +22,15 @@ vi.mock('@/app/components/workflow/store/workflow/use-nodes', () => ({
default: () => mockUseNodes(),
}))
vi.mock('@/app/components/base/action-button', () => ({
__esModule: true,
default: ({ children, onClick }: { children?: ReactNode; onClick?: () => void }) => (
<button type="button" aria-label="close-preview" onClick={onClick}>
{children}
</button>
),
}))
vi.mock('@/app/components/base/badge', () => ({
__esModule: true,
default: ({ children }: { children?: ReactNode }) => <div data-testid="badge">{children}</div>,
@@ -131,7 +140,7 @@ describe('FormContentPreview', () => {
<FormContentPreview content="content" formInputs={[]} userActions={[]} onClose={onClose} />,
)
fireEvent.click(screen.getByRole('button', { name: 'operation.close' }))
fireEvent.click(screen.getByRole('button', { name: 'close-preview' }))
expect(onClose).toHaveBeenCalledTimes(1)
})

View File

@@ -1,4 +1,5 @@
'use client'
import type { FC } from 'react'
import type { FormInputItem, UserAction } from '../types'
import { Button } from '@langgenius/dify-ui/button'
import * as React from 'react'
@@ -21,12 +22,12 @@ type FormContentPreviewProps = {
onClose: () => void
}
const FormContentPreview = ({
const FormContentPreview: FC<FormContentPreviewProps> = ({
content,
formInputs,
userActions,
onClose,
}: FormContentPreviewProps) => {
}) => {
const { t } = useTranslation()
const panelWidth = useStore((state) => state.panelWidth)
const nodes = useNodes()
@@ -68,11 +69,8 @@ const FormContentPreview = ({
<Badge uppercase className="border-text-accent-secondary text-text-accent-secondary">
{t(($) => $[`${i18nPrefix}.formContent.preview`], { ns: 'workflow' })}
</Badge>
<ActionButton
aria-label={t(($) => $['operation.close'], { ns: 'common' })}
onClick={onClose}
>
<span aria-hidden className="i-ri-close-line size-5 text-text-tertiary" />
<ActionButton onClick={onClose}>
<span className="i-ri-close-line size-5 text-text-tertiary" />
</ActionButton>
</div>
<div className="max-h-[calc(100vh-167px)] overflow-y-auto px-4">