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