dauth-context-react 5.0.0 → 6.0.0
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/README.md +101 -228
- package/dist/index.d.mts +12 -2
- package/dist/index.d.ts +12 -2
- package/dist/index.js +789 -96
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +794 -96
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -1
- package/src/DauthProfileModal.tsx +875 -0
- package/src/api/dauth.api.ts +6 -17
- package/src/api/interfaces/dauth.api.responses.ts +0 -7
- package/src/api/utils/config.ts +8 -5
- package/src/index.tsx +13 -26
- package/src/initialDauthState.ts +0 -1
- package/src/interfaces.ts +12 -6
- package/src/reducer/dauth.actions.ts +9 -61
package/src/api/dauth.api.ts
CHANGED
|
@@ -4,14 +4,18 @@ import {
|
|
|
4
4
|
ISessionResponse,
|
|
5
5
|
IUpdateUserResponse,
|
|
6
6
|
IDeleteAccountResponse,
|
|
7
|
-
IProfileRedirectResponse,
|
|
8
7
|
} from './interfaces/dauth.api.responses';
|
|
9
8
|
|
|
10
9
|
function getCsrfToken(): string {
|
|
11
10
|
const match = document.cookie.match(
|
|
12
11
|
/(?:^|;\s*)(?:__Host-csrf|csrf-token)=([^;]*)/
|
|
13
12
|
);
|
|
14
|
-
|
|
13
|
+
if (!match?.[1]) return '';
|
|
14
|
+
try {
|
|
15
|
+
return decodeURIComponent(match[1]);
|
|
16
|
+
} catch {
|
|
17
|
+
return match[1];
|
|
18
|
+
}
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
export async function exchangeCodeAPI(
|
|
@@ -81,18 +85,3 @@ export async function deleteAccountAPI(
|
|
|
81
85
|
const data = await response.json();
|
|
82
86
|
return { response, data };
|
|
83
87
|
}
|
|
84
|
-
|
|
85
|
-
export async function profileRedirectAPI(
|
|
86
|
-
basePath: string
|
|
87
|
-
): Promise<IProfileRedirectResponse> {
|
|
88
|
-
const response = await fetch(
|
|
89
|
-
`${basePath}/profile-redirect`,
|
|
90
|
-
{
|
|
91
|
-
method: 'GET',
|
|
92
|
-
headers: { 'X-CSRF-Token': getCsrfToken() },
|
|
93
|
-
credentials: 'include',
|
|
94
|
-
}
|
|
95
|
-
);
|
|
96
|
-
const data = await response.json();
|
|
97
|
-
return { response, data };
|
|
98
|
-
}
|
package/src/api/utils/config.ts
CHANGED
|
@@ -9,14 +9,17 @@ export function setDauthUrl(url: string | undefined) {
|
|
|
9
9
|
function checkIsLocalhost(): boolean {
|
|
10
10
|
if (typeof window === 'undefined') return false;
|
|
11
11
|
const hostname = window.location.hostname;
|
|
12
|
-
return
|
|
12
|
+
return (
|
|
13
13
|
hostname === 'localhost' ||
|
|
14
14
|
hostname === '[::1]' ||
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
/^127(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)){3}$/.test(
|
|
16
|
+
hostname
|
|
17
17
|
) ||
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
/^192\.168(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)){2}$/.test(
|
|
19
|
+
hostname
|
|
20
|
+
) ||
|
|
21
|
+
/^10(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)){3}$/.test(
|
|
22
|
+
hostname
|
|
20
23
|
)
|
|
21
24
|
);
|
|
22
25
|
}
|
package/src/index.tsx
CHANGED
|
@@ -16,9 +16,17 @@ import type {
|
|
|
16
16
|
IDauthProviderProps,
|
|
17
17
|
IDauthAuthMethods,
|
|
18
18
|
IDauthUser,
|
|
19
|
+
IFormField,
|
|
20
|
+
DauthProfileModalProps,
|
|
19
21
|
} from './interfaces';
|
|
20
22
|
|
|
21
|
-
export
|
|
23
|
+
export { DauthProfileModal } from './DauthProfileModal';
|
|
24
|
+
export type {
|
|
25
|
+
IDauthProviderProps,
|
|
26
|
+
IDauthAuthMethods,
|
|
27
|
+
IFormField,
|
|
28
|
+
DauthProfileModalProps,
|
|
29
|
+
};
|
|
22
30
|
|
|
23
31
|
const defaultOnError = (error: Error) => console.error(error);
|
|
24
32
|
|
|
@@ -33,10 +41,7 @@ export const DauthProvider: React.FC<IDauthProviderProps> = (
|
|
|
33
41
|
env,
|
|
34
42
|
dauthUrl,
|
|
35
43
|
} = props;
|
|
36
|
-
const [dauthState, dispatch] = useReducer(
|
|
37
|
-
userReducer,
|
|
38
|
-
initialDauthState
|
|
39
|
-
);
|
|
44
|
+
const [dauthState, dispatch] = useReducer(userReducer, initialDauthState);
|
|
40
45
|
|
|
41
46
|
// Configure custom dauth URL before any API calls
|
|
42
47
|
useEffect(() => {
|
|
@@ -66,16 +71,11 @@ export const DauthProvider: React.FC<IDauthProviderProps> = (
|
|
|
66
71
|
|
|
67
72
|
const loginWithRedirect = useCallback(() => {
|
|
68
73
|
const base = `${getClientBasePath()}/${domainName}/${routes.signin}`;
|
|
69
|
-
const url = env
|
|
70
|
-
? `${base}?env=${encodeURIComponent(env)}`
|
|
71
|
-
: base;
|
|
74
|
+
const url = env ? `${base}?env=${encodeURIComponent(env)}` : base;
|
|
72
75
|
return window.location.replace(url);
|
|
73
76
|
}, [domainName, env]);
|
|
74
77
|
|
|
75
|
-
const logout = useCallback(
|
|
76
|
-
() => action.logoutAction(ctx),
|
|
77
|
-
[ctx]
|
|
78
|
-
);
|
|
78
|
+
const logout = useCallback(() => action.logoutAction(ctx), [ctx]);
|
|
79
79
|
|
|
80
80
|
const updateUser = useCallback(
|
|
81
81
|
async (fields: Partial<IDauthUser>) => {
|
|
@@ -108,11 +108,6 @@ export const DauthProvider: React.FC<IDauthProviderProps> = (
|
|
|
108
108
|
[ctx]
|
|
109
109
|
);
|
|
110
110
|
|
|
111
|
-
const updateUserWithRedirect = useCallback(
|
|
112
|
-
() => action.updateUserWithRedirectAction(ctx),
|
|
113
|
-
[ctx]
|
|
114
|
-
);
|
|
115
|
-
|
|
116
111
|
const deleteAccount = useCallback(
|
|
117
112
|
() => action.deleteAccountAction(ctx),
|
|
118
113
|
[ctx]
|
|
@@ -124,17 +119,9 @@ export const DauthProvider: React.FC<IDauthProviderProps> = (
|
|
|
124
119
|
loginWithRedirect,
|
|
125
120
|
logout,
|
|
126
121
|
updateUser,
|
|
127
|
-
updateUserWithRedirect,
|
|
128
122
|
deleteAccount,
|
|
129
123
|
}),
|
|
130
|
-
[
|
|
131
|
-
dauthState,
|
|
132
|
-
loginWithRedirect,
|
|
133
|
-
logout,
|
|
134
|
-
updateUser,
|
|
135
|
-
updateUserWithRedirect,
|
|
136
|
-
deleteAccount,
|
|
137
|
-
]
|
|
124
|
+
[dauthState, loginWithRedirect, logout, updateUser, deleteAccount]
|
|
138
125
|
);
|
|
139
126
|
|
|
140
127
|
return (
|
package/src/initialDauthState.ts
CHANGED
package/src/interfaces.ts
CHANGED
|
@@ -31,12 +31,18 @@ export interface IDauthAuthMethods {
|
|
|
31
31
|
passkey: boolean;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
export interface IFormField {
|
|
35
|
+
field: string;
|
|
36
|
+
required: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
34
39
|
export interface IDauthDomainState {
|
|
35
40
|
name: string;
|
|
36
41
|
environments?: IDauthDomainEnvironment[];
|
|
37
42
|
loginRedirect: string;
|
|
38
43
|
allowedOrigins: string[];
|
|
39
44
|
authMethods?: IDauthAuthMethods;
|
|
45
|
+
formFields?: IFormField[];
|
|
40
46
|
}
|
|
41
47
|
|
|
42
48
|
export interface IDauthState {
|
|
@@ -47,19 +53,19 @@ export interface IDauthState {
|
|
|
47
53
|
loginWithRedirect: () => void;
|
|
48
54
|
logout: () => void;
|
|
49
55
|
updateUser: (fields: Partial<IDauthUser>) => Promise<boolean>;
|
|
50
|
-
updateUserWithRedirect: () => void;
|
|
51
56
|
deleteAccount: () => Promise<boolean>;
|
|
52
57
|
}
|
|
53
58
|
|
|
59
|
+
export interface DauthProfileModalProps {
|
|
60
|
+
open: boolean;
|
|
61
|
+
onClose: () => void;
|
|
62
|
+
}
|
|
63
|
+
|
|
54
64
|
export interface IActionStatus {
|
|
55
65
|
type: TStatusTypes;
|
|
56
66
|
message: string;
|
|
57
67
|
}
|
|
58
|
-
export type TStatusTypes =
|
|
59
|
-
| 'success'
|
|
60
|
-
| 'error'
|
|
61
|
-
| 'info'
|
|
62
|
-
| 'warning';
|
|
68
|
+
export type TStatusTypes = 'success' | 'error' | 'info' | 'warning';
|
|
63
69
|
|
|
64
70
|
export interface IDauthProviderProps {
|
|
65
71
|
domainName: string;
|
|
@@ -4,7 +4,6 @@ import {
|
|
|
4
4
|
logoutAPI,
|
|
5
5
|
updateUserAPI,
|
|
6
6
|
deleteAccountAPI,
|
|
7
|
-
profileRedirectAPI,
|
|
8
7
|
} from '../api/dauth.api';
|
|
9
8
|
import { IDauthDomainState, IDauthUser } from '../interfaces';
|
|
10
9
|
import * as DauthTypes from './dauth.types';
|
|
@@ -15,10 +14,7 @@ export interface ActionContext {
|
|
|
15
14
|
onError: (error: Error) => void;
|
|
16
15
|
}
|
|
17
16
|
|
|
18
|
-
export async function exchangeCodeAction(
|
|
19
|
-
ctx: ActionContext,
|
|
20
|
-
code: string
|
|
21
|
-
) {
|
|
17
|
+
export async function exchangeCodeAction(ctx: ActionContext, code: string) {
|
|
22
18
|
const { dispatch, authProxyPath, onError } = ctx;
|
|
23
19
|
dispatch({
|
|
24
20
|
type: DauthTypes.SET_IS_LOADING,
|
|
@@ -26,11 +22,7 @@ export async function exchangeCodeAction(
|
|
|
26
22
|
});
|
|
27
23
|
try {
|
|
28
24
|
// Clean URL immediately (before any fetch)
|
|
29
|
-
window.history.replaceState(
|
|
30
|
-
{},
|
|
31
|
-
document.title,
|
|
32
|
-
window.location.pathname
|
|
33
|
-
);
|
|
25
|
+
window.history.replaceState({}, document.title, window.location.pathname);
|
|
34
26
|
const result = await exchangeCodeAPI(authProxyPath, code);
|
|
35
27
|
if (result.response.status === 200) {
|
|
36
28
|
dispatch({
|
|
@@ -45,9 +37,7 @@ export async function exchangeCodeAction(
|
|
|
45
37
|
}
|
|
46
38
|
resetUser(dispatch);
|
|
47
39
|
} catch (error) {
|
|
48
|
-
onError(
|
|
49
|
-
error instanceof Error ? error : new Error(String(error))
|
|
50
|
-
);
|
|
40
|
+
onError(error instanceof Error ? error : new Error(String(error)));
|
|
51
41
|
resetUser(dispatch);
|
|
52
42
|
} finally {
|
|
53
43
|
dispatch({
|
|
@@ -79,9 +69,7 @@ export async function autoLoginAction(ctx: ActionContext) {
|
|
|
79
69
|
// No session — not authenticated (not an error)
|
|
80
70
|
resetUser(dispatch);
|
|
81
71
|
} catch (error) {
|
|
82
|
-
onError(
|
|
83
|
-
error instanceof Error ? error : new Error(String(error))
|
|
84
|
-
);
|
|
72
|
+
onError(error instanceof Error ? error : new Error(String(error)));
|
|
85
73
|
resetUser(dispatch);
|
|
86
74
|
} finally {
|
|
87
75
|
dispatch({
|
|
@@ -106,9 +94,7 @@ export async function logoutAction(ctx: ActionContext) {
|
|
|
106
94
|
type: DauthTypes.LOGIN,
|
|
107
95
|
payload: {
|
|
108
96
|
user: {
|
|
109
|
-
language:
|
|
110
|
-
window.document.documentElement.getAttribute('lang') ||
|
|
111
|
-
'es',
|
|
97
|
+
language: window.document.documentElement.getAttribute('lang') || 'es',
|
|
112
98
|
},
|
|
113
99
|
domain: {},
|
|
114
100
|
isAuthenticated: false,
|
|
@@ -126,10 +112,7 @@ export async function updateUserAction(
|
|
|
126
112
|
): Promise<boolean> {
|
|
127
113
|
const { dispatch, authProxyPath, onError } = ctx;
|
|
128
114
|
if (user.language) {
|
|
129
|
-
window.document.documentElement.setAttribute(
|
|
130
|
-
'lang',
|
|
131
|
-
user.language
|
|
132
|
-
);
|
|
115
|
+
window.document.documentElement.setAttribute('lang', user.language);
|
|
133
116
|
}
|
|
134
117
|
try {
|
|
135
118
|
const result = await updateUserAPI(authProxyPath, user);
|
|
@@ -140,45 +123,14 @@ export async function updateUserAction(
|
|
|
140
123
|
});
|
|
141
124
|
return true;
|
|
142
125
|
}
|
|
143
|
-
onError(
|
|
144
|
-
new Error('Update user error: ' + result.data.message)
|
|
145
|
-
);
|
|
126
|
+
onError(new Error('Update user error: ' + result.data.message));
|
|
146
127
|
return false;
|
|
147
128
|
} catch (error) {
|
|
148
|
-
onError(
|
|
149
|
-
error instanceof Error
|
|
150
|
-
? error
|
|
151
|
-
: new Error('Update user error')
|
|
152
|
-
);
|
|
129
|
+
onError(error instanceof Error ? error : new Error('Update user error'));
|
|
153
130
|
return false;
|
|
154
131
|
}
|
|
155
132
|
}
|
|
156
133
|
|
|
157
|
-
export async function updateUserWithRedirectAction(
|
|
158
|
-
ctx: ActionContext
|
|
159
|
-
) {
|
|
160
|
-
const { authProxyPath, onError } = ctx;
|
|
161
|
-
try {
|
|
162
|
-
const result = await profileRedirectAPI(authProxyPath);
|
|
163
|
-
if (
|
|
164
|
-
result.response.status === 200 &&
|
|
165
|
-
result.data.redirectUrl
|
|
166
|
-
) {
|
|
167
|
-
window.location.replace(result.data.redirectUrl);
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
onError(
|
|
171
|
-
new Error('Could not generate profile redirect')
|
|
172
|
-
);
|
|
173
|
-
} catch (error) {
|
|
174
|
-
onError(
|
|
175
|
-
error instanceof Error
|
|
176
|
-
? error
|
|
177
|
-
: new Error('Profile redirect error')
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
134
|
export async function deleteAccountAction(
|
|
183
135
|
ctx: ActionContext
|
|
184
136
|
): Promise<boolean> {
|
|
@@ -191,11 +143,7 @@ export async function deleteAccountAction(
|
|
|
191
143
|
}
|
|
192
144
|
return false;
|
|
193
145
|
} catch (error) {
|
|
194
|
-
onError(
|
|
195
|
-
error instanceof Error
|
|
196
|
-
? error
|
|
197
|
-
: new Error('Delete account error')
|
|
198
|
-
);
|
|
146
|
+
onError(error instanceof Error ? error : new Error('Delete account error'));
|
|
199
147
|
return false;
|
|
200
148
|
}
|
|
201
149
|
}
|