@springmicro/auth 0.5.1 → 0.5.3

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 (143) hide show
  1. package/.eslintrc.cjs +18 -18
  2. package/README.md +60 -60
  3. package/dist/{Index-64ac63ff-Bb7oVTBw.js → Index-64ac63ff-CSMaEBDA.js} +1 -1
  4. package/dist/{Index-6c094240-Cre9O000.js → Index-6c094240-mSgidTC1.js} +1 -1
  5. package/dist/{Web3Auth-D-jzK5S2.js → Web3Auth-R27O2eCc.js} +10 -10
  6. package/dist/alphawallet-B7eovdf-.js +0 -0
  7. package/dist/apexwallet-ysP20G9D.js +0 -0
  8. package/dist/atoken-BBnNTzcz.js +0 -0
  9. package/dist/bifrostwallet-CNNBO92h.js +0 -0
  10. package/dist/binance-DvPgAzCw.js +0 -0
  11. package/dist/bitget-DZOUrwgy.js +0 -0
  12. package/dist/bitpie-C_uxmEzw.js +0 -0
  13. package/dist/bitski-DpzkHNkv.js +0 -0
  14. package/dist/blockwallet-BL69cqkA.js +0 -0
  15. package/dist/brave-DnRzMQYW.js +0 -0
  16. package/dist/ccip-c78d2f16-DLcRlwod.js +0 -0
  17. package/dist/coin98wallet-CkFdOvXt.js +0 -0
  18. package/dist/coinbase-CaGNx5if.js +0 -0
  19. package/dist/core-9rK8tWud.js +0 -0
  20. package/dist/defiwallet-0AsgJBSy.js +0 -0
  21. package/dist/detected-CX_JZaUk.js +0 -0
  22. package/dist/echooo-CmaetT_Z.js +0 -0
  23. package/dist/enkrypt-BibtwvK7.js +0 -0
  24. package/dist/exodus-BxzkDWWP.js +0 -0
  25. package/dist/fordefi-CvKyVwhI.js +0 -0
  26. package/dist/foxwallet-D7c_LDTf.js +0 -0
  27. package/dist/frame-CDfNmjUy.js +0 -0
  28. package/dist/frontier-DqKNSj2z.js +0 -0
  29. package/dist/{hashMessage-1XTFjS64.js → hashMessage-B7KFzddV.js} +1 -1
  30. package/dist/huobiwallet-CxSwqa5L.js +0 -0
  31. package/dist/hyperpay-nmHE3WqM.js +0 -0
  32. package/dist/icon-2Zqy7pqQ.js +0 -0
  33. package/dist/icon-CGWI1Ies.js +0 -0
  34. package/dist/icon-C_O4nprO.js +0 -0
  35. package/dist/icon-DbgmZCnU.js +0 -0
  36. package/dist/icon-Dbjb6jc5.js +0 -0
  37. package/dist/icon-cAd7RhSC.js +0 -0
  38. package/dist/imtoken-yENtOTS3.js +0 -0
  39. package/dist/index-021f6a62-Zz543EDB.js +0 -0
  40. package/dist/{index-BcBHeHDh.js → index-1HLXgKdW.js} +2 -2
  41. package/dist/index-40a497ea-Bn2mC0r7.js +0 -0
  42. package/dist/{index-Bt-ptciq.js → index-C8xLRoDS.js} +1 -1
  43. package/dist/{index-Dd6Cx4jI.js → index-ChYsa81u.js} +2 -2
  44. package/dist/{index-Bi-yyFKX.js → index-Cp0Pai1C.js} +1 -1
  45. package/dist/{index-BCIsepdu.js → index-D_yyUC2r.js} +1060 -1025
  46. package/dist/{index-m_M0OdCv.js → index-TsElTuix.js} +1 -1
  47. package/dist/{index-D23L6orl.js → index-ayOaYQgv.js} +4 -4
  48. package/dist/index.d.ts +8 -3
  49. package/dist/index.js +1 -1
  50. package/dist/index.umd.cjs +165 -165
  51. package/dist/infinitywallet-C8J4FUYw.js +0 -0
  52. package/dist/kayros-AIrUK1za.js +0 -0
  53. package/dist/keplr-CyLTca9B.js +0 -0
  54. package/dist/lif3wallet-C5D6r8pg.js +0 -0
  55. package/dist/liquality-nbCtErVq.js +0 -0
  56. package/dist/mathwallet-CWkivCXo.js +0 -0
  57. package/dist/meetone-kKmvI8md.js +0 -0
  58. package/dist/metamask-BN7yiDV1.js +0 -0
  59. package/dist/mykey-CQZ6UcLH.js +0 -0
  60. package/dist/native-FDN9oNjc.js +0 -0
  61. package/dist/{number-DcOvM-ZJ.js → number-D9Q-3_Cz.js} +1 -1
  62. package/dist/okxwallet-CJLVogh2.js +0 -0
  63. package/dist/oneInch-CciyZ4Pz.js +0 -0
  64. package/dist/onekey-Dal8kYjU.js +0 -0
  65. package/dist/opera-DnKg-TJU.js +0 -0
  66. package/dist/ownbit-BshJUVuW.js +0 -0
  67. package/dist/phantom-CJ8dIcov.js +0 -0
  68. package/dist/rabby-D4thTcd6.js +0 -0
  69. package/dist/rainbow-mXld6yWV.js +0 -0
  70. package/dist/roninwallet-ZyYrd-D1.js +0 -0
  71. package/dist/safeheron-Eg1Jb29V.js +0 -0
  72. package/dist/safepal-DVIKy94N.js +0 -0
  73. package/dist/sequence-BS2IbtDg.js +0 -0
  74. package/dist/stablewallet-jyB079Wb.js +0 -0
  75. package/dist/status-FrAvQjfn.js +0 -0
  76. package/dist/subwallet-DMvFqKyY.js +0 -0
  77. package/dist/talisman-Bp8zUXqB.js +0 -0
  78. package/dist/tallywallet-B4OS9nIr.js +0 -0
  79. package/dist/tokenary-C7jjcbQa.js +0 -0
  80. package/dist/tokenpocket-9ZRPmAFA.js +0 -0
  81. package/dist/tp-V2em5bdl.js +0 -0
  82. package/dist/transactionRequest-be6a8ea9-CUKWo5mx.js +0 -0
  83. package/dist/trust-SgHubMq7.js +0 -0
  84. package/dist/xdefi-COVIyGz4.js +0 -0
  85. package/dist/zeal-DxHbDqm0.js +0 -0
  86. package/dist/zerion-BfW0UElc.js +0 -0
  87. package/dist/zodiacpilot-CrId6F4w.js +0 -0
  88. package/index.html +13 -13
  89. package/package.json +2 -2
  90. package/src/auth/config.ts +91 -91
  91. package/src/components/SignUp.tsx +472 -442
  92. package/src/components/forms/AgreementModal.tsx +88 -88
  93. package/src/components/forms/CaptchaController.tsx +94 -94
  94. package/src/components/forms/CaptchaModal.jsx +171 -171
  95. package/src/components/forms/CaptchaWidget.jsx +146 -146
  96. package/src/components/forms/PasswordChecker.ts +88 -88
  97. package/src/components/forms/SendCodeInput.tsx +155 -149
  98. package/src/components/forms/SignUpFormItem.tsx +708 -704
  99. package/src/components/forms/util/application-api.ts +161 -157
  100. package/src/components/forms/util/auth-api.ts +198 -198
  101. package/src/components/forms/util/auth-util.tsx +346 -346
  102. package/src/components/forms/util/core.ts +493 -493
  103. package/src/components/forms/util/invitation-api.ts +132 -132
  104. package/src/components/forms/util/provider.tsx +603 -603
  105. package/src/components/forms/util/user-api.ts +311 -307
  106. package/src/components/provider/AdfsLoginButton.jsx +38 -38
  107. package/src/components/provider/AlipayLoginButton.jsx +38 -38
  108. package/src/components/provider/AppleLoginButton.jsx +38 -38
  109. package/src/components/provider/AzureADB2CLoginButton.jsx +38 -38
  110. package/src/components/provider/AzureADLoginButton.jsx +38 -38
  111. package/src/components/provider/BaiduLoginButton.jsx +38 -38
  112. package/src/components/provider/BilibiliLoginButton.jsx +37 -37
  113. package/src/components/provider/CasdoorLoginButton.jsx +38 -38
  114. package/src/components/provider/DingTalkLoginButton.jsx +37 -37
  115. package/src/components/provider/DouyinLoginButton.jsx +38 -38
  116. package/src/components/provider/FacebookLoginButton.jsx +37 -37
  117. package/src/components/provider/GitHubLoginButton.jsx +37 -37
  118. package/src/components/provider/GitLabLoginButton.jsx +38 -38
  119. package/src/components/provider/GiteeLoginButton.jsx +34 -34
  120. package/src/components/provider/GoogleLoginButton.jsx +68 -68
  121. package/src/components/provider/InfoflowLoginButton.jsx +38 -38
  122. package/src/components/provider/LarkLoginButton.jsx +38 -38
  123. package/src/components/provider/LinkedInLoginButton.jsx +37 -37
  124. package/src/components/provider/LoginButton.jsx +33 -33
  125. package/src/components/provider/OktaLoginButton.jsx +38 -38
  126. package/src/components/provider/Provider.jsx +3 -3
  127. package/src/components/provider/ProviderButton.jsx +327 -327
  128. package/src/components/provider/QqLoginButton.jsx +31 -31
  129. package/src/components/provider/SelfLoginButton.jsx +47 -47
  130. package/src/components/provider/SlackLoginButton.jsx +38 -38
  131. package/src/components/provider/SteamLoginButton.jsx +38 -38
  132. package/src/components/provider/WeComLoginButton.jsx +34 -34
  133. package/src/components/provider/Web3Auth.jsx +365 -365
  134. package/src/components/provider/WechatLoginButton.jsx +37 -37
  135. package/src/components/provider/WeiboLoginButton.jsx +34 -34
  136. package/src/components/util.tsx +174 -170
  137. package/src/i18n/en/signup.json +48 -48
  138. package/src/i18n/index.ts +17 -17
  139. package/src/index.tsx +8 -8
  140. package/src/vite-env.d.ts +1 -1
  141. package/tsconfig.json +23 -23
  142. package/tsconfig.node.json +10 -10
  143. package/vite.config.ts +33 -33
@@ -1,442 +1,472 @@
1
- import React from "react";
2
- import * as Setting from "./forms/util/core";
3
- import * as AuthBackend from "./forms/util/auth-api";
4
- import * as ApplicationBackend from "./forms/util/application-api";
5
- import * as AuthUtil from "./forms/util/auth-util";
6
- import * as InvitationBackend from "./forms/util/invitation-api";
7
- import * as UserBackend from "./forms/util/user-api";
8
- import type { Invitation } from "./forms/util/invitation-api";
9
- import type { Application } from "./forms/util/provider";
10
- import { renderFormItem } from "./forms/SignUpFormItem";
11
- import { I18nextProvider, useTranslation } from "react-i18next";
12
- import i18n from "../i18n";
13
- import {
14
- type SignupItem,
15
- getDefaultValues,
16
- getFormProps,
17
- getIcon,
18
- getZod,
19
- getPasswordVisibilityRecord,
20
- getKey,
21
- getType,
22
- requiresCode,
23
- } from "./util";
24
-
25
- export type SignUpFormState = {
26
- applicationName?: string | null;
27
- name?: string;
28
- password?: string;
29
- confirm?: string;
30
- username?: string;
31
- email: string;
32
- phone: string;
33
- emailOrPhoneMode?: string;
34
- countryCode: string;
35
- emailCode: string;
36
- phoneCode: string;
37
- validEmail: boolean;
38
- validPhone: boolean;
39
- region: string;
40
- isTermsOfUseVisible?: boolean;
41
- termsOfUseContent: string;
42
- invitationCode?: string;
43
- invitation?: Invitation;
44
- msg?: string;
45
- agreement?: boolean;
46
- };
47
-
48
- type SignUpFormProps = {
49
- application: Application | null;
50
- applicationName?: string;
51
-
52
- // onUpdateAccount: (account: Account) => void;
53
- onUpdateApplication: (application: Application | null) => void;
54
- };
55
- type Account = any;
56
-
57
- function SignUpForm(props: SignUpFormProps) {
58
- const { t, i18n } = useTranslation();
59
- const [state, setState] = React.useState<SignUpFormState>({
60
- applicationName: props.applicationName ?? null,
61
- email: "",
62
- phone: "",
63
- emailOrPhoneMode: "",
64
- countryCode: "",
65
- emailCode: "",
66
- phoneCode: "",
67
- validEmail: false,
68
- validPhone: false,
69
- region: "",
70
- isTermsOfUseVisible: false,
71
- termsOfUseContent: "",
72
- });
73
- const form = React.useRef();
74
-
75
- const getApplication = (applicationName?: string) => {
76
- if (applicationName === undefined) {
77
- return;
78
- }
79
-
80
- ApplicationBackend.getApplication("admin", applicationName).then(
81
- (res: any) => {
82
- if (res.status === "error") {
83
- Setting.showMessage("error", res.msg);
84
- return;
85
- }
86
-
87
- onUpdateApplication(res.data);
88
- }
89
- );
90
- };
91
- const getApplicationLogin = (oAuthParams: any) => {
92
- AuthBackend.getApplicationLogin(oAuthParams).then((res) => {
93
- if (res.status === "ok") {
94
- const application = res.data;
95
- onUpdateApplication(application);
96
- } else {
97
- onUpdateApplication(null);
98
- setState({
99
- ...state,
100
- msg: res.msg,
101
- });
102
- }
103
- });
104
- };
105
-
106
- const getApplicationObj = () => {
107
- return props.application;
108
- };
109
-
110
- const getInvitationCodeInfo = (
111
- invitationCode: string,
112
- application: string
113
- ) => {
114
- InvitationBackend.getInvitationCodeInfo(invitationCode, application).then(
115
- (res: any) => {
116
- if (res.status === "error") {
117
- Setting.showMessage("error", res.msg);
118
- return;
119
- }
120
- setState({ ...state, invitation: res.data });
121
- }
122
- );
123
- };
124
-
125
- // const onUpdateAccount = (account: Account) => {
126
- // props.onUpdateAccount(account);
127
- // };
128
-
129
- const onUpdateApplication = (application: Application | null) => {
130
- props.onUpdateApplication(application);
131
- };
132
-
133
- React.useEffect(() => {
134
- const oAuthParams = AuthUtil.getOAuthGetParameters();
135
- if (oAuthParams !== null) {
136
- const signinUrl = window.location.pathname.replace(
137
- "/signup/oauth/authorize",
138
- "/login/oauth/authorize"
139
- );
140
- sessionStorage.setItem("signinUrl", signinUrl + window.location.search);
141
- }
142
-
143
- if (getApplicationObj() === undefined) {
144
- if (state.applicationName !== null) {
145
- getApplication(state.applicationName);
146
-
147
- const sp = new URLSearchParams(window.location.search);
148
- if (sp.has("invitationCode")) {
149
- const invitationCode = sp.get("invitationCode") ?? "";
150
- setState({ ...state, invitationCode });
151
- if (invitationCode !== "") {
152
- getInvitationCodeInfo(
153
- invitationCode,
154
- "admin/" + state.applicationName
155
- );
156
- }
157
- }
158
- } else if (oAuthParams !== null) {
159
- getApplicationLogin(oAuthParams);
160
- } else {
161
- Setting.showMessage(
162
- "error",
163
- `Unknown application name: ${state.applicationName}`
164
- );
165
- onUpdateApplication(null);
166
- }
167
- }
168
- }, []);
169
-
170
- const application = getApplicationObj();
171
- if (application === undefined || application === null) {
172
- return null;
173
- }
174
- // return <div>SIGN UP</div>;
175
- return application.signupItems?.map((signupItem, idx) => {
176
- return (
177
- <div key={idx}>
178
- {renderFormItem(application, signupItem, state, setState, t)}
179
- </div>
180
- );
181
- });
182
- }
183
-
184
- type SignUpState = {
185
- application: Application | null;
186
- };
187
-
188
- type SignUpProps = {
189
- applicationName: string;
190
- casdoorApiRoot: string;
191
- springmicroApiRoot?: string;
192
- };
193
-
194
- export function SignUp({
195
- applicationName,
196
- casdoorApiRoot,
197
- springmicroApiRoot,
198
- }: SignUpProps) {
199
- Setting.ROOT_URLS.casdoor = casdoorApiRoot;
200
- if (springmicroApiRoot) {
201
- Setting.ROOT_URLS.springmicro = springmicroApiRoot;
202
- }
203
- const [state, setState] = React.useState<SignUpState>({ application: null });
204
-
205
- const getApplication = () => {
206
- ApplicationBackend.getApplication("admin", applicationName).then(
207
- (res: any) => {
208
- console.log(res);
209
- if (res.status === "error") {
210
- Setting.showMessage("error", res.msg);
211
- return;
212
- }
213
-
214
- setState({
215
- application: res.data,
216
- });
217
- }
218
- );
219
- };
220
-
221
- React.useEffect(() => {
222
- // console.log(i18n.t("signup.username"));
223
- getApplication();
224
- }, []);
225
-
226
- if (state.application === undefined) return <></>;
227
- return (
228
- <I18nextProvider i18n={i18n}>
229
- <SignUpForm
230
- application={state.application}
231
- onUpdateApplication={(application) => {
232
- setState({
233
- application: application,
234
- });
235
- }}
236
- />
237
- </I18nextProvider>
238
- );
239
- }
240
-
241
- interface SignUpContextType {
242
- application: Application;
243
- getDefaultValues: typeof getDefaultValues;
244
- getFormProps: typeof getFormProps;
245
- icon: (
246
- signupItem: SignupItem,
247
- Button: React.JSX.ElementType
248
- ) => React.JSX.Element | null;
249
- getZod: typeof getZod;
250
- getKey: typeof getKey;
251
- type: (signupItem: SignupItem) => string;
252
- sendCode: typeof UserBackend.sendCode;
253
- requiresCode: typeof requiresCode;
254
- getCaptcha: typeof UserBackend.getCaptcha;
255
- // updateApplication: (updates: Partial<Application>) => void;
256
- }
257
-
258
- const SignUpContext = React.createContext<SignUpContextType | undefined>(
259
- undefined
260
- );
261
-
262
- export function SignUpProvider({
263
- applicationName,
264
- casdoorApiRoot,
265
- springmicroApiRoot,
266
- children,
267
- }: React.PropsWithChildren<SignUpProps>) {
268
- const { t, i18n } = useTranslation();
269
- Setting.ROOT_URLS.casdoor = casdoorApiRoot;
270
- if (springmicroApiRoot) {
271
- Setting.ROOT_URLS.springmicro = springmicroApiRoot;
272
- }
273
- const [state, setState] = React.useState<
274
- SignUpState & Partial<SignUpFormState>
275
- >({ application: null });
276
- const [togglePasswordVisibility, setTogglePasswordVisibility] =
277
- React.useState<Record<string, boolean>>({});
278
-
279
- const getApplication = () => {
280
- ApplicationBackend.getApplication("admin", applicationName).then(
281
- (res: any) => {
282
- console.log(res);
283
- if (res.status === "error") {
284
- Setting.showMessage("error", res.msg);
285
- return;
286
- }
287
-
288
- setState({
289
- application: res.data,
290
- });
291
- }
292
- );
293
- };
294
-
295
- const onUpdateApplication = (application: Application) => {
296
- setState({
297
- application: application,
298
- });
299
- };
300
-
301
- const getInvitationCodeInfo = (
302
- invitationCode: string,
303
- application: string
304
- ) => {
305
- InvitationBackend.getInvitationCodeInfo(invitationCode, application).then(
306
- (res: any) => {
307
- if (res.status === "error") {
308
- Setting.showMessage("error", res.msg);
309
- return;
310
- }
311
- setState({ ...state, invitation: res.data });
312
- }
313
- );
314
- };
315
-
316
- const getApplicationLogin = (oAuthParams: any) => {
317
- AuthBackend.getApplicationLogin(oAuthParams).then((res) => {
318
- if (res.status === "ok") {
319
- const application = res.data;
320
- onUpdateApplication(application);
321
- } else {
322
- onUpdateApplication(null);
323
- setState({
324
- ...state,
325
- msg: res.msg,
326
- });
327
- }
328
- });
329
- };
330
-
331
- const icon = (item: SignupItem, Button: React.JSX.ElementType) => {
332
- return getIcon(
333
- item,
334
- {
335
- password: {
336
- state: togglePasswordVisibility,
337
- setState: setTogglePasswordVisibility,
338
- },
339
- },
340
- Button
341
- );
342
- };
343
-
344
- const type = (item: SignupItem) => {
345
- return getType(item, {
346
- hidePassword: togglePasswordVisibility,
347
- });
348
- };
349
-
350
- const sendCode = UserBackend.sendCode;
351
- const getCaptcha = UserBackend.getCaptcha;
352
-
353
- React.useEffect(() => {
354
- // console.log(i18n.t("signup.username"));
355
- getApplication();
356
- }, []);
357
-
358
- React.useEffect(() => {
359
- if (state.application) {
360
- setTogglePasswordVisibility(
361
- getPasswordVisibilityRecord(state.application.signupItems)
362
- );
363
- }
364
- }, [state.application]);
365
-
366
- React.useEffect(() => {
367
- const oAuthParams = AuthUtil.getOAuthGetParameters();
368
- if (oAuthParams !== null) {
369
- const signinUrl = window.location.pathname.replace(
370
- "/signup/oauth/authorize",
371
- "/login/oauth/authorize"
372
- );
373
- sessionStorage.setItem("signinUrl", signinUrl + window.location.search);
374
- }
375
-
376
- if (state.application === undefined) {
377
- if (state.applicationName !== null) {
378
- getApplication();
379
-
380
- const sp = new URLSearchParams(window.location.search);
381
- if (sp.has("invitationCode")) {
382
- const invitationCode = sp.get("invitationCode") ?? "";
383
- setState({ ...state, invitationCode });
384
- if (invitationCode !== "") {
385
- getInvitationCodeInfo(
386
- invitationCode,
387
- "admin/" + state.applicationName
388
- );
389
- }
390
- }
391
- } else if (oAuthParams !== null) {
392
- getApplicationLogin(oAuthParams);
393
- } else {
394
- Setting.showMessage(
395
- "error",
396
- `Unknown application name: ${state.applicationName}`
397
- );
398
- onUpdateApplication(null);
399
- }
400
- }
401
- }, []);
402
-
403
- // const application = getApplicationObj();
404
- if (state.application === undefined || state.application === null) {
405
- return null;
406
- }
407
- // return <div>SIGN UP</div>;
408
- // return state.application.signupItems?.map((signupItem, idx) => {
409
- // return (
410
- // <div key={idx}>
411
- // {renderFormItem(state.application, signupItem, state, setState, t)}
412
- // </div>
413
- // );
414
- // });
415
-
416
- return (
417
- <SignUpContext.Provider
418
- value={{
419
- application: state.application,
420
- getDefaultValues,
421
- getFormProps,
422
- icon,
423
- getZod,
424
- getKey,
425
- type,
426
- sendCode,
427
- requiresCode,
428
- getCaptcha,
429
- }}
430
- >
431
- {children}
432
- </SignUpContext.Provider>
433
- );
434
- }
435
-
436
- export const useSignUp = (): SignUpContextType => {
437
- const context = React.useContext(SignUpContext);
438
- if (!context) {
439
- throw new Error("useSignUp must be used within a SignUpProvider");
440
- }
441
- return context;
442
- };
1
+ import React from "react";
2
+ import * as Setting from "./forms/util/core";
3
+ import * as AuthBackend from "./forms/util/auth-api";
4
+ import * as ApplicationBackend from "./forms/util/application-api";
5
+ import * as AuthUtil from "./forms/util/auth-util";
6
+ import * as InvitationBackend from "./forms/util/invitation-api";
7
+ import * as UserBackend from "./forms/util/user-api";
8
+ import type { Invitation } from "./forms/util/invitation-api";
9
+ import type { Application } from "./forms/util/provider";
10
+ import { renderFormItem } from "./forms/SignUpFormItem";
11
+ import { I18nextProvider, useTranslation } from "react-i18next";
12
+ import i18n from "../i18n";
13
+ import {
14
+ type SignupItem,
15
+ getDefaultValues,
16
+ getFormProps,
17
+ getIcon,
18
+ getZod,
19
+ getPasswordVisibilityRecord,
20
+ getKey,
21
+ getType,
22
+ requiresCode,
23
+ isVisible,
24
+ } from "./util";
25
+
26
+ export type SignUpFormState = {
27
+ applicationName?: string | null;
28
+ name?: string;
29
+ password?: string;
30
+ confirm?: string;
31
+ username?: string;
32
+ email: string;
33
+ phone: string;
34
+ emailOrPhoneMode?: string;
35
+ countryCode: string;
36
+ emailCode: string;
37
+ phoneCode: string;
38
+ validEmail: boolean;
39
+ validPhone: boolean;
40
+ region: string;
41
+ isTermsOfUseVisible?: boolean;
42
+ termsOfUseContent: string;
43
+ invitationCode?: string;
44
+ invitation?: Invitation;
45
+ msg?: string;
46
+ agreement?: boolean;
47
+ };
48
+
49
+ type SignUpFormProps = {
50
+ application: Application | null;
51
+ applicationName?: string;
52
+ CLIENT_ID: string;
53
+ CLIENT_SECRET: string;
54
+
55
+ // onUpdateAccount: (account: Account) => void;
56
+ onUpdateApplication: (application: Application | null) => void;
57
+ };
58
+ type Account = any;
59
+
60
+ function SignUpForm(props: SignUpFormProps) {
61
+ const { t, i18n } = useTranslation();
62
+ const [state, setState] = React.useState<SignUpFormState>({
63
+ applicationName: props.applicationName ?? null,
64
+ email: "",
65
+ phone: "",
66
+ emailOrPhoneMode: "",
67
+ countryCode: "",
68
+ emailCode: "",
69
+ phoneCode: "",
70
+ validEmail: false,
71
+ validPhone: false,
72
+ region: "",
73
+ isTermsOfUseVisible: false,
74
+ termsOfUseContent: "",
75
+ });
76
+ const form = React.useRef();
77
+
78
+ const getApplication = (applicationName?: string) => {
79
+ if (applicationName === undefined) {
80
+ return;
81
+ }
82
+
83
+ ApplicationBackend.getApplication(
84
+ "admin",
85
+ applicationName,
86
+ props.CLIENT_ID,
87
+ props.CLIENT_SECRET
88
+ ).then((res: any) => {
89
+ if (res.status === "error") {
90
+ Setting.showMessage("error", res.msg);
91
+ return;
92
+ }
93
+
94
+ onUpdateApplication(res.data);
95
+ });
96
+ };
97
+ const getApplicationLogin = (oAuthParams: any) => {
98
+ AuthBackend.getApplicationLogin(oAuthParams).then((res) => {
99
+ if (res.status === "ok") {
100
+ const application = res.data;
101
+ onUpdateApplication(application);
102
+ } else {
103
+ onUpdateApplication(null);
104
+ setState({
105
+ ...state,
106
+ msg: res.msg,
107
+ });
108
+ }
109
+ });
110
+ };
111
+
112
+ const getApplicationObj = () => {
113
+ return props.application;
114
+ };
115
+
116
+ const getInvitationCodeInfo = (
117
+ invitationCode: string,
118
+ application: string
119
+ ) => {
120
+ InvitationBackend.getInvitationCodeInfo(invitationCode, application).then(
121
+ (res: any) => {
122
+ if (res.status === "error") {
123
+ Setting.showMessage("error", res.msg);
124
+ return;
125
+ }
126
+ setState({ ...state, invitation: res.data });
127
+ }
128
+ );
129
+ };
130
+
131
+ // const onUpdateAccount = (account: Account) => {
132
+ // props.onUpdateAccount(account);
133
+ // };
134
+
135
+ const onUpdateApplication = (application: Application | null) => {
136
+ props.onUpdateApplication(application);
137
+ };
138
+
139
+ React.useEffect(() => {
140
+ const oAuthParams = AuthUtil.getOAuthGetParameters();
141
+ if (oAuthParams !== null) {
142
+ const signinUrl = window.location.pathname.replace(
143
+ "/signup/oauth/authorize",
144
+ "/login/oauth/authorize"
145
+ );
146
+ sessionStorage.setItem("signinUrl", signinUrl + window.location.search);
147
+ }
148
+
149
+ if (getApplicationObj() === undefined) {
150
+ if (state.applicationName !== null) {
151
+ getApplication(state.applicationName);
152
+
153
+ const sp = new URLSearchParams(window.location.search);
154
+ if (sp.has("invitationCode")) {
155
+ const invitationCode = sp.get("invitationCode") ?? "";
156
+ setState({ ...state, invitationCode });
157
+ if (invitationCode !== "") {
158
+ getInvitationCodeInfo(
159
+ invitationCode,
160
+ "admin/" + state.applicationName
161
+ );
162
+ }
163
+ }
164
+ } else if (oAuthParams !== null) {
165
+ getApplicationLogin(oAuthParams);
166
+ } else {
167
+ Setting.showMessage(
168
+ "error",
169
+ `Unknown application name: ${state.applicationName}`
170
+ );
171
+ onUpdateApplication(null);
172
+ }
173
+ }
174
+ }, []);
175
+
176
+ const application = getApplicationObj();
177
+ if (application === undefined || application === null) {
178
+ return null;
179
+ }
180
+ // return <div>SIGN UP</div>;
181
+ return application.signupItems?.map((signupItem, idx) => {
182
+ return (
183
+ <div key={idx}>
184
+ {renderFormItem(
185
+ application,
186
+ signupItem,
187
+ state,
188
+ setState,
189
+ t,
190
+ props.CLIENT_ID,
191
+ props.CLIENT_SECRET
192
+ )}
193
+ </div>
194
+ );
195
+ });
196
+ }
197
+
198
+ type SignUpState = {
199
+ application: Application | null;
200
+ };
201
+
202
+ type SignUpProps = {
203
+ applicationName: string;
204
+ casdoorApiRoot: string;
205
+ springmicroApiRoot?: string;
206
+ CLIENT_ID: string;
207
+ CLIENT_SECRET: string;
208
+ };
209
+
210
+ export function SignUp({
211
+ applicationName,
212
+ casdoorApiRoot,
213
+ springmicroApiRoot,
214
+ CLIENT_ID,
215
+ CLIENT_SECRET,
216
+ }: SignUpProps) {
217
+ Setting.ROOT_URLS.casdoor = casdoorApiRoot;
218
+ if (springmicroApiRoot) {
219
+ Setting.ROOT_URLS.springmicro = springmicroApiRoot;
220
+ }
221
+ const [state, setState] = React.useState<SignUpState>({ application: null });
222
+
223
+ const getApplication = () => {
224
+ ApplicationBackend.getApplication(
225
+ "admin",
226
+ applicationName,
227
+ CLIENT_ID,
228
+ CLIENT_SECRET
229
+ ).then((res: any) => {
230
+ console.log(res);
231
+ if (res.status === "error") {
232
+ Setting.showMessage("error", res.msg);
233
+ return;
234
+ }
235
+
236
+ setState({
237
+ application: res.data,
238
+ });
239
+ });
240
+ };
241
+
242
+ React.useEffect(() => {
243
+ // console.log(i18n.t("signup.username"));
244
+ getApplication();
245
+ }, []);
246
+
247
+ if (state.application === undefined) return <></>;
248
+ return (
249
+ <I18nextProvider i18n={i18n}>
250
+ <SignUpForm
251
+ application={state.application}
252
+ onUpdateApplication={(application) => {
253
+ setState({
254
+ application: application,
255
+ });
256
+ }}
257
+ CLIENT_ID={CLIENT_ID}
258
+ CLIENT_SECRET={CLIENT_SECRET}
259
+ />
260
+ </I18nextProvider>
261
+ );
262
+ }
263
+
264
+ interface SignUpContextType {
265
+ application: Application;
266
+ getDefaultValues: typeof getDefaultValues;
267
+ getFormProps: typeof getFormProps;
268
+ icon: (
269
+ signupItem: SignupItem,
270
+ Button: React.JSX.ElementType
271
+ ) => React.JSX.Element | null;
272
+ getZod: typeof getZod;
273
+ getKey: typeof getKey;
274
+ type: (signupItem: SignupItem) => string;
275
+ sendCode: typeof UserBackend.sendCode;
276
+ requiresCode: typeof requiresCode;
277
+ getCaptcha: typeof UserBackend.getCaptcha;
278
+ isVisible: typeof isVisible;
279
+ // updateApplication: (updates: Partial<Application>) => void;
280
+ }
281
+
282
+ const SignUpContext = React.createContext<SignUpContextType | undefined>(
283
+ undefined
284
+ );
285
+
286
+ export function SignUpProvider({
287
+ applicationName,
288
+ casdoorApiRoot,
289
+ springmicroApiRoot,
290
+ CLIENT_ID,
291
+ CLIENT_SECRET,
292
+ children,
293
+ }: React.PropsWithChildren<SignUpProps>) {
294
+ const { t, i18n } = useTranslation();
295
+ Setting.ROOT_URLS.casdoor = casdoorApiRoot;
296
+ if (springmicroApiRoot) {
297
+ Setting.ROOT_URLS.springmicro = springmicroApiRoot;
298
+ }
299
+ const [state, setState] = React.useState<
300
+ SignUpState & Partial<SignUpFormState>
301
+ >({ application: null });
302
+ const [togglePasswordVisibility, setTogglePasswordVisibility] =
303
+ React.useState<Record<string, boolean>>({});
304
+
305
+ const getApplication = () => {
306
+ ApplicationBackend.getApplication(
307
+ "admin",
308
+ applicationName,
309
+ CLIENT_ID,
310
+ CLIENT_SECRET
311
+ ).then((res: any) => {
312
+ console.log(res);
313
+ if (res.status === "error") {
314
+ Setting.showMessage("error", res.msg);
315
+ return;
316
+ }
317
+
318
+ setState({
319
+ application: res.data,
320
+ });
321
+ });
322
+ };
323
+
324
+ const onUpdateApplication = (application: Application) => {
325
+ setState({
326
+ application: application,
327
+ });
328
+ };
329
+
330
+ const getInvitationCodeInfo = (
331
+ invitationCode: string,
332
+ application: string
333
+ ) => {
334
+ InvitationBackend.getInvitationCodeInfo(invitationCode, application).then(
335
+ (res: any) => {
336
+ if (res.status === "error") {
337
+ Setting.showMessage("error", res.msg);
338
+ return;
339
+ }
340
+ setState({ ...state, invitation: res.data });
341
+ }
342
+ );
343
+ };
344
+
345
+ const getApplicationLogin = (oAuthParams: any) => {
346
+ AuthBackend.getApplicationLogin(oAuthParams).then((res) => {
347
+ if (res.status === "ok") {
348
+ const application = res.data;
349
+ onUpdateApplication(application);
350
+ } else {
351
+ onUpdateApplication(null);
352
+ setState({
353
+ ...state,
354
+ msg: res.msg,
355
+ });
356
+ }
357
+ });
358
+ };
359
+
360
+ const icon = (item: SignupItem, Button: React.JSX.ElementType) => {
361
+ return getIcon(
362
+ item,
363
+ {
364
+ password: {
365
+ state: togglePasswordVisibility,
366
+ setState: setTogglePasswordVisibility,
367
+ },
368
+ },
369
+ Button
370
+ );
371
+ };
372
+
373
+ const type = (item: SignupItem) => {
374
+ return getType(item, {
375
+ hidePassword: togglePasswordVisibility,
376
+ });
377
+ };
378
+
379
+ const sendCode = UserBackend.sendCode;
380
+ const getCaptcha = UserBackend.getCaptcha;
381
+
382
+ React.useEffect(() => {
383
+ // console.log(i18n.t("signup.username"));
384
+ getApplication();
385
+ }, []);
386
+
387
+ React.useEffect(() => {
388
+ if (state.application) {
389
+ setTogglePasswordVisibility(
390
+ getPasswordVisibilityRecord(state.application.signupItems)
391
+ );
392
+ }
393
+ }, [state.application]);
394
+
395
+ React.useEffect(() => {
396
+ const oAuthParams = AuthUtil.getOAuthGetParameters();
397
+ if (oAuthParams !== null) {
398
+ const signinUrl = window.location.pathname.replace(
399
+ "/signup/oauth/authorize",
400
+ "/login/oauth/authorize"
401
+ );
402
+ sessionStorage.setItem("signinUrl", signinUrl + window.location.search);
403
+ }
404
+
405
+ if (state.application === undefined) {
406
+ if (state.applicationName !== null) {
407
+ getApplication();
408
+
409
+ const sp = new URLSearchParams(window.location.search);
410
+ if (sp.has("invitationCode")) {
411
+ const invitationCode = sp.get("invitationCode") ?? "";
412
+ setState({ ...state, invitationCode });
413
+ if (invitationCode !== "") {
414
+ getInvitationCodeInfo(
415
+ invitationCode,
416
+ "admin/" + state.applicationName
417
+ );
418
+ }
419
+ }
420
+ } else if (oAuthParams !== null) {
421
+ getApplicationLogin(oAuthParams);
422
+ } else {
423
+ Setting.showMessage(
424
+ "error",
425
+ `Unknown application name: ${state.applicationName}`
426
+ );
427
+ onUpdateApplication(null);
428
+ }
429
+ }
430
+ }, []);
431
+
432
+ // const application = getApplicationObj();
433
+ if (state.application === undefined || state.application === null) {
434
+ return null;
435
+ }
436
+ // return <div>SIGN UP</div>;
437
+ // return state.application.signupItems?.map((signupItem, idx) => {
438
+ // return (
439
+ // <div key={idx}>
440
+ // {renderFormItem(state.application, signupItem, state, setState, t)}
441
+ // </div>
442
+ // );
443
+ // });
444
+
445
+ return (
446
+ <SignUpContext.Provider
447
+ value={{
448
+ application: state.application,
449
+ getDefaultValues,
450
+ getFormProps,
451
+ icon,
452
+ getZod,
453
+ getKey,
454
+ type,
455
+ sendCode,
456
+ requiresCode,
457
+ getCaptcha,
458
+ isVisible,
459
+ }}
460
+ >
461
+ {children}
462
+ </SignUpContext.Provider>
463
+ );
464
+ }
465
+
466
+ export const useSignUp = (): SignUpContextType => {
467
+ const context = React.useContext(SignUpContext);
468
+ if (!context) {
469
+ throw new Error("useSignUp must be used within a SignUpProvider");
470
+ }
471
+ return context;
472
+ };