@jsb188/auth-web 1.1.11 → 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 +21 -0
- package/package.json +19 -9
- package/src/hooks/use-auth-popup.ts +142 -0
- package/src/hooks/use-oauth.tsx +83 -163
- package/src/main.ts +8 -0
- package/src/modules/AssignRequiredAuth.tsx +93 -59
- package/src/modules/AuthFormUI.tsx +2 -2
- package/src/modules/AuthForms-SSO.tsx +39 -62
- package/src/modules/AuthForms.tsx +344 -266
- package/src/modules/AuthGate.tsx +8 -23
- package/src/modules/InlineAuthForm.tsx +319 -86
- package/src/modules/ManagedSessions.tsx +25 -42
- package/src/modules/OtherAuthButtons.tsx +18 -23
- package/src/modules/_helpers.ts +77 -42
- package/src/modules/schemas.ts +114 -244
- package/src/schemas/account.ts +86 -149
- package/src/schemas/address.ts +113 -156
- package/src/utils/authPopup.ts +35 -0
- package/src/utils/googleIdentity.ts +45 -0
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.
|
|
3
|
+
"version": "1.1.13",
|
|
4
4
|
"description": "Authentication module for web.",
|
|
5
|
-
"license": "
|
|
5
|
+
"license": "MIT",
|
|
6
6
|
"author": "jsb188",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"exports": {
|
|
@@ -12,16 +12,26 @@
|
|
|
12
12
|
},
|
|
13
13
|
"peerDependencies": {
|
|
14
14
|
"@azure/msal-browser": "^4.30.0",
|
|
15
|
-
"@jsb188/app": "1.1.
|
|
16
|
-
"@jsb188/graphql": "1.1.
|
|
17
|
-
"@jsb188/react": "1.1.
|
|
18
|
-
"@jsb188/react-web": "1.1.
|
|
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
|
-
"react": "^19.
|
|
21
|
-
"react-router": "^8.
|
|
20
|
+
"react": "^19.3.0",
|
|
21
|
+
"react-router": "^8.4.0",
|
|
22
|
+
"@jsb188/form": "1.1.13",
|
|
23
|
+
"zod": "^3.25.76"
|
|
22
24
|
},
|
|
23
25
|
"scripts": {
|
|
24
26
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
25
27
|
"build": "tsc"
|
|
26
|
-
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"src/**/*.{ts,tsx,css,svg}",
|
|
31
|
+
"!**/*.test.*",
|
|
32
|
+
"!**/*.spec.*",
|
|
33
|
+
"!**/tests/**",
|
|
34
|
+
"!**/*.md",
|
|
35
|
+
"!**/.DS_Store"
|
|
36
|
+
]
|
|
27
37
|
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { hydrateAuthenticationSession } from '../modules/_helpers.ts';
|
|
3
|
+
import { type AuthPopupCompletionMessage, isAuthPopupCompletionMessage } from '../utils/authPopup.ts';
|
|
4
|
+
|
|
5
|
+
interface UseAuthenticationPopupParams {
|
|
6
|
+
href?: string | null;
|
|
7
|
+
onAuthenticated: (auth: any, completionMessage: AuthPopupCompletionMessage) => boolean | Promise<boolean>;
|
|
8
|
+
onError?: (error: any) => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
* Open one same-origin authentication window and adopt its cookie-backed
|
|
13
|
+
* account session after the popup reports completion.
|
|
14
|
+
*/
|
|
15
|
+
export function useAuthenticationPopup(p: UseAuthenticationPopupParams) {
|
|
16
|
+
const [popupOpen, setPopupOpen] = useState(false);
|
|
17
|
+
const popupRef = useRef<Window | null>(null);
|
|
18
|
+
const popupIdRef = useRef<string | null>(null);
|
|
19
|
+
const closeTimerRef = useRef<ReturnType<typeof globalThis.setInterval> | null>(null);
|
|
20
|
+
const completionStartedRef = useRef(false);
|
|
21
|
+
const onAuthenticatedRef = useRef(p.onAuthenticated);
|
|
22
|
+
const onErrorRef = useRef(p.onError);
|
|
23
|
+
|
|
24
|
+
onAuthenticatedRef.current = p.onAuthenticated;
|
|
25
|
+
onErrorRef.current = p.onError;
|
|
26
|
+
|
|
27
|
+
/*
|
|
28
|
+
* Clear the tracked popup and its close monitor after any terminal outcome.
|
|
29
|
+
*/
|
|
30
|
+
const resetPopup = useCallback(() => {
|
|
31
|
+
if (closeTimerRef.current !== null) {
|
|
32
|
+
globalThis.clearInterval(closeTimerRef.current);
|
|
33
|
+
closeTimerRef.current = null;
|
|
34
|
+
}
|
|
35
|
+
popupRef.current = null;
|
|
36
|
+
popupIdRef.current = null;
|
|
37
|
+
completionStartedRef.current = false;
|
|
38
|
+
setPopupOpen(false);
|
|
39
|
+
}, []);
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
/*
|
|
43
|
+
* Validate a popup completion signal before rehydrating its shared session.
|
|
44
|
+
*/
|
|
45
|
+
const onMessage = (event: MessageEvent) => {
|
|
46
|
+
const popup = popupRef.current;
|
|
47
|
+
const popupId = popupIdRef.current;
|
|
48
|
+
if (
|
|
49
|
+
!popup ||
|
|
50
|
+
!popupId ||
|
|
51
|
+
event.origin !== globalThis.location.origin ||
|
|
52
|
+
event.source !== popup ||
|
|
53
|
+
completionStartedRef.current
|
|
54
|
+
) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const accountCompletion = isAuthPopupCompletionMessage(event.data, popupId) ? event.data : null;
|
|
59
|
+
if (!accountCompletion) return;
|
|
60
|
+
|
|
61
|
+
completionStartedRef.current = true;
|
|
62
|
+
|
|
63
|
+
/* Adopt only the identity kind returned by this still-active popup. */
|
|
64
|
+
const completeAuthentication = async () => {
|
|
65
|
+
const { auth, error } = await hydrateAuthenticationSession();
|
|
66
|
+
if (popupRef.current !== popup || popupIdRef.current !== popupId) return;
|
|
67
|
+
if (!auth?.token || !accountCompletion || !(await onAuthenticatedRef.current(auth, accountCompletion))) {
|
|
68
|
+
onErrorRef.current?.(error || { errorCode: 'network_error' });
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
void completeAuthentication()
|
|
72
|
+
.catch((error) => {
|
|
73
|
+
if (popupRef.current === popup) onErrorRef.current?.(error);
|
|
74
|
+
})
|
|
75
|
+
.finally(() => {
|
|
76
|
+
if (popupRef.current !== popup || popupIdRef.current !== popupId) return;
|
|
77
|
+
popup.close();
|
|
78
|
+
resetPopup();
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
globalThis.addEventListener('message', onMessage);
|
|
83
|
+
return () => {
|
|
84
|
+
globalThis.removeEventListener('message', onMessage);
|
|
85
|
+
popupRef.current?.close();
|
|
86
|
+
resetPopup();
|
|
87
|
+
};
|
|
88
|
+
}, [resetPopup]);
|
|
89
|
+
|
|
90
|
+
/*
|
|
91
|
+
* Launch the configured same-origin sign-in route from a direct user action.
|
|
92
|
+
*/
|
|
93
|
+
const openAuthenticationPopup = useCallback(
|
|
94
|
+
(additionalSearchParams?: Record<string, string>) => {
|
|
95
|
+
if (!p.href || popupRef.current) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const popupUrl = new URL(p.href, globalThis.location.origin);
|
|
100
|
+
if (popupUrl.origin !== globalThis.location.origin) {
|
|
101
|
+
onErrorRef.current?.({ errorCode: 'forbidden' });
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const popupId = globalThis.crypto.randomUUID();
|
|
106
|
+
for (const [name, value] of Object.entries(additionalSearchParams || {})) {
|
|
107
|
+
popupUrl.searchParams.set(name, value);
|
|
108
|
+
}
|
|
109
|
+
popupUrl.searchParams.set('popup', '1');
|
|
110
|
+
popupUrl.searchParams.set('popupId', popupId);
|
|
111
|
+
const width = 520;
|
|
112
|
+
const height = 720;
|
|
113
|
+
const left = Math.max(0, globalThis.screenX + (globalThis.outerWidth - width) / 2);
|
|
114
|
+
const top = Math.max(0, globalThis.screenY + (globalThis.outerHeight - height) / 2);
|
|
115
|
+
const popup = globalThis.open(
|
|
116
|
+
popupUrl.toString(),
|
|
117
|
+
'marketday-auth',
|
|
118
|
+
`popup,width=${width},height=${height},left=${Math.round(left)},top=${Math.round(top)}`,
|
|
119
|
+
);
|
|
120
|
+
if (!popup) {
|
|
121
|
+
onErrorRef.current?.({ errorCode: 'popup_blocked' });
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
popupRef.current = popup;
|
|
126
|
+
popupIdRef.current = popupId;
|
|
127
|
+
setPopupOpen(true);
|
|
128
|
+
popup.focus();
|
|
129
|
+
closeTimerRef.current = globalThis.setInterval(() => {
|
|
130
|
+
if (popup.closed && !completionStartedRef.current) {
|
|
131
|
+
resetPopup();
|
|
132
|
+
}
|
|
133
|
+
}, 500);
|
|
134
|
+
},
|
|
135
|
+
[p.href, resetPopup],
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
openAuthenticationPopup,
|
|
140
|
+
popupOpen,
|
|
141
|
+
};
|
|
142
|
+
}
|
package/src/hooks/use-oauth.tsx
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { getENVVariable } from '@jsb188/app';
|
|
2
2
|
import type { OnErrorGQLFn } from '@jsb188/graphql/types.d';
|
|
3
|
-
import {
|
|
4
|
-
AppContext,
|
|
5
|
-
onAccountLogin,
|
|
6
|
-
useOpenModalPopUp,
|
|
7
|
-
} from '@jsb188/react/states';
|
|
3
|
+
import { AppContext, onAccountLogin, useOpenModalPopUp } from '@jsb188/react/states';
|
|
8
4
|
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
|
|
9
5
|
import { authenticateOAuthSignInSession } from '../modules/_helpers';
|
|
6
|
+
import { loadGoogleIdentityServices } from '../utils/googleIdentity';
|
|
10
7
|
|
|
11
8
|
/**
|
|
12
9
|
* Types
|
|
@@ -22,10 +19,6 @@ export interface SsoLoginOptionData {
|
|
|
22
19
|
googleHostedDomain?: string | null;
|
|
23
20
|
}
|
|
24
21
|
|
|
25
|
-
// Called when an "authenticateOnly" sign-in verified an email with no account
|
|
26
|
-
// and the server returned a temporary form respondent token instead
|
|
27
|
-
type OnRespondentTokenFn = (token: string, email: string | null) => void;
|
|
28
|
-
|
|
29
22
|
export type OnFinishSignInFn = (
|
|
30
23
|
token: string,
|
|
31
24
|
sessionBootstrapToken?: string | null,
|
|
@@ -35,13 +28,11 @@ export type OnFinishSignInFn = (
|
|
|
35
28
|
interface ContinueWithGoogleParams {
|
|
36
29
|
doNotAlertErrors?: boolean;
|
|
37
30
|
onFinishSignIn: OnFinishSignInFn;
|
|
38
|
-
onRespondentToken?: OnRespondentTokenFn;
|
|
39
31
|
}
|
|
40
32
|
|
|
41
33
|
interface ContinueWithMicrosoftParams {
|
|
42
34
|
doNotAlertErrors?: boolean;
|
|
43
35
|
onFinishSignIn: OnFinishSignInFn;
|
|
44
|
-
onRespondentToken?: OnRespondentTokenFn;
|
|
45
36
|
onError?: OnErrorGQLFn;
|
|
46
37
|
}
|
|
47
38
|
|
|
@@ -57,20 +48,13 @@ interface GoogleLoginHookParams extends ContinueWithGoogleParams {
|
|
|
57
48
|
/*
|
|
58
49
|
* Apply a BFF-authenticated provider result to the caller and shared app state.
|
|
59
50
|
*/
|
|
60
|
-
async function applyOAuthAuthenticationResult(
|
|
61
|
-
auth: any,
|
|
62
|
-
onFinishSignIn: OnFinishSignInFn,
|
|
63
|
-
onRespondentToken: OnRespondentTokenFn | undefined,
|
|
64
|
-
dispatchApp: any,
|
|
65
|
-
): Promise<boolean> {
|
|
51
|
+
async function applyOAuthAuthenticationResult(auth: any, onFinishSignIn: OnFinishSignInFn, dispatchApp: any): Promise<boolean> {
|
|
66
52
|
if (auth?.token && auth.account?.id) {
|
|
67
53
|
const persisted = await onFinishSignIn(auth.token, null, true);
|
|
68
54
|
if (persisted) {
|
|
69
55
|
onAccountLogin({ ...auth, sessionBootstrapToken: null }, dispatchApp);
|
|
70
56
|
return true;
|
|
71
57
|
}
|
|
72
|
-
} else if (auth?.respondentToken) {
|
|
73
|
-
onRespondentToken?.(auth.respondentToken, auth.respondentEmail || null);
|
|
74
58
|
}
|
|
75
59
|
|
|
76
60
|
return false;
|
|
@@ -81,18 +65,7 @@ async function applyOAuthAuthenticationResult(
|
|
|
81
65
|
*/
|
|
82
66
|
|
|
83
67
|
function useGoogleLogin(p: GoogleLoginHookParams) {
|
|
84
|
-
const {
|
|
85
|
-
scriptLoaded,
|
|
86
|
-
scope = '',
|
|
87
|
-
onError,
|
|
88
|
-
onNonOAuthError,
|
|
89
|
-
onFinishSignIn,
|
|
90
|
-
onRespondentToken,
|
|
91
|
-
overrideScope,
|
|
92
|
-
state,
|
|
93
|
-
doNotAlertErrors,
|
|
94
|
-
...props
|
|
95
|
-
} = p;
|
|
68
|
+
const { scriptLoaded, scope = '', onError, onNonOAuthError, onFinishSignIn, overrideScope, state, doNotAlertErrors, ...props } = p;
|
|
96
69
|
|
|
97
70
|
const { dispatchApp } = useContext(AppContext);
|
|
98
71
|
const openModalPopUp = useOpenModalPopUp();
|
|
@@ -107,9 +80,6 @@ function useGoogleLogin(p: GoogleLoginHookParams) {
|
|
|
107
80
|
const onNonOAuthErrorRef = useRef(onNonOAuthError);
|
|
108
81
|
onNonOAuthErrorRef.current = onNonOAuthError;
|
|
109
82
|
|
|
110
|
-
const onRespondentTokenRef = useRef(onRespondentToken);
|
|
111
|
-
onRespondentTokenRef.current = onRespondentToken;
|
|
112
|
-
|
|
113
83
|
const onFinishSignInRef = useRef(onFinishSignIn);
|
|
114
84
|
onFinishSignInRef.current = onFinishSignIn;
|
|
115
85
|
|
|
@@ -124,18 +94,11 @@ function useGoogleLogin(p: GoogleLoginHookParams) {
|
|
|
124
94
|
|
|
125
95
|
// Org SSO connection to sign in through; read by the token callback below
|
|
126
96
|
const ssoConnectionIdRef = useRef<string | null>(null);
|
|
127
|
-
// Public web form context; allows contact-gated account creation (CHILD_ORGS forms)
|
|
128
|
-
const formPublicIdRef = useRef<string | null>(null);
|
|
129
|
-
// When true, a verified email with no account returns a temporary respondent
|
|
130
|
-
// token instead of erroring (ANYONE web forms)
|
|
131
|
-
const authenticateOnlyRef = useRef<boolean>(false);
|
|
132
97
|
|
|
133
98
|
useEffect(() => {
|
|
134
99
|
if (scriptLoaded) {
|
|
135
100
|
// If using this package, it is expected that you install your own Google OAuth script
|
|
136
|
-
const client = (
|
|
137
|
-
globalThis as any
|
|
138
|
-
).google?.accounts?.oauth2.initTokenClient({
|
|
101
|
+
const client = (globalThis as any).google?.accounts?.oauth2.initTokenClient({
|
|
139
102
|
client_id: getENVVariable('GOOGLE_OAUTH'),
|
|
140
103
|
scope: overrideScope ? scope : `openid profile email ${scope}`,
|
|
141
104
|
callback: async (response: any) => {
|
|
@@ -152,11 +115,8 @@ function useGoogleLogin(p: GoogleLoginHookParams) {
|
|
|
152
115
|
provider: 'GOOGLE' as const,
|
|
153
116
|
oauthToken,
|
|
154
117
|
ssoConnectionId: ssoConnectionIdRef.current,
|
|
155
|
-
formPublicId: formPublicIdRef.current,
|
|
156
|
-
authenticateOnly: authenticateOnlyRef.current,
|
|
157
118
|
};
|
|
158
|
-
const { auth, error } =
|
|
159
|
-
await authenticateOAuthSignInSession(variables);
|
|
119
|
+
const { auth, error } = await authenticateOAuthSignInSession(variables);
|
|
160
120
|
if (error) {
|
|
161
121
|
setWaitingForOAuth(false);
|
|
162
122
|
if (!doNotAlertErrorsRef.current) {
|
|
@@ -167,12 +127,7 @@ function useGoogleLogin(p: GoogleLoginHookParams) {
|
|
|
167
127
|
}
|
|
168
128
|
|
|
169
129
|
try {
|
|
170
|
-
const authenticated = await applyOAuthAuthenticationResult(
|
|
171
|
-
auth,
|
|
172
|
-
onFinishSignInRef.current,
|
|
173
|
-
onRespondentTokenRef.current,
|
|
174
|
-
dispatchAppRef.current,
|
|
175
|
-
);
|
|
130
|
+
const authenticated = await applyOAuthAuthenticationResult(auth, onFinishSignInRef.current, dispatchAppRef.current);
|
|
176
131
|
if (!authenticated) {
|
|
177
132
|
setWaitingForOAuth(false);
|
|
178
133
|
}
|
|
@@ -194,28 +149,18 @@ function useGoogleLogin(p: GoogleLoginHookParams) {
|
|
|
194
149
|
}
|
|
195
150
|
}, [scriptLoaded, scope, state]);
|
|
196
151
|
|
|
197
|
-
const onInitiateGoogleSignIn = useCallback(
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
authenticateOnlyRef.current = !!authenticateOnly;
|
|
207
|
-
if (clientRef.current) {
|
|
208
|
-
setWaitingForProvider(true);
|
|
209
|
-
try {
|
|
210
|
-
clientRef.current.requestAccessToken(overrideConfig);
|
|
211
|
-
} catch (error) {
|
|
212
|
-
setWaitingForProvider(false);
|
|
213
|
-
throw error;
|
|
214
|
-
}
|
|
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;
|
|
215
161
|
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
);
|
|
162
|
+
}
|
|
163
|
+
}, []);
|
|
219
164
|
|
|
220
165
|
return {
|
|
221
166
|
onInitiateGoogleSignIn,
|
|
@@ -229,36 +174,29 @@ function useGoogleLogin(p: GoogleLoginHookParams) {
|
|
|
229
174
|
*/
|
|
230
175
|
|
|
231
176
|
export function useContinueWithGoogle(params: ContinueWithGoogleParams) {
|
|
232
|
-
const { doNotAlertErrors, onFinishSignIn
|
|
177
|
+
const { doNotAlertErrors, onFinishSignIn } = params;
|
|
233
178
|
const [scriptLoaded, setScriptLoaded] = useState(false);
|
|
234
179
|
|
|
235
180
|
useEffect(() => {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
scriptTag.onerror = () => {
|
|
246
|
-
setScriptLoaded(false);
|
|
247
|
-
};
|
|
248
|
-
|
|
249
|
-
document.body.appendChild(scriptTag);
|
|
181
|
+
let active = true;
|
|
182
|
+
loadGoogleIdentityServices().then(
|
|
183
|
+
() => {
|
|
184
|
+
if (active) setScriptLoaded(true);
|
|
185
|
+
},
|
|
186
|
+
() => {
|
|
187
|
+
if (active) setScriptLoaded(false);
|
|
188
|
+
},
|
|
189
|
+
);
|
|
250
190
|
return () => {
|
|
251
|
-
|
|
191
|
+
active = false;
|
|
252
192
|
};
|
|
253
193
|
}, []);
|
|
254
194
|
|
|
255
|
-
const { onInitiateGoogleSignIn, oauthButtonDisabled, waitingForOAuth } =
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
scriptLoaded,
|
|
261
|
-
});
|
|
195
|
+
const { onInitiateGoogleSignIn, oauthButtonDisabled, waitingForOAuth } = useGoogleLogin({
|
|
196
|
+
doNotAlertErrors,
|
|
197
|
+
onFinishSignIn,
|
|
198
|
+
scriptLoaded,
|
|
199
|
+
});
|
|
262
200
|
|
|
263
201
|
return {
|
|
264
202
|
oauthButtonDisabled,
|
|
@@ -306,16 +244,12 @@ function getMSALClient() {
|
|
|
306
244
|
*/
|
|
307
245
|
|
|
308
246
|
export function useContinueWithMicrosoft(params: ContinueWithMicrosoftParams) {
|
|
309
|
-
const { doNotAlertErrors, onFinishSignIn,
|
|
310
|
-
params;
|
|
247
|
+
const { doNotAlertErrors, onFinishSignIn, onError } = params;
|
|
311
248
|
const { dispatchApp } = useContext(AppContext);
|
|
312
249
|
const openModalPopUp = useOpenModalPopUp();
|
|
313
250
|
const [waitingForPopUp, setWaitingForPopUp] = useState(false);
|
|
314
251
|
const [waitingForOAuth, setWaitingForOAuth] = useState(false);
|
|
315
252
|
|
|
316
|
-
const onRespondentTokenRef = useRef(onRespondentToken);
|
|
317
|
-
onRespondentTokenRef.current = onRespondentToken;
|
|
318
|
-
|
|
319
253
|
const onFinishSignInRef = useRef(onFinishSignIn);
|
|
320
254
|
onFinishSignInRef.current = onFinishSignIn;
|
|
321
255
|
|
|
@@ -331,73 +265,59 @@ export function useContinueWithMicrosoft(params: ContinueWithMicrosoftParams) {
|
|
|
331
265
|
const dispatchAppRef = useRef(dispatchApp);
|
|
332
266
|
dispatchAppRef.current = dispatchApp;
|
|
333
267
|
|
|
334
|
-
const onInitiateMicrosoftSignIn = useCallback(
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
scopes: ['openid', 'profile', 'email'],
|
|
352
|
-
prompt: 'select_account',
|
|
353
|
-
// Pin the popup to the org's Entra tenant instead of "common"
|
|
354
|
-
...(ssoTenantId
|
|
355
|
-
? { authority: `https://login.microsoftonline.com/${ssoTenantId}` }
|
|
356
|
-
: null),
|
|
357
|
-
...(loginHint ? { loginHint } : null),
|
|
358
|
-
});
|
|
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
|
+
});
|
|
359
285
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
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) {
|
|
372
307
|
setWaitingForOAuth(false);
|
|
373
|
-
if (!doNotAlertErrorsRef.current) {
|
|
374
|
-
openModalPopUpRef.current(null, authentication.error);
|
|
375
|
-
}
|
|
376
|
-
onErrorRef.current?.(authentication.error, variables);
|
|
377
|
-
} else {
|
|
378
|
-
const authenticated = await applyOAuthAuthenticationResult(
|
|
379
|
-
authentication.auth,
|
|
380
|
-
onFinishSignInRef.current,
|
|
381
|
-
onRespondentTokenRef.current,
|
|
382
|
-
dispatchAppRef.current,
|
|
383
|
-
);
|
|
384
|
-
if (!authenticated) {
|
|
385
|
-
setWaitingForOAuth(false);
|
|
386
|
-
}
|
|
387
308
|
}
|
|
388
309
|
}
|
|
389
|
-
} catch (err: any) {
|
|
390
|
-
setWaitingForOAuth(false);
|
|
391
|
-
// User closing/cancelling the popup is not an error
|
|
392
|
-
if (err?.errorCode !== 'user_cancelled') {
|
|
393
|
-
console.warn(err);
|
|
394
|
-
}
|
|
395
|
-
} finally {
|
|
396
|
-
setWaitingForPopUp(false);
|
|
397
310
|
}
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
|
|
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
|
+
}, []);
|
|
401
321
|
|
|
402
322
|
return {
|
|
403
323
|
onInitiateMicrosoftSignIn,
|
package/src/main.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
+
export * from './hooks/use-auth-popup';
|
|
1
2
|
export * from './hooks/use-oauth';
|
|
3
|
+
export {
|
|
4
|
+
getAuthReturnQuery,
|
|
5
|
+
getAuthReturnContext,
|
|
6
|
+
hydrateAuthenticationSession,
|
|
7
|
+
persistSignInSession,
|
|
8
|
+
} from './modules/_helpers';
|
|
2
9
|
export * from './modules/AssignRequiredAuth';
|
|
3
10
|
export * from './modules/AuthForms';
|
|
4
11
|
export * from './modules/AuthFormUI';
|
|
@@ -8,3 +15,4 @@ export * from './modules/ManagedSessions';
|
|
|
8
15
|
export * from './modules/OtherAuthButtons';
|
|
9
16
|
export * from './modules/schemas';
|
|
10
17
|
export * from './ui/ManagedSessionUI';
|
|
18
|
+
export * from './utils/authPopup';
|