dauth-context-react 2.1.0 → 2.3.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 +15 -11
- package/dist/{interfaces.d.ts → index.d.mts} +65 -48
- package/dist/index.d.ts +65 -8
- package/dist/index.js +730 -5
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +715 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +26 -20
- package/src/api/dauth.api.ts +20 -0
- package/src/api/interfaces/dauth.api.responses.ts +8 -0
- package/src/index.tsx +67 -32
- package/src/initialDauthState.ts +1 -0
- package/src/interfaces.ts +16 -0
- package/src/reducer/dauth.actions.ts +99 -60
- package/dist/api/dauth.api.d.ts +0 -10
- package/dist/api/interfaces/dauth.api.responses.d.ts +0 -38
- package/dist/api/utils/config.d.ts +0 -4
- package/dist/api/utils/routes.d.ts +0 -4
- package/dist/constants.d.ts +0 -2
- package/dist/dauth-context-react.cjs.development.js +0 -1118
- package/dist/dauth-context-react.cjs.development.js.map +0 -1
- package/dist/dauth-context-react.cjs.production.min.js +0 -2
- package/dist/dauth-context-react.cjs.production.min.js.map +0 -1
- package/dist/dauth-context-react.esm.js +0 -1110
- package/dist/dauth-context-react.esm.js.map +0 -1
- package/dist/initialDauthState.d.ts +0 -3
- package/dist/reducer/dauth.actions.d.ts +0 -41
- package/dist/reducer/dauth.reducer.d.ts +0 -2
- package/dist/reducer/dauth.types.d.ts +0 -5
|
@@ -1,25 +1,36 @@
|
|
|
1
1
|
import {
|
|
2
|
+
deleteAccountAPI,
|
|
2
3
|
getUserAPI,
|
|
3
4
|
logoutAPI,
|
|
4
5
|
refreshTokenAPI,
|
|
5
6
|
sendEmailVerificationAPI,
|
|
6
7
|
updateUserAPI,
|
|
7
8
|
} from '../api/dauth.api';
|
|
8
|
-
import {
|
|
9
|
-
|
|
9
|
+
import {
|
|
10
|
+
IDauthDomainState,
|
|
11
|
+
IDauthStorageKeys,
|
|
12
|
+
IDauthUser,
|
|
13
|
+
} from '../interfaces';
|
|
10
14
|
import * as DauthTypes from './dauth.types';
|
|
11
15
|
|
|
12
|
-
|
|
16
|
+
export interface ActionContext {
|
|
13
17
|
dispatch: React.Dispatch<any>;
|
|
18
|
+
domainName: string;
|
|
19
|
+
storageKeys: IDauthStorageKeys;
|
|
20
|
+
onError: (error: Error) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type TSetDauthStateAction = ActionContext & {
|
|
14
24
|
token: string;
|
|
15
25
|
refreshToken: string;
|
|
16
|
-
domainName: string;
|
|
17
26
|
};
|
|
18
27
|
export async function setDauthStateAction({
|
|
19
28
|
dispatch,
|
|
20
29
|
token,
|
|
21
30
|
refreshToken,
|
|
22
31
|
domainName,
|
|
32
|
+
storageKeys,
|
|
33
|
+
onError,
|
|
23
34
|
}: TSetDauthStateAction) {
|
|
24
35
|
dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } });
|
|
25
36
|
try {
|
|
@@ -34,15 +45,15 @@ export async function setDauthStateAction({
|
|
|
34
45
|
},
|
|
35
46
|
});
|
|
36
47
|
window.history.replaceState({}, document.title, window.location.pathname);
|
|
37
|
-
localStorage.setItem(
|
|
38
|
-
localStorage.setItem(
|
|
48
|
+
localStorage.setItem(storageKeys.accessToken, token);
|
|
49
|
+
localStorage.setItem(storageKeys.refreshToken, refreshToken);
|
|
39
50
|
return;
|
|
40
51
|
} else {
|
|
41
|
-
return resetUser(dispatch);
|
|
52
|
+
return resetUser(dispatch, storageKeys);
|
|
42
53
|
}
|
|
43
54
|
} catch (error) {
|
|
44
|
-
|
|
45
|
-
return resetUser(dispatch);
|
|
55
|
+
onError(error instanceof Error ? error : new Error(String(error)));
|
|
56
|
+
return resetUser(dispatch, storageKeys);
|
|
46
57
|
} finally {
|
|
47
58
|
dispatch({
|
|
48
59
|
type: DauthTypes.SET_IS_LOADING,
|
|
@@ -51,30 +62,28 @@ export async function setDauthStateAction({
|
|
|
51
62
|
}
|
|
52
63
|
}
|
|
53
64
|
|
|
54
|
-
type TSetAutoLoginAction = {
|
|
55
|
-
dispatch: React.Dispatch<any>;
|
|
56
|
-
domainName: string;
|
|
57
|
-
};
|
|
58
65
|
export async function setAutoLoginAction({
|
|
59
66
|
dispatch,
|
|
60
67
|
domainName,
|
|
61
|
-
|
|
68
|
+
storageKeys,
|
|
69
|
+
onError,
|
|
70
|
+
}: ActionContext) {
|
|
62
71
|
dispatch({ type: DauthTypes.SET_IS_LOADING, payload: { isLoading: true } });
|
|
63
|
-
const storedRefreshToken = localStorage.getItem(
|
|
72
|
+
const storedRefreshToken = localStorage.getItem(storageKeys.refreshToken);
|
|
64
73
|
if (!storedRefreshToken) {
|
|
65
74
|
dispatch({
|
|
66
75
|
type: DauthTypes.SET_IS_LOADING,
|
|
67
76
|
payload: { isLoading: false },
|
|
68
77
|
});
|
|
69
|
-
return resetUser(dispatch);
|
|
78
|
+
return resetUser(dispatch, storageKeys);
|
|
70
79
|
}
|
|
71
80
|
try {
|
|
72
81
|
const refreshResult = await refreshTokenAPI(domainName, storedRefreshToken);
|
|
73
82
|
if (refreshResult.response.status === 200) {
|
|
74
83
|
const newAccessToken = refreshResult.data.accessToken;
|
|
75
84
|
const newRefreshToken = refreshResult.data.refreshToken;
|
|
76
|
-
localStorage.setItem(
|
|
77
|
-
localStorage.setItem(
|
|
85
|
+
localStorage.setItem(storageKeys.accessToken, newAccessToken);
|
|
86
|
+
localStorage.setItem(storageKeys.refreshToken, newRefreshToken);
|
|
78
87
|
const getUserFetch = await getUserAPI(domainName, newAccessToken);
|
|
79
88
|
if (getUserFetch.response.status === 200) {
|
|
80
89
|
dispatch({
|
|
@@ -89,10 +98,10 @@ export async function setAutoLoginAction({
|
|
|
89
98
|
}
|
|
90
99
|
}
|
|
91
100
|
// Refresh failed — session expired
|
|
92
|
-
resetUser(dispatch);
|
|
101
|
+
resetUser(dispatch, storageKeys);
|
|
93
102
|
} catch (error) {
|
|
94
|
-
|
|
95
|
-
resetUser(dispatch);
|
|
103
|
+
onError(error instanceof Error ? error : new Error(String(error)));
|
|
104
|
+
resetUser(dispatch, storageKeys);
|
|
96
105
|
} finally {
|
|
97
106
|
dispatch({
|
|
98
107
|
type: DauthTypes.SET_IS_LOADING,
|
|
@@ -104,11 +113,9 @@ export async function setAutoLoginAction({
|
|
|
104
113
|
export async function setLogoutAction({
|
|
105
114
|
dispatch,
|
|
106
115
|
domainName,
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}) {
|
|
111
|
-
const storedRefreshToken = localStorage.getItem(REFRESH_TOKEN_LS);
|
|
116
|
+
storageKeys,
|
|
117
|
+
}: Omit<ActionContext, 'onError'>) {
|
|
118
|
+
const storedRefreshToken = localStorage.getItem(storageKeys.refreshToken);
|
|
112
119
|
if (storedRefreshToken && domainName) {
|
|
113
120
|
try {
|
|
114
121
|
await logoutAPI(domainName, storedRefreshToken);
|
|
@@ -121,15 +128,14 @@ export async function setLogoutAction({
|
|
|
121
128
|
type: DauthTypes.LOGIN,
|
|
122
129
|
payload: {
|
|
123
130
|
user: {
|
|
124
|
-
language:
|
|
125
|
-
window.document.documentElement.getAttribute('lang') || 'es',
|
|
131
|
+
language: window.document.documentElement.getAttribute('lang') || 'es',
|
|
126
132
|
},
|
|
127
133
|
domain: {},
|
|
128
134
|
isAuthenticated: false,
|
|
129
135
|
},
|
|
130
136
|
});
|
|
131
|
-
localStorage.removeItem(
|
|
132
|
-
localStorage.removeItem(
|
|
137
|
+
localStorage.removeItem(storageKeys.accessToken);
|
|
138
|
+
localStorage.removeItem(storageKeys.refreshToken);
|
|
133
139
|
return dispatch({
|
|
134
140
|
type: DauthTypes.SET_IS_LOADING,
|
|
135
141
|
payload: { isLoading: false },
|
|
@@ -139,32 +145,35 @@ export async function setLogoutAction({
|
|
|
139
145
|
export async function refreshSessionAction({
|
|
140
146
|
dispatch,
|
|
141
147
|
domainName,
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const storedRefreshToken = localStorage.getItem(REFRESH_TOKEN_LS);
|
|
148
|
+
storageKeys,
|
|
149
|
+
onError,
|
|
150
|
+
}: ActionContext) {
|
|
151
|
+
const storedRefreshToken = localStorage.getItem(storageKeys.refreshToken);
|
|
147
152
|
if (!storedRefreshToken) {
|
|
148
|
-
return resetUser(dispatch);
|
|
153
|
+
return resetUser(dispatch, storageKeys);
|
|
149
154
|
}
|
|
150
155
|
try {
|
|
151
156
|
const refreshResult = await refreshTokenAPI(domainName, storedRefreshToken);
|
|
152
157
|
if (refreshResult.response.status === 200) {
|
|
153
|
-
localStorage.setItem(
|
|
154
|
-
|
|
158
|
+
localStorage.setItem(
|
|
159
|
+
storageKeys.accessToken,
|
|
160
|
+
refreshResult.data.accessToken
|
|
161
|
+
);
|
|
162
|
+
localStorage.setItem(
|
|
163
|
+
storageKeys.refreshToken,
|
|
164
|
+
refreshResult.data.refreshToken
|
|
165
|
+
);
|
|
155
166
|
return;
|
|
156
167
|
}
|
|
157
168
|
// Refresh failed — revoked or expired
|
|
158
|
-
resetUser(dispatch);
|
|
169
|
+
resetUser(dispatch, storageKeys);
|
|
159
170
|
} catch (error) {
|
|
160
|
-
|
|
161
|
-
resetUser(dispatch);
|
|
171
|
+
onError(error instanceof Error ? error : new Error(String(error)));
|
|
172
|
+
resetUser(dispatch, storageKeys);
|
|
162
173
|
}
|
|
163
174
|
}
|
|
164
175
|
|
|
165
|
-
type TSetUpdateAction = {
|
|
166
|
-
dispatch: React.Dispatch<any>;
|
|
167
|
-
domainName: string;
|
|
176
|
+
type TSetUpdateAction = ActionContext & {
|
|
168
177
|
user: Partial<IDauthUser>;
|
|
169
178
|
token: string | null;
|
|
170
179
|
};
|
|
@@ -173,6 +182,7 @@ export async function setUpdateUserAction({
|
|
|
173
182
|
domainName,
|
|
174
183
|
user,
|
|
175
184
|
token,
|
|
185
|
+
onError,
|
|
176
186
|
}: TSetUpdateAction) {
|
|
177
187
|
if (user.language) {
|
|
178
188
|
window.document.documentElement.setAttribute('lang', user.language);
|
|
@@ -193,18 +203,18 @@ export async function setUpdateUserAction({
|
|
|
193
203
|
});
|
|
194
204
|
return true;
|
|
195
205
|
} else {
|
|
196
|
-
|
|
206
|
+
onError(new Error('Update user error: ' + getUserFetch.data.message));
|
|
197
207
|
return false;
|
|
198
208
|
}
|
|
199
209
|
} catch (error) {
|
|
200
|
-
|
|
210
|
+
onError(
|
|
211
|
+
error instanceof Error ? error : new Error('Update user error')
|
|
212
|
+
);
|
|
201
213
|
return false;
|
|
202
214
|
}
|
|
203
215
|
}
|
|
204
216
|
|
|
205
|
-
type TSetSendEmailVerificationAction = {
|
|
206
|
-
dispatch: React.Dispatch<any>;
|
|
207
|
-
domainName: string;
|
|
217
|
+
type TSetSendEmailVerificationAction = ActionContext & {
|
|
208
218
|
token: string;
|
|
209
219
|
};
|
|
210
220
|
export async function sendEmailVerificationAction({
|
|
@@ -243,7 +253,7 @@ export async function sendEmailVerificationAction({
|
|
|
243
253
|
});
|
|
244
254
|
return false;
|
|
245
255
|
}
|
|
246
|
-
} catch (
|
|
256
|
+
} catch (_) {
|
|
247
257
|
dispatch({
|
|
248
258
|
type: DauthTypes.SET_SEND_EMAIL_VERIFICATION_STATUS,
|
|
249
259
|
payload: {
|
|
@@ -262,11 +272,10 @@ export async function sendEmailVerificationAction({
|
|
|
262
272
|
export async function getAccessTokenAction({
|
|
263
273
|
dispatch,
|
|
264
274
|
domainName,
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
const token_ls = localStorage.getItem(TOKEN_LS);
|
|
275
|
+
storageKeys,
|
|
276
|
+
onError,
|
|
277
|
+
}: ActionContext) {
|
|
278
|
+
const token_ls = localStorage.getItem(storageKeys.accessToken);
|
|
270
279
|
if (!token_ls) return 'token-not-found';
|
|
271
280
|
// Decode JWT to check expiry (without verification — that's the server's job)
|
|
272
281
|
try {
|
|
@@ -276,8 +285,13 @@ export async function getAccessTokenAction({
|
|
|
276
285
|
const expiresIn = (payload.exp || 0) * 1000 - Date.now();
|
|
277
286
|
// If token expires in less than 5 minutes, refresh proactively
|
|
278
287
|
if (expiresIn < 5 * 60 * 1000) {
|
|
279
|
-
await refreshSessionAction({
|
|
280
|
-
|
|
288
|
+
await refreshSessionAction({
|
|
289
|
+
dispatch,
|
|
290
|
+
domainName,
|
|
291
|
+
storageKeys,
|
|
292
|
+
onError,
|
|
293
|
+
});
|
|
294
|
+
const refreshedToken = localStorage.getItem(storageKeys.accessToken);
|
|
281
295
|
return refreshedToken || 'token-not-found';
|
|
282
296
|
}
|
|
283
297
|
}
|
|
@@ -287,11 +301,36 @@ export async function getAccessTokenAction({
|
|
|
287
301
|
return token_ls;
|
|
288
302
|
}
|
|
289
303
|
|
|
304
|
+
export async function deleteAccountAction({
|
|
305
|
+
dispatch,
|
|
306
|
+
domainName,
|
|
307
|
+
storageKeys,
|
|
308
|
+
onError,
|
|
309
|
+
token,
|
|
310
|
+
}: ActionContext & { token: string }) {
|
|
311
|
+
try {
|
|
312
|
+
const result = await deleteAccountAPI(domainName, token);
|
|
313
|
+
if (result.response.status === 200) {
|
|
314
|
+
resetUser(dispatch, storageKeys);
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
return false;
|
|
318
|
+
} catch (error) {
|
|
319
|
+
onError(
|
|
320
|
+
error instanceof Error ? error : new Error('Delete account error')
|
|
321
|
+
);
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
290
326
|
///////////////////////////////////////////
|
|
291
327
|
//////////////////////////////////////////
|
|
292
|
-
export const resetUser = (
|
|
293
|
-
|
|
294
|
-
|
|
328
|
+
export const resetUser = (
|
|
329
|
+
dispatch: React.Dispatch<any>,
|
|
330
|
+
storageKeys: IDauthStorageKeys
|
|
331
|
+
) => {
|
|
332
|
+
localStorage.removeItem(storageKeys.accessToken);
|
|
333
|
+
localStorage.removeItem(storageKeys.refreshToken);
|
|
295
334
|
return dispatch({
|
|
296
335
|
type: DauthTypes.LOGIN,
|
|
297
336
|
payload: {
|
package/dist/api/dauth.api.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { IDauthUser } from '../interfaces';
|
|
2
|
-
import { IgetUserAPIResponse, IrefreshAccessTokenAPIResponse, IrefreshTokenAPIResponse, IsendEmailVerificationAPIResponse, IupdateUserAPIResponse } from './interfaces/dauth.api.responses';
|
|
3
|
-
export declare const getUserAPI: (domainName: string, token: string) => Promise<IgetUserAPIResponse>;
|
|
4
|
-
export declare const updateUserAPI: (domainName: string, user: Partial<IDauthUser>, token: string) => Promise<IupdateUserAPIResponse>;
|
|
5
|
-
export declare const sendEmailVerificationAPI: (domainName: string, token: string) => Promise<IsendEmailVerificationAPIResponse>;
|
|
6
|
-
export declare const refreshAccessTokenAPI: (domainName: string, token: string) => Promise<IrefreshAccessTokenAPIResponse>;
|
|
7
|
-
export declare const refreshTokenAPI: (domainName: string, refreshToken: string) => Promise<IrefreshTokenAPIResponse>;
|
|
8
|
-
export declare const logoutAPI: (domainName: string, refreshToken: string) => Promise<{
|
|
9
|
-
response: Response;
|
|
10
|
-
}>;
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { IDauthDomainState, IDauthUser } from '../../interfaces';
|
|
2
|
-
export interface IgetUserAPIResponse {
|
|
3
|
-
response: Response;
|
|
4
|
-
data: {
|
|
5
|
-
status: string;
|
|
6
|
-
user: IDauthUser;
|
|
7
|
-
domain: IDauthDomainState;
|
|
8
|
-
};
|
|
9
|
-
}
|
|
10
|
-
export interface IupdateUserAPIResponse {
|
|
11
|
-
response: Response;
|
|
12
|
-
data: {
|
|
13
|
-
status: string;
|
|
14
|
-
user: IDauthUser;
|
|
15
|
-
message: string;
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
export interface IsendEmailVerificationAPIResponse {
|
|
19
|
-
response: Response;
|
|
20
|
-
data: {
|
|
21
|
-
status: string;
|
|
22
|
-
message: string;
|
|
23
|
-
emailStatus: string;
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
export interface IrefreshAccessTokenAPIResponse {
|
|
27
|
-
response: Response;
|
|
28
|
-
data: {
|
|
29
|
-
accessToken: string;
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
export interface IrefreshTokenAPIResponse {
|
|
33
|
-
response: Response;
|
|
34
|
-
data: {
|
|
35
|
-
accessToken: string;
|
|
36
|
-
refreshToken: string;
|
|
37
|
-
};
|
|
38
|
-
}
|
package/dist/constants.d.ts
DELETED