dauth-context-react 4.0.3 → 5.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/dist/index.d.mts +3 -6
- package/dist/index.d.ts +3 -6
- package/dist/index.js +190 -366
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +191 -368
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -1
- package/src/api/dauth.api.ts +73 -93
- package/src/api/interfaces/dauth.api.responses.ts +11 -13
- package/src/api/utils/config.ts +3 -11
- package/src/api/utils/routes.ts +0 -1
- package/src/constants.ts +0 -2
- package/src/index.tsx +42 -122
- package/src/initialDauthState.ts +0 -1
- package/src/interfaces.ts +8 -12
- package/src/reducer/dauth.actions.ts +106 -187
package/dist/index.mjs
CHANGED
|
@@ -5,8 +5,7 @@ import {
|
|
|
5
5
|
useEffect,
|
|
6
6
|
useCallback,
|
|
7
7
|
createContext,
|
|
8
|
-
useContext
|
|
9
|
-
useRef
|
|
8
|
+
useContext
|
|
10
9
|
} from "react";
|
|
11
10
|
|
|
12
11
|
// src/initialDauthState.ts
|
|
@@ -21,7 +20,6 @@ var initialDauthState = {
|
|
|
21
20
|
},
|
|
22
21
|
logout: () => {
|
|
23
22
|
},
|
|
24
|
-
getAccessToken: () => Promise.resolve(""),
|
|
25
23
|
updateUser: () => Promise.resolve(false),
|
|
26
24
|
updateUserWithRedirect: () => {
|
|
27
25
|
},
|
|
@@ -69,133 +67,80 @@ function userReducer(state, action) {
|
|
|
69
67
|
}
|
|
70
68
|
}
|
|
71
69
|
|
|
72
|
-
// src/api/
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
function setDauthUrl(url) {
|
|
77
|
-
_dauthUrl = url?.replace(/\/+$/, "");
|
|
78
|
-
}
|
|
79
|
-
function checkIsLocalhost() {
|
|
80
|
-
if (typeof window === "undefined") return false;
|
|
81
|
-
const hostname = window.location.hostname;
|
|
82
|
-
return Boolean(
|
|
83
|
-
hostname === "localhost" || hostname === "[::1]" || hostname.match(
|
|
84
|
-
/(192)\.(168)\.(1)\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gm
|
|
85
|
-
) || hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/)
|
|
70
|
+
// src/api/dauth.api.ts
|
|
71
|
+
function getCsrfToken() {
|
|
72
|
+
const match = document.cookie.match(
|
|
73
|
+
/(?:^|;\s*)(?:__Host-csrf|csrf-token)=([^;]*)/
|
|
86
74
|
);
|
|
75
|
+
return match?.[1] ?? "";
|
|
87
76
|
}
|
|
88
|
-
function
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if (_dauthUrl) return _dauthUrl;
|
|
98
|
-
const isLocalhost = checkIsLocalhost();
|
|
99
|
-
const clientPort = 5185;
|
|
100
|
-
const clientLocalUrl = `${window.location.protocol}//${window.location.hostname}:${clientPort}`;
|
|
101
|
-
const clientProdUrl = `https://${serverDomain}`;
|
|
102
|
-
return isLocalhost ? clientLocalUrl : clientProdUrl;
|
|
77
|
+
async function exchangeCodeAPI(basePath, code) {
|
|
78
|
+
const response = await fetch(`${basePath}/exchange-code`, {
|
|
79
|
+
method: "POST",
|
|
80
|
+
headers: { "Content-Type": "application/json" },
|
|
81
|
+
credentials: "include",
|
|
82
|
+
body: JSON.stringify({ code })
|
|
83
|
+
});
|
|
84
|
+
const data = await response.json();
|
|
85
|
+
return { response, data };
|
|
103
86
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
var getUserAPI = async (domainName, token) => {
|
|
107
|
-
const params = {
|
|
87
|
+
async function getSessionAPI(basePath) {
|
|
88
|
+
const response = await fetch(`${basePath}/session`, {
|
|
108
89
|
method: "GET",
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
"Content-Type": "application/json"
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
const response = await fetch(
|
|
115
|
-
`${getServerBasePath()}/app/${domainName}/user`,
|
|
116
|
-
params
|
|
117
|
-
);
|
|
90
|
+
credentials: "include"
|
|
91
|
+
});
|
|
118
92
|
const data = await response.json();
|
|
119
93
|
return { response, data };
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const
|
|
94
|
+
}
|
|
95
|
+
async function logoutAPI(basePath) {
|
|
96
|
+
const response = await fetch(`${basePath}/logout`, {
|
|
97
|
+
method: "POST",
|
|
98
|
+
headers: {
|
|
99
|
+
"Content-Type": "application/json",
|
|
100
|
+
"X-CSRF-Token": getCsrfToken()
|
|
101
|
+
},
|
|
102
|
+
credentials: "include"
|
|
103
|
+
});
|
|
104
|
+
return { response };
|
|
105
|
+
}
|
|
106
|
+
async function updateUserAPI(basePath, user) {
|
|
107
|
+
const response = await fetch(`${basePath}/user`, {
|
|
123
108
|
method: "PATCH",
|
|
124
109
|
headers: {
|
|
125
|
-
|
|
126
|
-
"
|
|
110
|
+
"Content-Type": "application/json",
|
|
111
|
+
"X-CSRF-Token": getCsrfToken()
|
|
127
112
|
},
|
|
113
|
+
credentials: "include",
|
|
128
114
|
body: JSON.stringify(user)
|
|
129
|
-
};
|
|
130
|
-
const response = await fetch(
|
|
131
|
-
`${getServerBasePath()}/app/${domainName}/user`,
|
|
132
|
-
params
|
|
133
|
-
);
|
|
134
|
-
const data = await response.json();
|
|
135
|
-
return { response, data };
|
|
136
|
-
};
|
|
137
|
-
var refreshTokenAPI = async (domainName, refreshToken) => {
|
|
138
|
-
const params = {
|
|
139
|
-
method: "POST",
|
|
140
|
-
headers: { "Content-Type": "application/json" },
|
|
141
|
-
body: JSON.stringify({ refreshToken })
|
|
142
|
-
};
|
|
143
|
-
const response = await fetch(
|
|
144
|
-
`${getServerBasePath()}/app/${domainName}/refresh-token`,
|
|
145
|
-
params
|
|
146
|
-
);
|
|
115
|
+
});
|
|
147
116
|
const data = await response.json();
|
|
148
117
|
return { response, data };
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const
|
|
118
|
+
}
|
|
119
|
+
async function deleteAccountAPI(basePath) {
|
|
120
|
+
const response = await fetch(`${basePath}/user`, {
|
|
152
121
|
method: "DELETE",
|
|
153
|
-
headers: {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
const response = await fetch(
|
|
159
|
-
`${getServerBasePath()}/app/${domainName}/user`,
|
|
160
|
-
params
|
|
161
|
-
);
|
|
122
|
+
headers: { "X-CSRF-Token": getCsrfToken() },
|
|
123
|
+
credentials: "include"
|
|
124
|
+
});
|
|
162
125
|
const data = await response.json();
|
|
163
126
|
return { response, data };
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const params = {
|
|
167
|
-
method: "POST",
|
|
168
|
-
headers: { "Content-Type": "application/json" },
|
|
169
|
-
body: JSON.stringify({ code })
|
|
170
|
-
};
|
|
127
|
+
}
|
|
128
|
+
async function profileRedirectAPI(basePath) {
|
|
171
129
|
const response = await fetch(
|
|
172
|
-
`${
|
|
173
|
-
|
|
130
|
+
`${basePath}/profile-redirect`,
|
|
131
|
+
{
|
|
132
|
+
method: "GET",
|
|
133
|
+
headers: { "X-CSRF-Token": getCsrfToken() },
|
|
134
|
+
credentials: "include"
|
|
135
|
+
}
|
|
174
136
|
);
|
|
175
137
|
const data = await response.json();
|
|
176
138
|
return { response, data };
|
|
177
|
-
}
|
|
178
|
-
var logoutAPI = async (domainName, refreshToken) => {
|
|
179
|
-
const params = {
|
|
180
|
-
method: "POST",
|
|
181
|
-
headers: { "Content-Type": "application/json" },
|
|
182
|
-
body: JSON.stringify({ refreshToken })
|
|
183
|
-
};
|
|
184
|
-
const response = await fetch(
|
|
185
|
-
`${getServerBasePath()}/app/${domainName}/logout`,
|
|
186
|
-
params
|
|
187
|
-
);
|
|
188
|
-
return { response };
|
|
189
|
-
};
|
|
139
|
+
}
|
|
190
140
|
|
|
191
141
|
// src/reducer/dauth.actions.ts
|
|
192
|
-
async function exchangeCodeAction({
|
|
193
|
-
dispatch,
|
|
194
|
-
code,
|
|
195
|
-
domainName,
|
|
196
|
-
storageKeys,
|
|
197
|
-
onError
|
|
198
|
-
}) {
|
|
142
|
+
async function exchangeCodeAction(ctx, code) {
|
|
143
|
+
const { dispatch, authProxyPath, onError } = ctx;
|
|
199
144
|
dispatch({
|
|
200
145
|
type: SET_IS_LOADING,
|
|
201
146
|
payload: { isLoading: true }
|
|
@@ -206,31 +151,24 @@ async function exchangeCodeAction({
|
|
|
206
151
|
document.title,
|
|
207
152
|
window.location.pathname
|
|
208
153
|
);
|
|
209
|
-
const
|
|
210
|
-
if (
|
|
211
|
-
return resetUser(dispatch, storageKeys);
|
|
212
|
-
}
|
|
213
|
-
const { accessToken, refreshToken } = exchangeResult.data;
|
|
214
|
-
localStorage.setItem(storageKeys.accessToken, accessToken);
|
|
215
|
-
localStorage.setItem(storageKeys.refreshToken, refreshToken);
|
|
216
|
-
const getUserFetch = await getUserAPI(domainName, accessToken);
|
|
217
|
-
if (getUserFetch.response.status === 200) {
|
|
154
|
+
const result = await exchangeCodeAPI(authProxyPath, code);
|
|
155
|
+
if (result.response.status === 200) {
|
|
218
156
|
dispatch({
|
|
219
157
|
type: LOGIN,
|
|
220
158
|
payload: {
|
|
221
|
-
user:
|
|
222
|
-
domain:
|
|
159
|
+
user: result.data.user,
|
|
160
|
+
domain: result.data.domain,
|
|
223
161
|
isAuthenticated: true
|
|
224
162
|
}
|
|
225
163
|
});
|
|
226
164
|
return;
|
|
227
165
|
}
|
|
228
|
-
|
|
166
|
+
resetUser(dispatch);
|
|
229
167
|
} catch (error) {
|
|
230
168
|
onError(
|
|
231
169
|
error instanceof Error ? error : new Error(String(error))
|
|
232
170
|
);
|
|
233
|
-
|
|
171
|
+
resetUser(dispatch);
|
|
234
172
|
} finally {
|
|
235
173
|
dispatch({
|
|
236
174
|
type: SET_IS_LOADING,
|
|
@@ -238,45 +176,31 @@ async function exchangeCodeAction({
|
|
|
238
176
|
});
|
|
239
177
|
}
|
|
240
178
|
}
|
|
241
|
-
async function
|
|
242
|
-
dispatch,
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
})
|
|
247
|
-
dispatch({ type: SET_IS_LOADING, payload: { isLoading: true } });
|
|
248
|
-
const storedRefreshToken = localStorage.getItem(storageKeys.refreshToken);
|
|
249
|
-
if (!storedRefreshToken) {
|
|
250
|
-
dispatch({
|
|
251
|
-
type: SET_IS_LOADING,
|
|
252
|
-
payload: { isLoading: false }
|
|
253
|
-
});
|
|
254
|
-
return resetUser(dispatch, storageKeys);
|
|
255
|
-
}
|
|
179
|
+
async function autoLoginAction(ctx) {
|
|
180
|
+
const { dispatch, authProxyPath, onError } = ctx;
|
|
181
|
+
dispatch({
|
|
182
|
+
type: SET_IS_LOADING,
|
|
183
|
+
payload: { isLoading: true }
|
|
184
|
+
});
|
|
256
185
|
try {
|
|
257
|
-
const
|
|
258
|
-
if (
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
user: getUserFetch.data.user,
|
|
269
|
-
domain: getUserFetch.data.domain,
|
|
270
|
-
isAuthenticated: true
|
|
271
|
-
}
|
|
272
|
-
});
|
|
273
|
-
return;
|
|
274
|
-
}
|
|
186
|
+
const result = await getSessionAPI(authProxyPath);
|
|
187
|
+
if (result.response.status === 200) {
|
|
188
|
+
dispatch({
|
|
189
|
+
type: LOGIN,
|
|
190
|
+
payload: {
|
|
191
|
+
user: result.data.user,
|
|
192
|
+
domain: result.data.domain,
|
|
193
|
+
isAuthenticated: true
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
return;
|
|
275
197
|
}
|
|
276
|
-
resetUser(dispatch
|
|
198
|
+
resetUser(dispatch);
|
|
277
199
|
} catch (error) {
|
|
278
|
-
onError(
|
|
279
|
-
|
|
200
|
+
onError(
|
|
201
|
+
error instanceof Error ? error : new Error(String(error))
|
|
202
|
+
);
|
|
203
|
+
resetUser(dispatch);
|
|
280
204
|
} finally {
|
|
281
205
|
dispatch({
|
|
282
206
|
type: SET_IS_LOADING,
|
|
@@ -284,19 +208,16 @@ async function setAutoLoginAction({
|
|
|
284
208
|
});
|
|
285
209
|
}
|
|
286
210
|
}
|
|
287
|
-
async function
|
|
288
|
-
dispatch,
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
}
|
|
292
|
-
const storedRefreshToken = localStorage.getItem(storageKeys.refreshToken);
|
|
293
|
-
if (storedRefreshToken && domainName) {
|
|
294
|
-
try {
|
|
295
|
-
await logoutAPI(domainName, storedRefreshToken);
|
|
296
|
-
} catch (_) {
|
|
297
|
-
}
|
|
211
|
+
async function logoutAction(ctx) {
|
|
212
|
+
const { dispatch, authProxyPath } = ctx;
|
|
213
|
+
try {
|
|
214
|
+
await logoutAPI(authProxyPath);
|
|
215
|
+
} catch {
|
|
298
216
|
}
|
|
299
|
-
dispatch({
|
|
217
|
+
dispatch({
|
|
218
|
+
type: SET_IS_LOADING,
|
|
219
|
+
payload: { isLoading: true }
|
|
220
|
+
});
|
|
300
221
|
dispatch({
|
|
301
222
|
type: LOGIN,
|
|
302
223
|
payload: {
|
|
@@ -307,126 +228,73 @@ async function setLogoutAction({
|
|
|
307
228
|
isAuthenticated: false
|
|
308
229
|
}
|
|
309
230
|
});
|
|
310
|
-
|
|
311
|
-
localStorage.removeItem(storageKeys.refreshToken);
|
|
312
|
-
return dispatch({
|
|
231
|
+
dispatch({
|
|
313
232
|
type: SET_IS_LOADING,
|
|
314
233
|
payload: { isLoading: false }
|
|
315
234
|
});
|
|
316
235
|
}
|
|
317
|
-
async function
|
|
318
|
-
dispatch,
|
|
319
|
-
domainName,
|
|
320
|
-
storageKeys,
|
|
321
|
-
onError
|
|
322
|
-
}) {
|
|
323
|
-
const storedRefreshToken = localStorage.getItem(storageKeys.refreshToken);
|
|
324
|
-
if (!storedRefreshToken) {
|
|
325
|
-
return resetUser(dispatch, storageKeys);
|
|
326
|
-
}
|
|
327
|
-
try {
|
|
328
|
-
const refreshResult = await refreshTokenAPI(domainName, storedRefreshToken);
|
|
329
|
-
if (refreshResult.response.status === 200) {
|
|
330
|
-
localStorage.setItem(
|
|
331
|
-
storageKeys.accessToken,
|
|
332
|
-
refreshResult.data.accessToken
|
|
333
|
-
);
|
|
334
|
-
localStorage.setItem(
|
|
335
|
-
storageKeys.refreshToken,
|
|
336
|
-
refreshResult.data.refreshToken
|
|
337
|
-
);
|
|
338
|
-
return;
|
|
339
|
-
}
|
|
340
|
-
resetUser(dispatch, storageKeys);
|
|
341
|
-
} catch (error) {
|
|
342
|
-
onError(error instanceof Error ? error : new Error(String(error)));
|
|
343
|
-
resetUser(dispatch, storageKeys);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
async function setUpdateUserAction({
|
|
347
|
-
dispatch,
|
|
348
|
-
domainName,
|
|
349
|
-
user,
|
|
350
|
-
token,
|
|
351
|
-
onError
|
|
352
|
-
}) {
|
|
236
|
+
async function updateUserAction(ctx, user) {
|
|
237
|
+
const { dispatch, authProxyPath, onError } = ctx;
|
|
353
238
|
if (user.language) {
|
|
354
|
-
window.document.documentElement.setAttribute(
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
type: UPDATE_USER,
|
|
359
|
-
payload: user
|
|
360
|
-
});
|
|
361
|
-
return false;
|
|
239
|
+
window.document.documentElement.setAttribute(
|
|
240
|
+
"lang",
|
|
241
|
+
user.language
|
|
242
|
+
);
|
|
362
243
|
}
|
|
363
244
|
try {
|
|
364
|
-
const
|
|
365
|
-
if (
|
|
245
|
+
const result = await updateUserAPI(authProxyPath, user);
|
|
246
|
+
if (result.response.status === 200) {
|
|
366
247
|
dispatch({
|
|
367
248
|
type: UPDATE_USER,
|
|
368
|
-
payload:
|
|
249
|
+
payload: result.data.user
|
|
369
250
|
});
|
|
370
251
|
return true;
|
|
371
|
-
} else {
|
|
372
|
-
onError(new Error("Update user error: " + getUserFetch.data.message));
|
|
373
|
-
return false;
|
|
374
252
|
}
|
|
253
|
+
onError(
|
|
254
|
+
new Error("Update user error: " + result.data.message)
|
|
255
|
+
);
|
|
256
|
+
return false;
|
|
375
257
|
} catch (error) {
|
|
376
|
-
onError(
|
|
258
|
+
onError(
|
|
259
|
+
error instanceof Error ? error : new Error("Update user error")
|
|
260
|
+
);
|
|
377
261
|
return false;
|
|
378
262
|
}
|
|
379
263
|
}
|
|
380
|
-
async function
|
|
381
|
-
|
|
382
|
-
domainName,
|
|
383
|
-
storageKeys,
|
|
384
|
-
onError
|
|
385
|
-
}) {
|
|
386
|
-
const token_ls = localStorage.getItem(storageKeys.accessToken);
|
|
387
|
-
if (!token_ls) return "token-not-found";
|
|
264
|
+
async function updateUserWithRedirectAction(ctx) {
|
|
265
|
+
const { authProxyPath, onError } = ctx;
|
|
388
266
|
try {
|
|
389
|
-
const
|
|
390
|
-
if (
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
if (expiresIn < 5 * 60 * 1e3) {
|
|
394
|
-
await refreshSessionAction({
|
|
395
|
-
dispatch,
|
|
396
|
-
domainName,
|
|
397
|
-
storageKeys,
|
|
398
|
-
onError
|
|
399
|
-
});
|
|
400
|
-
const refreshedToken = localStorage.getItem(storageKeys.accessToken);
|
|
401
|
-
return refreshedToken || "token-not-found";
|
|
402
|
-
}
|
|
267
|
+
const result = await profileRedirectAPI(authProxyPath);
|
|
268
|
+
if (result.response.status === 200 && result.data.redirectUrl) {
|
|
269
|
+
window.location.replace(result.data.redirectUrl);
|
|
270
|
+
return;
|
|
403
271
|
}
|
|
404
|
-
|
|
272
|
+
onError(
|
|
273
|
+
new Error("Could not generate profile redirect")
|
|
274
|
+
);
|
|
275
|
+
} catch (error) {
|
|
276
|
+
onError(
|
|
277
|
+
error instanceof Error ? error : new Error("Profile redirect error")
|
|
278
|
+
);
|
|
405
279
|
}
|
|
406
|
-
return token_ls;
|
|
407
280
|
}
|
|
408
|
-
async function deleteAccountAction({
|
|
409
|
-
dispatch,
|
|
410
|
-
domainName,
|
|
411
|
-
storageKeys,
|
|
412
|
-
onError,
|
|
413
|
-
token
|
|
414
|
-
}) {
|
|
281
|
+
async function deleteAccountAction(ctx) {
|
|
282
|
+
const { dispatch, authProxyPath, onError } = ctx;
|
|
415
283
|
try {
|
|
416
|
-
const result = await deleteAccountAPI(
|
|
284
|
+
const result = await deleteAccountAPI(authProxyPath);
|
|
417
285
|
if (result.response.status === 200) {
|
|
418
|
-
resetUser(dispatch
|
|
286
|
+
resetUser(dispatch);
|
|
419
287
|
return true;
|
|
420
288
|
}
|
|
421
289
|
return false;
|
|
422
290
|
} catch (error) {
|
|
423
|
-
onError(
|
|
291
|
+
onError(
|
|
292
|
+
error instanceof Error ? error : new Error("Delete account error")
|
|
293
|
+
);
|
|
424
294
|
return false;
|
|
425
295
|
}
|
|
426
296
|
}
|
|
427
|
-
var resetUser = (dispatch
|
|
428
|
-
localStorage.removeItem(storageKeys.accessToken);
|
|
429
|
-
localStorage.removeItem(storageKeys.refreshToken);
|
|
297
|
+
var resetUser = (dispatch) => {
|
|
430
298
|
return dispatch({
|
|
431
299
|
type: LOGIN,
|
|
432
300
|
payload: {
|
|
@@ -437,118 +305,87 @@ var resetUser = (dispatch, storageKeys) => {
|
|
|
437
305
|
});
|
|
438
306
|
};
|
|
439
307
|
|
|
308
|
+
// src/api/utils/config.ts
|
|
309
|
+
var serverDomain = "dauth.ovh";
|
|
310
|
+
var _dauthUrl;
|
|
311
|
+
function setDauthUrl(url) {
|
|
312
|
+
_dauthUrl = url?.replace(/\/+$/, "");
|
|
313
|
+
}
|
|
314
|
+
function checkIsLocalhost() {
|
|
315
|
+
if (typeof window === "undefined") return false;
|
|
316
|
+
const hostname = window.location.hostname;
|
|
317
|
+
return Boolean(
|
|
318
|
+
hostname === "localhost" || hostname === "[::1]" || hostname.match(
|
|
319
|
+
/(192)\.(168)\.(1)\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gm
|
|
320
|
+
) || hostname.match(
|
|
321
|
+
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
|
|
322
|
+
)
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
function getClientBasePath() {
|
|
326
|
+
if (_dauthUrl) return _dauthUrl;
|
|
327
|
+
const isLocalhost = checkIsLocalhost();
|
|
328
|
+
const clientPort = 5185;
|
|
329
|
+
const clientLocalUrl = `${window.location.protocol}//${window.location.hostname}:${clientPort}`;
|
|
330
|
+
const clientProdUrl = `https://${serverDomain}`;
|
|
331
|
+
return isLocalhost ? clientLocalUrl : clientProdUrl;
|
|
332
|
+
}
|
|
333
|
+
|
|
440
334
|
// src/constants.ts
|
|
441
|
-
var TOKEN_LS = "dauth_state";
|
|
442
|
-
var REFRESH_TOKEN_LS = "dauth_refresh_token";
|
|
443
335
|
var AUTH_CODE_PARAM = "code";
|
|
444
336
|
|
|
445
337
|
// src/api/utils/routes.ts
|
|
446
338
|
var routes = {
|
|
447
|
-
signin: "signin"
|
|
448
|
-
updateUser: "update-user"
|
|
339
|
+
signin: "signin"
|
|
449
340
|
};
|
|
450
341
|
|
|
451
342
|
// src/index.tsx
|
|
452
343
|
import { jsx } from "react/jsx-runtime";
|
|
453
344
|
var defaultOnError = (error) => console.error(error);
|
|
454
345
|
var DauthProvider = (props) => {
|
|
455
|
-
const {
|
|
456
|
-
|
|
457
|
-
|
|
346
|
+
const {
|
|
347
|
+
domainName,
|
|
348
|
+
children,
|
|
349
|
+
authProxyPath = "/api/auth",
|
|
350
|
+
onError,
|
|
351
|
+
env,
|
|
352
|
+
dauthUrl
|
|
353
|
+
} = props;
|
|
354
|
+
const [dauthState, dispatch] = useReducer(
|
|
355
|
+
userReducer,
|
|
356
|
+
initialDauthState_default
|
|
357
|
+
);
|
|
458
358
|
useEffect(() => {
|
|
459
359
|
setDauthUrl(dauthUrl);
|
|
460
360
|
}, [dauthUrl]);
|
|
461
|
-
const storageKeys = useMemo(
|
|
462
|
-
() => ({
|
|
463
|
-
accessToken: storageKey?.accessToken ?? TOKEN_LS,
|
|
464
|
-
refreshToken: storageKey?.refreshToken ?? REFRESH_TOKEN_LS
|
|
465
|
-
}),
|
|
466
|
-
[storageKey?.accessToken, storageKey?.refreshToken]
|
|
467
|
-
);
|
|
468
361
|
const handleError = useCallback(
|
|
469
362
|
(error) => (onError ?? defaultOnError)(error),
|
|
470
363
|
[onError]
|
|
471
364
|
);
|
|
472
365
|
const ctx = useMemo(
|
|
473
|
-
() => ({ dispatch,
|
|
474
|
-
[
|
|
366
|
+
() => ({ dispatch, authProxyPath, onError: handleError }),
|
|
367
|
+
[authProxyPath, handleError]
|
|
475
368
|
);
|
|
476
|
-
const scheduleRefresh = useCallback(() => {
|
|
477
|
-
if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
|
|
478
|
-
const token = localStorage.getItem(storageKeys.accessToken);
|
|
479
|
-
if (!token) return;
|
|
480
|
-
try {
|
|
481
|
-
const payloadB64 = token.split(".")[1];
|
|
482
|
-
if (!payloadB64) return;
|
|
483
|
-
const payload = JSON.parse(atob(payloadB64));
|
|
484
|
-
const expiresIn = (payload.exp || 0) * 1e3 - Date.now();
|
|
485
|
-
const refreshIn = Math.max(expiresIn - 5 * 60 * 1e3, 1e4);
|
|
486
|
-
refreshTimerRef.current = setTimeout(async () => {
|
|
487
|
-
await refreshSessionAction(ctx);
|
|
488
|
-
scheduleRefresh();
|
|
489
|
-
}, refreshIn);
|
|
490
|
-
} catch (_) {
|
|
491
|
-
refreshTimerRef.current = setTimeout(
|
|
492
|
-
async () => {
|
|
493
|
-
await refreshSessionAction(ctx);
|
|
494
|
-
scheduleRefresh();
|
|
495
|
-
},
|
|
496
|
-
5 * 60 * 1e3
|
|
497
|
-
);
|
|
498
|
-
}
|
|
499
|
-
}, [ctx, storageKeys.accessToken]);
|
|
500
369
|
useEffect(() => {
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
return exchangeCodeAction({ ...ctx, code });
|
|
508
|
-
}
|
|
509
|
-
})();
|
|
510
|
-
}, []);
|
|
511
|
-
useEffect(() => {
|
|
512
|
-
(async () => {
|
|
513
|
-
const refreshToken = localStorage.getItem(storageKeys.refreshToken);
|
|
514
|
-
if (refreshToken && !dauthState.isAuthenticated) {
|
|
515
|
-
return setAutoLoginAction(ctx);
|
|
516
|
-
} else {
|
|
517
|
-
return dispatch({
|
|
518
|
-
type: SET_IS_LOADING,
|
|
519
|
-
payload: { isLoading: false }
|
|
520
|
-
});
|
|
521
|
-
}
|
|
522
|
-
})();
|
|
523
|
-
}, []);
|
|
524
|
-
useEffect(() => {
|
|
525
|
-
if (dauthState.isAuthenticated) {
|
|
526
|
-
scheduleRefresh();
|
|
370
|
+
const params = new URLSearchParams(window.location.search);
|
|
371
|
+
const code = params.get(AUTH_CODE_PARAM);
|
|
372
|
+
if (code) {
|
|
373
|
+
exchangeCodeAction(ctx, code);
|
|
374
|
+
} else {
|
|
375
|
+
autoLoginAction(ctx);
|
|
527
376
|
}
|
|
528
|
-
|
|
529
|
-
if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
|
|
530
|
-
};
|
|
531
|
-
}, [dauthState.isAuthenticated, scheduleRefresh]);
|
|
377
|
+
}, []);
|
|
532
378
|
const loginWithRedirect = useCallback(() => {
|
|
533
379
|
const base = `${getClientBasePath()}/${domainName}/${routes.signin}`;
|
|
534
380
|
const url = env ? `${base}?env=${encodeURIComponent(env)}` : base;
|
|
535
381
|
return window.location.replace(url);
|
|
536
382
|
}, [domainName, env]);
|
|
537
|
-
const logout = useCallback(
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
domainName,
|
|
542
|
-
storageKeys
|
|
543
|
-
});
|
|
544
|
-
}, [domainName, storageKeys]);
|
|
545
|
-
const getAccessToken = useCallback(async () => {
|
|
546
|
-
const token = await getAccessTokenAction(ctx);
|
|
547
|
-
return token;
|
|
548
|
-
}, [ctx]);
|
|
383
|
+
const logout = useCallback(
|
|
384
|
+
() => logoutAction(ctx),
|
|
385
|
+
[ctx]
|
|
386
|
+
);
|
|
549
387
|
const updateUser = useCallback(
|
|
550
388
|
async (fields) => {
|
|
551
|
-
const token_ls = localStorage.getItem(storageKeys.accessToken);
|
|
552
389
|
const {
|
|
553
390
|
name,
|
|
554
391
|
lastname,
|
|
@@ -573,36 +410,23 @@ var DauthProvider = (props) => {
|
|
|
573
410
|
country,
|
|
574
411
|
metadata
|
|
575
412
|
};
|
|
576
|
-
return
|
|
577
|
-
...ctx,
|
|
578
|
-
user,
|
|
579
|
-
token: token_ls
|
|
580
|
-
});
|
|
413
|
+
return updateUserAction(ctx, user);
|
|
581
414
|
},
|
|
582
|
-
[ctx
|
|
415
|
+
[ctx]
|
|
416
|
+
);
|
|
417
|
+
const updateUserWithRedirect = useCallback(
|
|
418
|
+
() => updateUserWithRedirectAction(ctx),
|
|
419
|
+
[ctx]
|
|
420
|
+
);
|
|
421
|
+
const deleteAccount = useCallback(
|
|
422
|
+
() => deleteAccountAction(ctx),
|
|
423
|
+
[ctx]
|
|
583
424
|
);
|
|
584
|
-
const updateUserWithRedirect = useCallback(() => {
|
|
585
|
-
const token_ls = localStorage.getItem(storageKeys.accessToken);
|
|
586
|
-
if (!token_ls) return;
|
|
587
|
-
return window.location.replace(
|
|
588
|
-
`${getClientBasePath()}/${domainName}/${routes.updateUser}/${token_ls}`
|
|
589
|
-
);
|
|
590
|
-
}, [domainName, storageKeys.accessToken]);
|
|
591
|
-
const deleteAccount = useCallback(async () => {
|
|
592
|
-
const token_ls = localStorage.getItem(storageKeys.accessToken);
|
|
593
|
-
if (!token_ls) return false;
|
|
594
|
-
if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
|
|
595
|
-
return await deleteAccountAction({
|
|
596
|
-
...ctx,
|
|
597
|
-
token: token_ls
|
|
598
|
-
});
|
|
599
|
-
}, [ctx, storageKeys.accessToken]);
|
|
600
425
|
const memoProvider = useMemo(
|
|
601
426
|
() => ({
|
|
602
427
|
...dauthState,
|
|
603
428
|
loginWithRedirect,
|
|
604
429
|
logout,
|
|
605
|
-
getAccessToken,
|
|
606
430
|
updateUser,
|
|
607
431
|
updateUserWithRedirect,
|
|
608
432
|
deleteAccount
|
|
@@ -611,7 +435,6 @@ var DauthProvider = (props) => {
|
|
|
611
435
|
dauthState,
|
|
612
436
|
loginWithRedirect,
|
|
613
437
|
logout,
|
|
614
|
-
getAccessToken,
|
|
615
438
|
updateUser,
|
|
616
439
|
updateUserWithRedirect,
|
|
617
440
|
deleteAccount
|