@authing/react-ui-components 3.0.2-beta.9 → 3.1.1-rc.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/.eslintignore +6 -0
  2. package/.eslintrc.js +7 -0
  3. package/.prettierrc +5 -0
  4. package/.vscode/settings.json +31 -0
  5. package/LICENSE +21 -0
  6. package/config/antdReplacer.js +51 -0
  7. package/config/env.js +106 -0
  8. package/config/getHttpsConfig.js +66 -0
  9. package/config/jest/cssTransform.js +14 -0
  10. package/config/jest/fileTransform.js +40 -0
  11. package/config/modules.js +134 -0
  12. package/config/paths.js +71 -0
  13. package/config/pnpTs.js +35 -0
  14. package/config/webpack.config.js +857 -0
  15. package/config/webpackDevServer.config.js +130 -0
  16. package/lib/index.d.ts +211 -1669
  17. package/lib/index.min.css +1 -2
  18. package/lib/index.min.js +1 -1
  19. package/lib/index.min.js.LICENSE.txt +0 -32
  20. package/package.json +18 -42
  21. package/public/favicon.ico +0 -0
  22. package/public/index.html +43 -0
  23. package/public/logo192.png +0 -0
  24. package/public/logo512.png +0 -0
  25. package/public/manifest.json +25 -0
  26. package/public/robots.txt +3 -0
  27. package/scripts/build.js +212 -0
  28. package/scripts/lib.js +200 -0
  29. package/scripts/start.js +166 -0
  30. package/scripts/test.js +53 -0
  31. package/src/common/AuthingDropdown/index.tsx +52 -0
  32. package/src/common/AuthingDropdown/style.less +43 -0
  33. package/src/common/AuthingTabs/index.tsx +98 -0
  34. package/src/common/AuthingTabs/style.less +135 -0
  35. package/src/common/CopyAbleText/index.tsx +54 -0
  36. package/src/common/CopyAbleText/style.less +13 -0
  37. package/src/common/VerifyCodeInput/index.tsx +76 -0
  38. package/src/common/VerifyCodeInput/style.less +24 -0
  39. package/src/components/AuthingGuard/AppMFALayout/index.tsx +74 -0
  40. package/src/components/AuthingGuard/AppMFALayout/style.less +12 -0
  41. package/src/components/AuthingGuard/CompleteUserInfoLayout/index.tsx +29 -0
  42. package/src/components/AuthingGuard/CompleteUserInfoLayout/style.less +8 -0
  43. package/src/components/AuthingGuard/Forms/ADLoginForm/index.tsx +117 -0
  44. package/src/components/AuthingGuard/Forms/Agreements/index.tsx +81 -0
  45. package/src/components/AuthingGuard/Forms/Agreements/style.less +44 -0
  46. package/src/components/AuthingGuard/Forms/CompleteUserInfoForm/index.tsx +139 -0
  47. package/src/components/AuthingGuard/Forms/EmailMfaVerifyForm/CheckEmailForm.tsx +86 -0
  48. package/src/components/AuthingGuard/Forms/EmailMfaVerifyForm/VerifyCodeForm.tsx +116 -0
  49. package/src/components/AuthingGuard/Forms/EmailMfaVerifyForm/index.tsx +40 -0
  50. package/src/components/AuthingGuard/Forms/EmailMfaVerifyForm/style.less +0 -0
  51. package/src/components/AuthingGuard/Forms/EmailRegisterForm/index.tsx +181 -0
  52. package/src/components/AuthingGuard/Forms/EmailRegisterForm/style.less +0 -0
  53. package/src/components/AuthingGuard/Forms/LdapLoginForm/index.tsx +161 -0
  54. package/src/components/AuthingGuard/Forms/LdapLoginForm/style.less +0 -0
  55. package/src/components/AuthingGuard/Forms/LoginFormFooter/index.tsx +76 -0
  56. package/src/components/AuthingGuard/Forms/LoginFormFooter/style.less +6 -0
  57. package/src/components/AuthingGuard/Forms/MfaResetCodeForm/Step1.tsx +86 -0
  58. package/src/components/AuthingGuard/Forms/MfaResetCodeForm/Step2.tsx +53 -0
  59. package/src/components/AuthingGuard/Forms/MfaResetCodeForm/index.tsx +65 -0
  60. package/src/components/AuthingGuard/Forms/MfaResetCodeForm/style.less +20 -0
  61. package/src/components/AuthingGuard/Forms/MfaVerifyForm/index.tsx +105 -0
  62. package/src/components/AuthingGuard/Forms/MfaVerifyForm/style.less +12 -0
  63. package/src/components/AuthingGuard/Forms/PasswordLoginForm/index.tsx +261 -0
  64. package/src/components/AuthingGuard/Forms/PhoneCodeLoginForm/index.tsx +133 -0
  65. package/src/components/AuthingGuard/Forms/PhoneRegisterForm/index.tsx +202 -0
  66. package/src/components/AuthingGuard/Forms/QrCodeLoginForm/index.tsx +61 -0
  67. package/src/components/AuthingGuard/Forms/QrCodeLoginForm/style.less +14 -0
  68. package/src/components/AuthingGuard/Forms/RegisterFormFooter/index.tsx +56 -0
  69. package/src/components/AuthingGuard/Forms/RegisterFormFooter/style.less +0 -0
  70. package/src/components/AuthingGuard/Forms/ResetPwdForm/Footer.tsx +21 -0
  71. package/src/components/AuthingGuard/Forms/ResetPwdForm/Step1.tsx +86 -0
  72. package/src/components/AuthingGuard/Forms/ResetPwdForm/Step2.tsx +127 -0
  73. package/src/components/AuthingGuard/Forms/ResetPwdForm/Step3.tsx +141 -0
  74. package/src/components/AuthingGuard/Forms/ResetPwdForm/Step4.tsx +51 -0
  75. package/src/components/AuthingGuard/Forms/ResetPwdForm/index.tsx +96 -0
  76. package/src/components/AuthingGuard/Forms/ResetPwdForm/style.less +3 -0
  77. package/src/components/AuthingGuard/Forms/SendPhoneCode/SendCodeBtn.tsx +88 -0
  78. package/src/components/AuthingGuard/Forms/SendPhoneCode/index.tsx +50 -0
  79. package/src/components/AuthingGuard/Forms/SendPhoneCode/style.less +26 -0
  80. package/src/components/AuthingGuard/Forms/SmsMfaVerifyForm/CheckPhoneForm.tsx +86 -0
  81. package/src/components/AuthingGuard/Forms/SmsMfaVerifyForm/VerifyCodeForm.tsx +113 -0
  82. package/src/components/AuthingGuard/Forms/SmsMfaVerifyForm/index.tsx +40 -0
  83. package/src/components/AuthingGuard/Forms/SmsMfaVerifyForm/style.less +3 -0
  84. package/src/components/AuthingGuard/Forms/SocialAndIdpLogin/index.tsx +325 -0
  85. package/src/components/AuthingGuard/Forms/SocialAndIdpLogin/style.less +75 -0
  86. package/src/components/AuthingGuard/Forms/UploadImage/index.tsx +70 -0
  87. package/src/components/AuthingGuard/Forms/index.ts +13 -0
  88. package/src/components/AuthingGuard/GuardLayout/index.tsx +488 -0
  89. package/src/components/AuthingGuard/GuardLayout/style.less +111 -0
  90. package/src/components/AuthingGuard/Header/index.tsx +28 -0
  91. package/src/components/AuthingGuard/Header/style.less +71 -0
  92. package/src/components/AuthingGuard/IconFont/iconfont.js +74 -0
  93. package/src/components/AuthingGuard/IconFont/index.tsx +19 -0
  94. package/src/components/AuthingGuard/IconFont/style.less +6 -0
  95. package/src/components/AuthingGuard/IconFont/svg.js +2 -0
  96. package/src/components/AuthingGuard/LoginLayout/index.tsx +205 -0
  97. package/src/components/AuthingGuard/LoginLayout/style.less +0 -0
  98. package/src/components/AuthingGuard/MfaLayout/Steps.ts +4 -0
  99. package/src/components/AuthingGuard/MfaLayout/index.tsx +49 -0
  100. package/src/components/AuthingGuard/MfaLayout/style.less +3 -0
  101. package/src/components/AuthingGuard/RegisterLayout/index.tsx +89 -0
  102. package/src/components/AuthingGuard/RegisterLayout/style.less +0 -0
  103. package/src/components/AuthingGuard/ResetPwdLayout/index.tsx +20 -0
  104. package/src/components/AuthingGuard/ToggleLang/index.tsx +51 -0
  105. package/src/components/AuthingGuard/api/appConfig.ts +154 -0
  106. package/src/components/AuthingGuard/api/http.ts +88 -0
  107. package/src/components/AuthingGuard/api/index.ts +3 -0
  108. package/src/components/AuthingGuard/api/sso.ts +29 -0
  109. package/src/components/AuthingGuard/api/userPoolConfig.ts +112 -0
  110. package/src/components/AuthingGuard/constants.ts +107 -0
  111. package/src/components/AuthingGuard/hooks/index.tsx +70 -0
  112. package/src/components/AuthingGuard/hooks/useScreenSize.tsx +68 -0
  113. package/src/components/AuthingGuard/index.tsx +134 -0
  114. package/src/components/AuthingGuard/locales/en/common.json +185 -0
  115. package/src/components/AuthingGuard/locales/en/index.ts +6 -0
  116. package/src/components/AuthingGuard/locales/en/login.json +94 -0
  117. package/src/components/AuthingGuard/locales/en/map.json +4 -0
  118. package/src/components/AuthingGuard/locales/en/user.json +81 -0
  119. package/src/components/AuthingGuard/locales/index.ts +45 -0
  120. package/src/components/AuthingGuard/locales/zh/common.json +185 -0
  121. package/src/components/AuthingGuard/locales/zh/index.ts +6 -0
  122. package/src/components/AuthingGuard/locales/zh/login.json +94 -0
  123. package/src/components/AuthingGuard/locales/zh/map.json +4 -0
  124. package/src/components/AuthingGuard/locales/zh/user.json +81 -0
  125. package/src/components/AuthingGuard/style.less +108 -0
  126. package/src/components/AuthingGuard/types/Forms.ts +95 -0
  127. package/src/components/AuthingGuard/types/GuardConfig.ts +424 -0
  128. package/src/components/AuthingGuard/types/GuardState.ts +7 -0
  129. package/src/components/AuthingGuard/types/Locales.ts +12 -0
  130. package/src/components/AuthingGuard/types/index.ts +4 -0
  131. package/src/components/index.ts +7 -0
  132. package/src/context/base.tsx +28 -0
  133. package/src/context/global/context.tsx +39 -0
  134. package/src/context/global/reducer.tsx +56 -0
  135. package/src/index.tsx +142 -0
  136. package/src/logo.svg +7 -0
  137. package/src/react-app-env.d.ts +71 -0
  138. package/src/reportWebVitals.ts +15 -0
  139. package/src/setupTests.ts +5 -0
  140. package/src/utils/clipboard.ts +27 -0
  141. package/src/utils/index.ts +180 -0
  142. package/src/utils/popupCenter.ts +48 -0
  143. package/tsconfig.json +24 -0
  144. package/lib/static/media/loading.4a67a5f3.svg +0 -29
@@ -0,0 +1,53 @@
1
+ import React, { FC } from 'react'
2
+
3
+ import { Button, Checkbox, Form } from 'antd'
4
+ import { CopyAbleText } from '../../../../common/CopyAbleText'
5
+ import { useTranslation } from 'react-i18next'
6
+
7
+ export interface MfaResetStep2Props {
8
+ recoverCode: string
9
+ onFinish: () => void
10
+ }
11
+
12
+ export const MfaResetStep2: FC<MfaResetStep2Props> = ({
13
+ recoverCode,
14
+ onFinish,
15
+ }) => {
16
+ const { t } = useTranslation()
17
+ return (
18
+ <Form onFinish={onFinish}>
19
+ <CopyAbleText className="authing-guard-secret-block">
20
+ {recoverCode}
21
+ </CopyAbleText>
22
+ <Form.Item
23
+ valuePropName="checked"
24
+ rules={[
25
+ {
26
+ validateTrigger: [],
27
+ validator(r, v) {
28
+ return new Promise((resolve, reject) => {
29
+ if (!v) {
30
+ reject(t('login.confirmSavedCode'))
31
+ }
32
+ resolve(true)
33
+ })
34
+ },
35
+ },
36
+ ]}
37
+ name="MfaCopyRecoverCode"
38
+ >
39
+ <Checkbox>{t('login.rememberedSecret')}</Checkbox>
40
+ </Form.Item>
41
+
42
+ <Button
43
+ className="authing-guard-mfa-confirm-btn"
44
+ htmlType="submit"
45
+ block
46
+ type="primary"
47
+ size="large"
48
+ >
49
+ {t('common.sure')}
50
+ </Button>
51
+ </Form>
52
+ )
53
+ }
@@ -0,0 +1,65 @@
1
+ import { User } from 'authing-js-sdk'
2
+ import React, { FC, useState } from 'react'
3
+
4
+ import { MfaResetStep1 } from './Step1'
5
+ import { MfaResetStep2 } from './Step2'
6
+ import { useGuardContext } from '../../../../context/global/context'
7
+ import { MfaResetForm } from '../../../../components/AuthingGuard/types'
8
+
9
+ import './style.less'
10
+ import { useTranslation } from 'react-i18next'
11
+
12
+ export const MfaResetCodeForm: FC<MfaResetForm> = ({
13
+ goVerify,
14
+ onSuccess,
15
+ onFail,
16
+ }) => {
17
+ const { t } = useTranslation()
18
+
19
+ const {
20
+ state: {
21
+ mfaData: { mfaToken },
22
+ },
23
+ } = useGuardContext()
24
+
25
+ const [step, setStep] = useState(1)
26
+ const [user, setUser] = useState<User | null>(null)
27
+ const [recoverCode, setRecoverCode] = useState('')
28
+
29
+ const onReset = (
30
+ user: User & {
31
+ recoveryCode: string
32
+ }
33
+ ) => {
34
+ setStep(2)
35
+ setUser(user)
36
+ setRecoverCode(user.recoveryCode)
37
+ }
38
+
39
+ const stepMap: Record<number, JSX.Element | undefined> = {
40
+ 1: (
41
+ <MfaResetStep1
42
+ onFail={onFail}
43
+ goVerify={goVerify}
44
+ onSuccess={onReset}
45
+ mfaToken={mfaToken}
46
+ />
47
+ ),
48
+ 2: (
49
+ <MfaResetStep2
50
+ onFinish={() => onSuccess?.(user!)}
51
+ recoverCode={recoverCode}
52
+ />
53
+ ),
54
+ }
55
+
56
+ return (
57
+ <>
58
+ <h2 className="authing-guard-mfa-reset-title">
59
+ {t('common.useRecoverCode')}
60
+ </h2>
61
+ <p className="authing-guard-mfa-reset-tips">{t('login.mfaAfterReset')}</p>
62
+ {stepMap[step]}
63
+ </>
64
+ )
65
+ }
@@ -0,0 +1,20 @@
1
+ .authing-guard-secret-block {
2
+ border: 1px solid #ddd;
3
+ justify-content: center;
4
+ border-radius: 2px;
5
+ padding: 12px 0;
6
+ text-align: center;
7
+ }
8
+
9
+ .authing-guard-mfa-reset-title {
10
+ color: #181818;
11
+ font-size: 18px;
12
+ font-weight: 600;
13
+ text-align: center;
14
+ margin: 0;
15
+ }
16
+
17
+ .authing-guard-mfa-reset-tips {
18
+ margin: 38px 0;
19
+ text-align: center;
20
+ }
@@ -0,0 +1,105 @@
1
+ import { User } from 'authing-js-sdk'
2
+ import React, { FC, useState } from 'react'
3
+ import { Button, Form } from 'antd'
4
+
5
+ import { VerifyCodeInput } from '../../../../common/VerifyCodeInput'
6
+ import { useGuardContext } from '../../../../context/global/context'
7
+ import { MfaVerifyForm } from '../../../../components/AuthingGuard/types'
8
+
9
+ import './style.less'
10
+ import { useTranslation } from 'react-i18next'
11
+
12
+ const CODE_LEN = 6
13
+
14
+ export const MFAVerifyForm: FC<MfaVerifyForm> = ({
15
+ onSuccess,
16
+ onFail,
17
+ goReset,
18
+ }) => {
19
+ const {
20
+ state: {
21
+ authClient,
22
+ mfaData: { mfaToken },
23
+ },
24
+ } = useGuardContext()
25
+
26
+ const { t } = useTranslation()
27
+
28
+ const [rawForm] = Form.useForm()
29
+
30
+ const [MfaCode, setMFACode] = useState(new Array(CODE_LEN).fill(''))
31
+ const [loading, setLoading] = useState(false)
32
+
33
+ const onFinish = async (values: any) => {
34
+ try {
35
+ const user: User = await authClient.mfa.verifyTotpMfa({
36
+ mfaToken,
37
+ totp: MfaCode.join(''),
38
+ })
39
+ onSuccess && onSuccess(user)
40
+ } catch (e) {
41
+ onFail?.(e)
42
+ } finally {
43
+ setLoading(false)
44
+ }
45
+ }
46
+
47
+ return (
48
+ <>
49
+ <h3 className="authing-guard-mfa-title">
50
+ {t('login.accPwdLoginVerify')}
51
+ </h3>
52
+ <p className="authing-guard-mfa-tips">{t('login.inputSixCode')}</p>
53
+ <Form
54
+ form={rawForm}
55
+ onSubmitCapture={() => setLoading(true)}
56
+ onFinish={onFinish}
57
+ onFinishFailed={() => setLoading(false)}
58
+ >
59
+ <Form.Item
60
+ name="mfaCode"
61
+ rules={[
62
+ {
63
+ validateTrigger: [],
64
+ validator() {
65
+ if (MfaCode.some((item) => !item)) {
66
+ return Promise.reject(t('login.inputFullMfaCode'))
67
+ }
68
+ return Promise.resolve()
69
+ },
70
+ },
71
+ ]}
72
+ >
73
+ <VerifyCodeInput verifyCode={MfaCode} setVerifyCode={setMFACode} />
74
+ </Form.Item>
75
+
76
+ <Button
77
+ className="authing-guard-mfa-confirm-btn"
78
+ loading={loading}
79
+ block
80
+ htmlType="submit"
81
+ type="primary"
82
+ size="large"
83
+ >
84
+ {t('common.sure')}
85
+ </Button>
86
+
87
+ <div className="authing-guard-form-actions">
88
+ <div className="authing-guard-tip-btn-comb">
89
+ <span className="authing-guard-tip">
90
+ {t('common.hasLooseSaftyCode')}
91
+ </span>
92
+ <Button
93
+ htmlType="button"
94
+ onClick={goReset}
95
+ className="authing-guard-text-btn"
96
+ type="text"
97
+ >
98
+ {t('common.useRecoverCode')}
99
+ </Button>
100
+ </div>
101
+ </div>
102
+ </Form>
103
+ </>
104
+ )
105
+ }
@@ -0,0 +1,12 @@
1
+ .authing-guard-mfa-title {
2
+ color: #181818;
3
+ font-size: 18px;
4
+ font-weight: 600;
5
+ text-align: center;
6
+ margin: 0;
7
+ }
8
+
9
+ .authing-guard-mfa-tips {
10
+ margin: 38px 0;
11
+ text-align: center;
12
+ }
@@ -0,0 +1,261 @@
1
+ import { Input, Form, Alert, message as Message, message } from 'antd'
2
+ import { FormInstance, Rule } from 'antd/lib/form'
3
+ import { UserOutlined, LockOutlined } from '@ant-design/icons'
4
+ import React, {
5
+ forwardRef,
6
+ useImperativeHandle,
7
+ useMemo,
8
+ useState,
9
+ } from 'react'
10
+
11
+ import { getRequiredRules, getUserRegisterParams } from '../../../../utils'
12
+ import { useGuardContext } from '../../../../context/global/context'
13
+ import { NEED_CAPTCHA } from '../../../../components/AuthingGuard/constants'
14
+ import {
15
+ LoginMethods,
16
+ PasswordLoginFormProps,
17
+ User,
18
+ } from '../../../../components/AuthingGuard/types'
19
+ import { LoginFormFooter } from '../../../../components/AuthingGuard/Forms/LoginFormFooter'
20
+ import { useTranslation } from 'react-i18next'
21
+ import { PasswordLoginMethods } from '../../api'
22
+ import { requestClient } from '../../api/http'
23
+
24
+ export const PasswordLoginForm = forwardRef<
25
+ FormInstance,
26
+ PasswordLoginFormProps
27
+ >(({ onSuccess, onValidateFail, onFail }, ref) => {
28
+ const { state, getValue } = useGuardContext()
29
+ const { t } = useTranslation()
30
+
31
+ const { config, authClient, realHost, userPoolId, appId, guardEvents } = state
32
+ const autoRegister = config.autoRegister
33
+
34
+ const captchaUrl = `${realHost}/api/v2/security/captcha`
35
+ const getCaptchaUrl = () => `${captchaUrl}?r=${+new Date()}`
36
+
37
+ const [rawForm] = Form.useForm()
38
+
39
+ const [needCaptcha, setNeedCaptcha] = useState(false)
40
+ const [verifyCodeUrl, setVerifyCodeUrl] = useState<string | null>(null)
41
+ const [loading, setLoading] = useState(false)
42
+
43
+ const onFinishFailed = (errorInfo: any) => {
44
+ setLoading(false)
45
+ onValidateFail && onValidateFail(errorInfo)
46
+ }
47
+
48
+ const loginMethodsText = useMemo<
49
+ Record<
50
+ PasswordLoginMethods,
51
+ {
52
+ t: string
53
+ sort: number
54
+ }
55
+ >
56
+ >(
57
+ () => ({
58
+ 'email-password': {
59
+ t: t('common.email'),
60
+ sort: 2,
61
+ },
62
+ 'phone-password': {
63
+ t: t('common.phoneNumber'),
64
+ sort: 1,
65
+ },
66
+ 'username-password': {
67
+ t: t('common.username'),
68
+ sort: 0,
69
+ },
70
+ }),
71
+ [t]
72
+ )
73
+
74
+ const usernamePlaceholder = useMemo(
75
+ () =>
76
+ t('login.inputAccount', {
77
+ text: config.passwordLoginMethods
78
+ ?.map((item) => loginMethodsText[item])
79
+ .sort((a, b) => a.sort - b.sort)
80
+ .map((item) => item.t)
81
+ .join(' / '),
82
+ }),
83
+ [config.passwordLoginMethods, loginMethodsText, t]
84
+ )
85
+
86
+ const identityRules = useMemo(() => {
87
+ const rules: Rule[] = getRequiredRules(t('common.accNotNull'))
88
+
89
+ const loginMethods = config.passwordLoginMethods
90
+
91
+ if (
92
+ loginMethods?.includes('email-password') &&
93
+ loginMethods?.length === 1
94
+ ) {
95
+ rules.push({
96
+ type: 'email',
97
+ message: t('common.emailFormatError'),
98
+ })
99
+ }
100
+
101
+ return rules
102
+ }, [config, t])
103
+
104
+ const login = async (values: any) => {
105
+ const identity = values.identity && values.identity.trim()
106
+ const password = values.password && values.password.trim()
107
+ const captchaCode = values.captchaCode && values.captchaCode.trim()
108
+
109
+ const encrypt = authClient.options.encryptFunction
110
+
111
+ const { publicKey, autoRegister } = getValue('config')
112
+ const { code, data, message } = await requestClient.post<User>(
113
+ '/api/v2/login/account',
114
+ {
115
+ account: identity,
116
+ password: await encrypt!(password, publicKey),
117
+ captchaCode,
118
+ customData: getUserRegisterParams(),
119
+ autoRegister: autoRegister,
120
+ },
121
+ {
122
+ headers: {
123
+ 'x-authing-userpool-id': userPoolId,
124
+ 'x-authing-app-id': appId,
125
+ },
126
+ }
127
+ )
128
+
129
+ if (code === 200) {
130
+ return data
131
+ } else {
132
+ Message.error(message)
133
+ // eslint-disable-next-line no-throw-literal
134
+ throw {
135
+ code,
136
+ message,
137
+ data,
138
+ }
139
+ }
140
+ }
141
+
142
+ const onFinish = async (values: any) => {
143
+ if (guardEvents.onBeforeLogin) {
144
+ try {
145
+ const canLogin = await guardEvents.onBeforeLogin(
146
+ {
147
+ type: LoginMethods.Password,
148
+ data: {
149
+ identity: values.identity,
150
+ password: values.password,
151
+ captchaCode: values.captchaCode,
152
+ },
153
+ },
154
+ authClient
155
+ )
156
+
157
+ if (!canLogin) {
158
+ setLoading(false)
159
+ return
160
+ }
161
+ } catch (e) {
162
+ if (typeof e === 'string') {
163
+ message.error(e)
164
+ } else {
165
+ message.error(e.message)
166
+ }
167
+ setLoading(false)
168
+ return
169
+ }
170
+ }
171
+
172
+ try {
173
+ let user: User | undefined
174
+ user = await login(values)
175
+ user && onSuccess && onSuccess(user)
176
+ } catch (error) {
177
+ if (error.code === NEED_CAPTCHA && verifyCodeUrl === null) {
178
+ setNeedCaptcha(true)
179
+ setVerifyCodeUrl(getCaptchaUrl())
180
+ }
181
+
182
+ onFail && onFail(error)
183
+ } finally {
184
+ setLoading(false)
185
+ }
186
+ }
187
+
188
+ useImperativeHandle(ref, () => rawForm)
189
+
190
+ const formItems = [
191
+ {
192
+ component: (
193
+ <Input
194
+ autoComplete="email,username,tel"
195
+ size="large"
196
+ placeholder={usernamePlaceholder}
197
+ prefix={<UserOutlined style={{ color: '#ddd' }} />}
198
+ />
199
+ ),
200
+ name: 'identity',
201
+ rules: identityRules,
202
+ },
203
+ {
204
+ component: (
205
+ <Input.Password
206
+ size="large"
207
+ placeholder={t('login.inputLoginPwd')}
208
+ prefix={<LockOutlined style={{ color: '#ddd' }} />}
209
+ />
210
+ ),
211
+ name: 'password',
212
+ rules: getRequiredRules(t('common.passwordNotNull')),
213
+ },
214
+ {
215
+ component: (
216
+ <Input
217
+ size="large"
218
+ placeholder={t('login.inputCaptchaCode')}
219
+ addonAfter={
220
+ <img
221
+ src={verifyCodeUrl ?? ''}
222
+ alt={t('login.captchaCode')}
223
+ style={{ height: '2em', cursor: 'pointer' }}
224
+ onClick={() => setVerifyCodeUrl(getCaptchaUrl())}
225
+ />
226
+ }
227
+ />
228
+ ),
229
+ name: 'captchaCode',
230
+ rules: getRequiredRules(t('common.captchaCodeNotNull')),
231
+ hide: !needCaptcha,
232
+ },
233
+ ]
234
+
235
+ return (
236
+ <Form
237
+ form={rawForm}
238
+ onSubmitCapture={() => setLoading(true)}
239
+ onFinishFailed={onFinishFailed}
240
+ onFinish={onFinish}
241
+ >
242
+ {autoRegister && (
243
+ <Alert message={t('login.autoRegister')} style={{ marginBottom: 24 }} />
244
+ )}
245
+ {formItems.map(
246
+ (item) =>
247
+ !item.hide && (
248
+ <Form.Item key={item.name} name={item.name} rules={item.rules}>
249
+ {item.component}
250
+ </Form.Item>
251
+ )
252
+ )}
253
+
254
+ <LoginFormFooter
255
+ needRegister
256
+ needRestPwd
257
+ loading={loading}
258
+ ></LoginFormFooter>
259
+ </Form>
260
+ )
261
+ })
@@ -0,0 +1,133 @@
1
+ import React, { forwardRef, useImperativeHandle, useState } from 'react'
2
+ import { FormInstance, Rule } from 'antd/lib/form'
3
+ import { Alert, Form, Input, message } from 'antd'
4
+ import { UserOutlined, SafetyOutlined } from '@ant-design/icons'
5
+
6
+ import {
7
+ getRequiredRules,
8
+ getUserRegisterParams,
9
+ VALIDATE_PATTERN,
10
+ } from '../../../../utils'
11
+ import { useGuardContext } from '../../../../context/global/context'
12
+ import {
13
+ LoginMethods,
14
+ PhoneCodeLoginFormProps,
15
+ } from '../../../../components/AuthingGuard/types'
16
+ import { SendPhoneCode } from '../../../../components/AuthingGuard/Forms/SendPhoneCode'
17
+ import { LoginFormFooter } from '../../../../components/AuthingGuard/Forms/LoginFormFooter'
18
+ import { useTranslation } from 'react-i18next'
19
+
20
+ export const PhoneCodeLoginForm = forwardRef<
21
+ FormInstance,
22
+ PhoneCodeLoginFormProps
23
+ >(({ onSuccess, onFail, onValidateFail }, ref) => {
24
+ const {
25
+ state: { authClient, config, guardEvents },
26
+ } = useGuardContext()
27
+ const { t } = useTranslation()
28
+
29
+ const rulesMap: Record<string, Rule[]> = {
30
+ phone: getRequiredRules(t('login.inputPhone')).concat({
31
+ pattern: VALIDATE_PATTERN.phone,
32
+ message: t('common.phoneFormateError'),
33
+ }),
34
+ code: getRequiredRules(t('common.inputVerifyCode')),
35
+ }
36
+
37
+ const [rawForm] = Form.useForm()
38
+
39
+ const [phone, setPhone] = useState('')
40
+ const [loading, setLoading] = useState(false)
41
+
42
+ const onFinishFailed = (errorInfo: any) => {
43
+ setLoading(false)
44
+ onValidateFail && onValidateFail(errorInfo)
45
+ }
46
+
47
+ const onFinish = async (values: any) => {
48
+ if (guardEvents.onBeforeLogin) {
49
+ try {
50
+ const canLogin = await guardEvents.onBeforeLogin(
51
+ {
52
+ type: LoginMethods.PhoneCode,
53
+ data: {
54
+ phone: values.phone,
55
+ code: values.code,
56
+ },
57
+ },
58
+ authClient
59
+ )
60
+
61
+ if (!canLogin) {
62
+ setLoading(false)
63
+ return
64
+ }
65
+ } catch (e) {
66
+ if (typeof e === 'string') {
67
+ message.error(e)
68
+ } else {
69
+ message.error(e.message)
70
+ }
71
+ setLoading(false)
72
+ return
73
+ }
74
+ }
75
+
76
+ try {
77
+ const { phone, code } = values
78
+ const user = await authClient.loginByPhoneCode(phone, code, {
79
+ params: getUserRegisterParams(),
80
+ })
81
+ onSuccess && onSuccess(user)
82
+ } catch (error) {
83
+ onFail && onFail(error)
84
+ } finally {
85
+ setLoading(false)
86
+ }
87
+ }
88
+
89
+ useImperativeHandle(ref, () => rawForm)
90
+
91
+ return (
92
+ <Form
93
+ form={rawForm}
94
+ onSubmitCapture={() => setLoading(true)}
95
+ onFinishFailed={onFinishFailed}
96
+ onFinish={onFinish}
97
+ >
98
+ {config.autoRegister && (
99
+ <Alert
100
+ message={t('login.phoneAutoRegister')}
101
+ style={{ marginBottom: 24 }}
102
+ />
103
+ )}
104
+ <Form.Item name="phone" rules={rulesMap.phone}>
105
+ <Input
106
+ autoComplete="tel"
107
+ onChange={(e) => {
108
+ setPhone(e.target.value)
109
+ }}
110
+ size="large"
111
+ placeholder={t('login.inputPhone')}
112
+ prefix={<UserOutlined style={{ color: '#ddd' }} />}
113
+ />
114
+ </Form.Item>
115
+ <Form.Item name="code" rules={rulesMap.code}>
116
+ <Input
117
+ size="large"
118
+ placeholder={t('common.inputFourVerifyCode', {
119
+ length: 4,
120
+ })}
121
+ prefix={<SafetyOutlined style={{ color: '#ddd' }} />}
122
+ suffix={<SendPhoneCode phone={phone} />}
123
+ />
124
+ </Form.Item>
125
+
126
+ <LoginFormFooter
127
+ needRestPwd
128
+ needRegister
129
+ loading={loading}
130
+ ></LoginFormFooter>
131
+ </Form>
132
+ )
133
+ })