@jsb188/auth-web 1.1.12 → 1.1.13

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 jsb188
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@jsb188/auth-web",
3
- "version": "1.1.12",
3
+ "version": "1.1.13",
4
4
  "description": "Authentication module for web.",
5
- "license": "ISC",
5
+ "license": "MIT",
6
6
  "author": "jsb188",
7
7
  "type": "module",
8
8
  "exports": {
@@ -12,18 +12,26 @@
12
12
  },
13
13
  "peerDependencies": {
14
14
  "@azure/msal-browser": "^4.30.0",
15
- "@jsb188/app": "1.1.12",
16
- "@jsb188/graphql": "1.1.12",
17
- "@jsb188/react": "1.1.12",
18
- "@jsb188/react-web": "1.1.12",
15
+ "@jsb188/app": "1.1.13",
16
+ "@jsb188/graphql": "1.1.13",
17
+ "@jsb188/react": "1.1.13",
18
+ "@jsb188/react-web": "1.1.13",
19
19
  "jotai": "^2.20.2",
20
20
  "react": "^19.3.0",
21
21
  "react-router": "^8.4.0",
22
- "@jsb188/form": "1.1.12",
22
+ "@jsb188/form": "1.1.13",
23
23
  "zod": "^3.25.76"
24
24
  },
25
25
  "scripts": {
26
26
  "test": "echo \"Error: no test specified\" && exit 1",
27
27
  "build": "tsc"
28
- }
28
+ },
29
+ "files": [
30
+ "src/**/*.{ts,tsx,css,svg}",
31
+ "!**/*.test.*",
32
+ "!**/*.spec.*",
33
+ "!**/tests/**",
34
+ "!**/*.md",
35
+ "!**/.DS_Store"
36
+ ]
29
37
  }
@@ -1,15 +1,9 @@
1
1
  import { useCallback, useEffect, useRef, useState } from 'react';
2
2
  import { hydrateAuthenticationSession } from '../modules/_helpers.ts';
3
- import {
4
- type AuthPopupCompletionMessage,
5
- isAuthPopupCompletionMessage,
6
- isAuthPopupRespondentCompletionMessage,
7
- } from '../utils/authPopup.ts';
3
+ import { type AuthPopupCompletionMessage, isAuthPopupCompletionMessage } from '../utils/authPopup.ts';
8
4
 
9
5
  interface UseAuthenticationPopupParams {
10
6
  href?: string | null;
11
- formPublicId?: string | null;
12
- onRespondentAuthenticated?: (token: string, email: string) => void | Promise<void>;
13
7
  onAuthenticated: (auth: any, completionMessage: AuthPopupCompletionMessage) => boolean | Promise<boolean>;
14
8
  onError?: (error: any) => void;
15
9
  }
@@ -22,18 +16,13 @@ export function useAuthenticationPopup(p: UseAuthenticationPopupParams) {
22
16
  const [popupOpen, setPopupOpen] = useState(false);
23
17
  const popupRef = useRef<Window | null>(null);
24
18
  const popupIdRef = useRef<string | null>(null);
25
- const popupFormPublicIdRef = useRef<string | null>(null);
26
19
  const closeTimerRef = useRef<ReturnType<typeof globalThis.setInterval> | null>(null);
27
20
  const completionStartedRef = useRef(false);
28
21
  const onAuthenticatedRef = useRef(p.onAuthenticated);
29
22
  const onErrorRef = useRef(p.onError);
30
- const formPublicIdRef = useRef(p.formPublicId);
31
- const onRespondentAuthenticatedRef = useRef(p.onRespondentAuthenticated);
32
23
 
33
24
  onAuthenticatedRef.current = p.onAuthenticated;
34
25
  onErrorRef.current = p.onError;
35
- formPublicIdRef.current = p.formPublicId;
36
- onRespondentAuthenticatedRef.current = p.onRespondentAuthenticated;
37
26
 
38
27
  /*
39
28
  * Clear the tracked popup and its close monitor after any terminal outcome.
@@ -45,7 +34,6 @@ export function useAuthenticationPopup(p: UseAuthenticationPopupParams) {
45
34
  }
46
35
  popupRef.current = null;
47
36
  popupIdRef.current = null;
48
- popupFormPublicIdRef.current = null;
49
37
  completionStartedRef.current = false;
50
38
  setPopupOpen(false);
51
39
  }, []);
@@ -67,25 +55,13 @@ export function useAuthenticationPopup(p: UseAuthenticationPopupParams) {
67
55
  return;
68
56
  }
69
57
 
70
- const formPublicId = popupFormPublicIdRef.current;
71
- const respondentCompletion =
72
- formPublicId &&
73
- formPublicId === formPublicIdRef.current &&
74
- onRespondentAuthenticatedRef.current &&
75
- isAuthPopupRespondentCompletionMessage(event.data, popupId, formPublicId)
76
- ? event.data
77
- : null;
78
58
  const accountCompletion = isAuthPopupCompletionMessage(event.data, popupId) ? event.data : null;
79
- if (!respondentCompletion && !accountCompletion) return;
59
+ if (!accountCompletion) return;
80
60
 
81
61
  completionStartedRef.current = true;
82
62
 
83
63
  /* Adopt only the identity kind returned by this still-active popup. */
84
64
  const completeAuthentication = async () => {
85
- if (respondentCompletion) {
86
- await onRespondentAuthenticatedRef.current?.(respondentCompletion.respondentToken, respondentCompletion.respondentEmail);
87
- return;
88
- }
89
65
  const { auth, error } = await hydrateAuthenticationSession();
90
66
  if (popupRef.current !== popup || popupIdRef.current !== popupId) return;
91
67
  if (!auth?.token || !accountCompletion || !(await onAuthenticatedRef.current(auth, accountCompletion))) {
@@ -148,7 +124,6 @@ export function useAuthenticationPopup(p: UseAuthenticationPopupParams) {
148
124
 
149
125
  popupRef.current = popup;
150
126
  popupIdRef.current = popupId;
151
- popupFormPublicIdRef.current = formPublicIdRef.current || null;
152
127
  setPopupOpen(true);
153
128
  popup.focus();
154
129
  closeTimerRef.current = globalThis.setInterval(() => {
@@ -19,10 +19,6 @@ export interface SsoLoginOptionData {
19
19
  googleHostedDomain?: string | null;
20
20
  }
21
21
 
22
- // Called when an "authenticateOnly" sign-in verified an email with no account
23
- // and the server returned a temporary form respondent token instead
24
- type OnRespondentTokenFn = (token: string, email: string | null) => void;
25
-
26
22
  export type OnFinishSignInFn = (
27
23
  token: string,
28
24
  sessionBootstrapToken?: string | null,
@@ -32,13 +28,11 @@ export type OnFinishSignInFn = (
32
28
  interface ContinueWithGoogleParams {
33
29
  doNotAlertErrors?: boolean;
34
30
  onFinishSignIn: OnFinishSignInFn;
35
- onRespondentToken?: OnRespondentTokenFn;
36
31
  }
37
32
 
38
33
  interface ContinueWithMicrosoftParams {
39
34
  doNotAlertErrors?: boolean;
40
35
  onFinishSignIn: OnFinishSignInFn;
41
- onRespondentToken?: OnRespondentTokenFn;
42
36
  onError?: OnErrorGQLFn;
43
37
  }
44
38
 
@@ -54,20 +48,13 @@ interface GoogleLoginHookParams extends ContinueWithGoogleParams {
54
48
  /*
55
49
  * Apply a BFF-authenticated provider result to the caller and shared app state.
56
50
  */
57
- async function applyOAuthAuthenticationResult(
58
- auth: any,
59
- onFinishSignIn: OnFinishSignInFn,
60
- onRespondentToken: OnRespondentTokenFn | undefined,
61
- dispatchApp: any,
62
- ): Promise<boolean> {
51
+ async function applyOAuthAuthenticationResult(auth: any, onFinishSignIn: OnFinishSignInFn, dispatchApp: any): Promise<boolean> {
63
52
  if (auth?.token && auth.account?.id) {
64
53
  const persisted = await onFinishSignIn(auth.token, null, true);
65
54
  if (persisted) {
66
55
  onAccountLogin({ ...auth, sessionBootstrapToken: null }, dispatchApp);
67
56
  return true;
68
57
  }
69
- } else if (auth?.respondentToken) {
70
- onRespondentToken?.(auth.respondentToken, auth.respondentEmail || null);
71
58
  }
72
59
 
73
60
  return false;
@@ -78,18 +65,7 @@ async function applyOAuthAuthenticationResult(
78
65
  */
79
66
 
80
67
  function useGoogleLogin(p: GoogleLoginHookParams) {
81
- const {
82
- scriptLoaded,
83
- scope = '',
84
- onError,
85
- onNonOAuthError,
86
- onFinishSignIn,
87
- onRespondentToken,
88
- overrideScope,
89
- state,
90
- doNotAlertErrors,
91
- ...props
92
- } = p;
68
+ const { scriptLoaded, scope = '', onError, onNonOAuthError, onFinishSignIn, overrideScope, state, doNotAlertErrors, ...props } = p;
93
69
 
94
70
  const { dispatchApp } = useContext(AppContext);
95
71
  const openModalPopUp = useOpenModalPopUp();
@@ -104,9 +80,6 @@ function useGoogleLogin(p: GoogleLoginHookParams) {
104
80
  const onNonOAuthErrorRef = useRef(onNonOAuthError);
105
81
  onNonOAuthErrorRef.current = onNonOAuthError;
106
82
 
107
- const onRespondentTokenRef = useRef(onRespondentToken);
108
- onRespondentTokenRef.current = onRespondentToken;
109
-
110
83
  const onFinishSignInRef = useRef(onFinishSignIn);
111
84
  onFinishSignInRef.current = onFinishSignIn;
112
85
 
@@ -121,11 +94,6 @@ function useGoogleLogin(p: GoogleLoginHookParams) {
121
94
 
122
95
  // Org SSO connection to sign in through; read by the token callback below
123
96
  const ssoConnectionIdRef = useRef<string | null>(null);
124
- // Public web form context; allows contact-gated account creation (CHILD_ORGS forms)
125
- const formPublicIdRef = useRef<string | null>(null);
126
- // When true, a verified email with no account returns a temporary respondent
127
- // token instead of erroring (ANYONE web forms)
128
- const authenticateOnlyRef = useRef<boolean>(false);
129
97
 
130
98
  useEffect(() => {
131
99
  if (scriptLoaded) {
@@ -147,8 +115,6 @@ function useGoogleLogin(p: GoogleLoginHookParams) {
147
115
  provider: 'GOOGLE' as const,
148
116
  oauthToken,
149
117
  ssoConnectionId: ssoConnectionIdRef.current,
150
- formPublicId: formPublicIdRef.current,
151
- authenticateOnly: authenticateOnlyRef.current,
152
118
  };
153
119
  const { auth, error } = await authenticateOAuthSignInSession(variables);
154
120
  if (error) {
@@ -161,12 +127,7 @@ function useGoogleLogin(p: GoogleLoginHookParams) {
161
127
  }
162
128
 
163
129
  try {
164
- const authenticated = await applyOAuthAuthenticationResult(
165
- auth,
166
- onFinishSignInRef.current,
167
- onRespondentTokenRef.current,
168
- dispatchAppRef.current,
169
- );
130
+ const authenticated = await applyOAuthAuthenticationResult(auth, onFinishSignInRef.current, dispatchAppRef.current);
170
131
  if (!authenticated) {
171
132
  setWaitingForOAuth(false);
172
133
  }
@@ -188,23 +149,18 @@ function useGoogleLogin(p: GoogleLoginHookParams) {
188
149
  }
189
150
  }, [scriptLoaded, scope, state]);
190
151
 
191
- const onInitiateGoogleSignIn = useCallback(
192
- (overrideConfig?: object, ssoConnectionId?: string | null, formPublicId?: string | null, authenticateOnly?: boolean) => {
193
- ssoConnectionIdRef.current = ssoConnectionId || null;
194
- formPublicIdRef.current = formPublicId || null;
195
- authenticateOnlyRef.current = !!authenticateOnly;
196
- if (clientRef.current) {
197
- setWaitingForProvider(true);
198
- try {
199
- clientRef.current.requestAccessToken(overrideConfig);
200
- } catch (error) {
201
- setWaitingForProvider(false);
202
- throw error;
203
- }
152
+ const onInitiateGoogleSignIn = useCallback((overrideConfig?: object, ssoConnectionId?: string | null) => {
153
+ ssoConnectionIdRef.current = ssoConnectionId || null;
154
+ if (clientRef.current) {
155
+ setWaitingForProvider(true);
156
+ try {
157
+ clientRef.current.requestAccessToken(overrideConfig);
158
+ } catch (error) {
159
+ setWaitingForProvider(false);
160
+ throw error;
204
161
  }
205
- },
206
- [],
207
- );
162
+ }
163
+ }, []);
208
164
 
209
165
  return {
210
166
  onInitiateGoogleSignIn,
@@ -218,7 +174,7 @@ function useGoogleLogin(p: GoogleLoginHookParams) {
218
174
  */
219
175
 
220
176
  export function useContinueWithGoogle(params: ContinueWithGoogleParams) {
221
- const { doNotAlertErrors, onFinishSignIn, onRespondentToken } = params;
177
+ const { doNotAlertErrors, onFinishSignIn } = params;
222
178
  const [scriptLoaded, setScriptLoaded] = useState(false);
223
179
 
224
180
  useEffect(() => {
@@ -239,7 +195,6 @@ export function useContinueWithGoogle(params: ContinueWithGoogleParams) {
239
195
  const { onInitiateGoogleSignIn, oauthButtonDisabled, waitingForOAuth } = useGoogleLogin({
240
196
  doNotAlertErrors,
241
197
  onFinishSignIn,
242
- onRespondentToken,
243
198
  scriptLoaded,
244
199
  });
245
200
 
@@ -289,15 +244,12 @@ function getMSALClient() {
289
244
  */
290
245
 
291
246
  export function useContinueWithMicrosoft(params: ContinueWithMicrosoftParams) {
292
- const { doNotAlertErrors, onFinishSignIn, onRespondentToken, onError } = params;
247
+ const { doNotAlertErrors, onFinishSignIn, onError } = params;
293
248
  const { dispatchApp } = useContext(AppContext);
294
249
  const openModalPopUp = useOpenModalPopUp();
295
250
  const [waitingForPopUp, setWaitingForPopUp] = useState(false);
296
251
  const [waitingForOAuth, setWaitingForOAuth] = useState(false);
297
252
 
298
- const onRespondentTokenRef = useRef(onRespondentToken);
299
- onRespondentTokenRef.current = onRespondentToken;
300
-
301
253
  const onFinishSignInRef = useRef(onFinishSignIn);
302
254
  onFinishSignInRef.current = onFinishSignIn;
303
255
 
@@ -313,65 +265,59 @@ export function useContinueWithMicrosoft(params: ContinueWithMicrosoftParams) {
313
265
  const dispatchAppRef = useRef(dispatchApp);
314
266
  dispatchAppRef.current = dispatchApp;
315
267
 
316
- const onInitiateMicrosoftSignIn = useCallback(
317
- async (ssoOption?: SsoLoginOptionData | any, loginHint?: string, formPublicId?: string | null, authenticateOnly?: boolean) => {
318
- setWaitingForPopUp(true);
319
-
320
- // A plain onClick passes the click event here; only a real SSO option
321
- // (with a tenant id) changes the popup behavior
322
- const ssoTenantId = ssoOption?.microsoftTenantId || null;
323
- const ssoConnectionId = ssoTenantId ? ssoOption?.id : null;
324
-
325
- try {
326
- const pca = await getMSALClient();
327
- const result = await pca.loginPopup({
328
- scopes: ['openid', 'profile', 'email'],
329
- prompt: 'select_account',
330
- // Pin the popup to the org's Entra tenant instead of "common"
331
- ...(ssoTenantId ? { authority: `https://login.microsoftonline.com/${ssoTenantId}` } : null),
332
- ...(loginHint ? { loginHint } : null),
333
- });
268
+ const onInitiateMicrosoftSignIn = useCallback(async (ssoOption?: SsoLoginOptionData | any, loginHint?: string) => {
269
+ setWaitingForPopUp(true);
270
+
271
+ // A plain onClick passes the click event here; only a real SSO option
272
+ // (with a tenant id) changes the popup behavior
273
+ const ssoTenantId = ssoOption?.microsoftTenantId || null;
274
+ const ssoConnectionId = ssoTenantId ? ssoOption?.id : null;
275
+
276
+ try {
277
+ const pca = await getMSALClient();
278
+ const result = await pca.loginPopup({
279
+ scopes: ['openid', 'profile', 'email'],
280
+ prompt: 'select_account',
281
+ // Pin the popup to the org's Entra tenant instead of "common"
282
+ ...(ssoTenantId ? { authority: `https://login.microsoftonline.com/${ssoTenantId}` } : null),
283
+ ...(loginHint ? { loginHint } : null),
284
+ });
334
285
 
335
- if (result?.idToken) {
336
- setWaitingForOAuth(true);
337
- const variables = {
338
- provider: 'MICROSOFT' as const,
339
- idToken: result.idToken,
340
- ssoConnectionId,
341
- formPublicId,
342
- authenticateOnly,
343
- };
344
- const authentication = await authenticateOAuthSignInSession(variables);
345
- if (authentication.error) {
286
+ if (result?.idToken) {
287
+ setWaitingForOAuth(true);
288
+ const variables = {
289
+ provider: 'MICROSOFT' as const,
290
+ idToken: result.idToken,
291
+ ssoConnectionId,
292
+ };
293
+ const authentication = await authenticateOAuthSignInSession(variables);
294
+ if (authentication.error) {
295
+ setWaitingForOAuth(false);
296
+ if (!doNotAlertErrorsRef.current) {
297
+ openModalPopUpRef.current(null, authentication.error);
298
+ }
299
+ onErrorRef.current?.(authentication.error, variables);
300
+ } else {
301
+ const authenticated = await applyOAuthAuthenticationResult(
302
+ authentication.auth,
303
+ onFinishSignInRef.current,
304
+ dispatchAppRef.current,
305
+ );
306
+ if (!authenticated) {
346
307
  setWaitingForOAuth(false);
347
- if (!doNotAlertErrorsRef.current) {
348
- openModalPopUpRef.current(null, authentication.error);
349
- }
350
- onErrorRef.current?.(authentication.error, variables);
351
- } else {
352
- const authenticated = await applyOAuthAuthenticationResult(
353
- authentication.auth,
354
- onFinishSignInRef.current,
355
- onRespondentTokenRef.current,
356
- dispatchAppRef.current,
357
- );
358
- if (!authenticated) {
359
- setWaitingForOAuth(false);
360
- }
361
308
  }
362
309
  }
363
- } catch (err: any) {
364
- setWaitingForOAuth(false);
365
- // User closing/cancelling the popup is not an error
366
- if (err?.errorCode !== 'user_cancelled') {
367
- console.warn(err);
368
- }
369
- } finally {
370
- setWaitingForPopUp(false);
371
310
  }
372
- },
373
- [],
374
- );
311
+ } catch (err: any) {
312
+ setWaitingForOAuth(false);
313
+ // User closing/cancelling the popup is not an error
314
+ if (err?.errorCode !== 'user_cancelled') {
315
+ console.warn(err);
316
+ }
317
+ } finally {
318
+ setWaitingForPopUp(false);
319
+ }
320
+ }, []);
375
321
 
376
322
  return {
377
323
  onInitiateMicrosoftSignIn,
package/src/main.ts CHANGED
@@ -1,11 +1,8 @@
1
1
  export * from './hooks/use-auth-popup';
2
2
  export * from './hooks/use-oauth';
3
- export * from './hooks/use-google-one-tap';
4
- export { suppressGoogleAutoSignIn } from './utils/googleIdentity';
5
3
  export {
6
4
  getAuthReturnQuery,
7
5
  getAuthReturnContext,
8
- getPublicFormReturnContext,
9
6
  hydrateAuthenticationSession,
10
7
  persistSignInSession,
11
8
  } from './modules/_helpers';
@@ -17,7 +17,6 @@ import { makeRequestTokenizedEmailSchema } from './schemas';
17
17
 
18
18
  interface SsoSignInProps {
19
19
  authRoutesMap?: Record<string, string>;
20
- formPublicId?: string | null;
21
20
  returnTo?: string | null;
22
21
  setAuthIface: (iface: null) => void;
23
22
  }
@@ -28,13 +27,8 @@ interface SsoSignInProps {
28
27
  * allowed to open the OAuth popup)
29
28
  */
30
29
 
31
- function SsoOrgOptions(p: {
32
- email: string;
33
- formPublicId?: string | null;
34
- options: SsoLoginOptionData[];
35
- onFinishSignIn: OnFinishSignInFn;
36
- }) {
37
- const { email, formPublicId, options, onFinishSignIn } = p;
30
+ function SsoOrgOptions(p: { email: string; options: SsoLoginOptionData[]; onFinishSignIn: OnFinishSignInFn }) {
31
+ const { email, options, onFinishSignIn } = p;
38
32
  const {
39
33
  onInitiateGoogleSignIn,
40
34
  oauthButtonDisabled: googleDisabled,
@@ -65,16 +59,15 @@ function SsoOrgOptions(p: {
65
59
  hint: email || undefined,
66
60
  },
67
61
  option.id,
68
- formPublicId ?? null,
69
62
  );
70
63
  break;
71
64
  case 'MICROSOFT':
72
- onInitiateMicrosoftSignIn(option, email, formPublicId ?? null);
65
+ onInitiateMicrosoftSignIn(option, email ?? null);
73
66
  break;
74
67
  default:
75
68
  }
76
69
  },
77
- [email, formPublicId, onInitiateGoogleSignIn, onInitiateMicrosoftSignIn],
70
+ [email, onInitiateGoogleSignIn, onInitiateMicrosoftSignIn],
78
71
  );
79
72
 
80
73
  return (
@@ -105,7 +98,7 @@ function SsoOrgOptions(p: {
105
98
  */
106
99
 
107
100
  export function SsoSignIn(p: SsoSignInProps) {
108
- const { authRoutesMap, formPublicId, returnTo, setAuthIface } = p;
101
+ const { authRoutesMap, returnTo, setAuthIface } = p;
109
102
  const navigate = useNavigate();
110
103
  const [email, setEmail] = useState('');
111
104
  const [options, setOptions] = useState<SsoLoginOptionData[] | null>(null);
@@ -170,7 +163,6 @@ export function SsoSignIn(p: SsoSignInProps) {
170
163
  {options?.length ? (
171
164
  <SsoOrgOptions
172
165
  email={email}
173
- formPublicId={formPublicId}
174
166
  options={options}
175
167
  onFinishSignIn={onFinishSignIn}
176
168
  />
@@ -199,7 +191,7 @@ export function SsoSignIn(p: SsoSignInProps) {
199
191
  type="button"
200
192
  disabled={saving}
201
193
  onClick={onBack}
202
- className="text ft_sm cl_md link"
194
+ className="text_link ft_sm cl_md link"
203
195
  >
204
196
  {i18n.t(options?.length ? 'auth.sso_use_different_email' : 'auth.sign_in_other_methods')}
205
197
  </button>