dauth-context-react 4.0.4 → 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 +198 -370
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +199 -372
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -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 -126
- package/src/initialDauthState.ts +0 -1
- package/src/interfaces.ts +8 -12
- package/src/reducer/dauth.actions.ts +114 -189
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,162 +85,108 @@ 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 }
|
|
219
165
|
});
|
|
220
166
|
try {
|
|
221
|
-
window.history.replaceState(
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
localStorage.setItem(storageKeys.refreshToken, refreshToken);
|
|
229
|
-
const getUserFetch = await getUserAPI(domainName, accessToken);
|
|
230
|
-
if (getUserFetch.response.status === 200) {
|
|
167
|
+
window.history.replaceState(
|
|
168
|
+
{},
|
|
169
|
+
document.title,
|
|
170
|
+
window.location.pathname
|
|
171
|
+
);
|
|
172
|
+
const result = await exchangeCodeAPI(authProxyPath, code);
|
|
173
|
+
if (result.response.status === 200) {
|
|
231
174
|
dispatch({
|
|
232
175
|
type: LOGIN,
|
|
233
176
|
payload: {
|
|
234
|
-
user:
|
|
235
|
-
domain:
|
|
177
|
+
user: result.data.user,
|
|
178
|
+
domain: result.data.domain,
|
|
236
179
|
isAuthenticated: true
|
|
237
180
|
}
|
|
238
181
|
});
|
|
239
182
|
return;
|
|
240
183
|
}
|
|
241
|
-
|
|
184
|
+
resetUser(dispatch);
|
|
242
185
|
} catch (error) {
|
|
243
|
-
onError(
|
|
244
|
-
|
|
186
|
+
onError(
|
|
187
|
+
error instanceof Error ? error : new Error(String(error))
|
|
188
|
+
);
|
|
189
|
+
resetUser(dispatch);
|
|
245
190
|
} finally {
|
|
246
191
|
dispatch({
|
|
247
192
|
type: SET_IS_LOADING,
|
|
@@ -249,45 +194,31 @@ async function exchangeCodeAction({
|
|
|
249
194
|
});
|
|
250
195
|
}
|
|
251
196
|
}
|
|
252
|
-
async function
|
|
253
|
-
dispatch,
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
})
|
|
258
|
-
dispatch({ type: SET_IS_LOADING, payload: { isLoading: true } });
|
|
259
|
-
const storedRefreshToken = localStorage.getItem(storageKeys.refreshToken);
|
|
260
|
-
if (!storedRefreshToken) {
|
|
261
|
-
dispatch({
|
|
262
|
-
type: SET_IS_LOADING,
|
|
263
|
-
payload: { isLoading: false }
|
|
264
|
-
});
|
|
265
|
-
return resetUser(dispatch, storageKeys);
|
|
266
|
-
}
|
|
197
|
+
async function autoLoginAction(ctx) {
|
|
198
|
+
const { dispatch, authProxyPath, onError } = ctx;
|
|
199
|
+
dispatch({
|
|
200
|
+
type: SET_IS_LOADING,
|
|
201
|
+
payload: { isLoading: true }
|
|
202
|
+
});
|
|
267
203
|
try {
|
|
268
|
-
const
|
|
269
|
-
if (
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
user: getUserFetch.data.user,
|
|
280
|
-
domain: getUserFetch.data.domain,
|
|
281
|
-
isAuthenticated: true
|
|
282
|
-
}
|
|
283
|
-
});
|
|
284
|
-
return;
|
|
285
|
-
}
|
|
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;
|
|
286
215
|
}
|
|
287
|
-
resetUser(dispatch
|
|
216
|
+
resetUser(dispatch);
|
|
288
217
|
} catch (error) {
|
|
289
|
-
onError(
|
|
290
|
-
|
|
218
|
+
onError(
|
|
219
|
+
error instanceof Error ? error : new Error(String(error))
|
|
220
|
+
);
|
|
221
|
+
resetUser(dispatch);
|
|
291
222
|
} finally {
|
|
292
223
|
dispatch({
|
|
293
224
|
type: SET_IS_LOADING,
|
|
@@ -295,19 +226,16 @@ async function setAutoLoginAction({
|
|
|
295
226
|
});
|
|
296
227
|
}
|
|
297
228
|
}
|
|
298
|
-
async function
|
|
299
|
-
dispatch,
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
303
|
-
const storedRefreshToken = localStorage.getItem(storageKeys.refreshToken);
|
|
304
|
-
if (storedRefreshToken && domainName) {
|
|
305
|
-
try {
|
|
306
|
-
await logoutAPI(domainName, storedRefreshToken);
|
|
307
|
-
} catch (_) {
|
|
308
|
-
}
|
|
229
|
+
async function logoutAction(ctx) {
|
|
230
|
+
const { dispatch, authProxyPath } = ctx;
|
|
231
|
+
try {
|
|
232
|
+
await logoutAPI(authProxyPath);
|
|
233
|
+
} catch {
|
|
309
234
|
}
|
|
310
|
-
dispatch({
|
|
235
|
+
dispatch({
|
|
236
|
+
type: SET_IS_LOADING,
|
|
237
|
+
payload: { isLoading: true }
|
|
238
|
+
});
|
|
311
239
|
dispatch({
|
|
312
240
|
type: LOGIN,
|
|
313
241
|
payload: {
|
|
@@ -318,126 +246,73 @@ async function setLogoutAction({
|
|
|
318
246
|
isAuthenticated: false
|
|
319
247
|
}
|
|
320
248
|
});
|
|
321
|
-
|
|
322
|
-
localStorage.removeItem(storageKeys.refreshToken);
|
|
323
|
-
return dispatch({
|
|
249
|
+
dispatch({
|
|
324
250
|
type: SET_IS_LOADING,
|
|
325
251
|
payload: { isLoading: false }
|
|
326
252
|
});
|
|
327
253
|
}
|
|
328
|
-
async function
|
|
329
|
-
dispatch,
|
|
330
|
-
domainName,
|
|
331
|
-
storageKeys,
|
|
332
|
-
onError
|
|
333
|
-
}) {
|
|
334
|
-
const storedRefreshToken = localStorage.getItem(storageKeys.refreshToken);
|
|
335
|
-
if (!storedRefreshToken) {
|
|
336
|
-
return resetUser(dispatch, storageKeys);
|
|
337
|
-
}
|
|
338
|
-
try {
|
|
339
|
-
const refreshResult = await refreshTokenAPI(domainName, storedRefreshToken);
|
|
340
|
-
if (refreshResult.response.status === 200) {
|
|
341
|
-
localStorage.setItem(
|
|
342
|
-
storageKeys.accessToken,
|
|
343
|
-
refreshResult.data.accessToken
|
|
344
|
-
);
|
|
345
|
-
localStorage.setItem(
|
|
346
|
-
storageKeys.refreshToken,
|
|
347
|
-
refreshResult.data.refreshToken
|
|
348
|
-
);
|
|
349
|
-
return;
|
|
350
|
-
}
|
|
351
|
-
resetUser(dispatch, storageKeys);
|
|
352
|
-
} catch (error) {
|
|
353
|
-
onError(error instanceof Error ? error : new Error(String(error)));
|
|
354
|
-
resetUser(dispatch, storageKeys);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
async function setUpdateUserAction({
|
|
358
|
-
dispatch,
|
|
359
|
-
domainName,
|
|
360
|
-
user,
|
|
361
|
-
token,
|
|
362
|
-
onError
|
|
363
|
-
}) {
|
|
254
|
+
async function updateUserAction(ctx, user) {
|
|
255
|
+
const { dispatch, authProxyPath, onError } = ctx;
|
|
364
256
|
if (user.language) {
|
|
365
|
-
window.document.documentElement.setAttribute(
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
type: UPDATE_USER,
|
|
370
|
-
payload: user
|
|
371
|
-
});
|
|
372
|
-
return false;
|
|
257
|
+
window.document.documentElement.setAttribute(
|
|
258
|
+
"lang",
|
|
259
|
+
user.language
|
|
260
|
+
);
|
|
373
261
|
}
|
|
374
262
|
try {
|
|
375
|
-
const
|
|
376
|
-
if (
|
|
263
|
+
const result = await updateUserAPI(authProxyPath, user);
|
|
264
|
+
if (result.response.status === 200) {
|
|
377
265
|
dispatch({
|
|
378
266
|
type: UPDATE_USER,
|
|
379
|
-
payload:
|
|
267
|
+
payload: result.data.user
|
|
380
268
|
});
|
|
381
269
|
return true;
|
|
382
|
-
} else {
|
|
383
|
-
onError(new Error("Update user error: " + getUserFetch.data.message));
|
|
384
|
-
return false;
|
|
385
270
|
}
|
|
271
|
+
onError(
|
|
272
|
+
new Error("Update user error: " + result.data.message)
|
|
273
|
+
);
|
|
274
|
+
return false;
|
|
386
275
|
} catch (error) {
|
|
387
|
-
onError(
|
|
276
|
+
onError(
|
|
277
|
+
error instanceof Error ? error : new Error("Update user error")
|
|
278
|
+
);
|
|
388
279
|
return false;
|
|
389
280
|
}
|
|
390
281
|
}
|
|
391
|
-
async function
|
|
392
|
-
|
|
393
|
-
domainName,
|
|
394
|
-
storageKeys,
|
|
395
|
-
onError
|
|
396
|
-
}) {
|
|
397
|
-
const token_ls = localStorage.getItem(storageKeys.accessToken);
|
|
398
|
-
if (!token_ls) return "token-not-found";
|
|
282
|
+
async function updateUserWithRedirectAction(ctx) {
|
|
283
|
+
const { authProxyPath, onError } = ctx;
|
|
399
284
|
try {
|
|
400
|
-
const
|
|
401
|
-
if (
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
if (expiresIn < 5 * 60 * 1e3) {
|
|
405
|
-
await refreshSessionAction({
|
|
406
|
-
dispatch,
|
|
407
|
-
domainName,
|
|
408
|
-
storageKeys,
|
|
409
|
-
onError
|
|
410
|
-
});
|
|
411
|
-
const refreshedToken = localStorage.getItem(storageKeys.accessToken);
|
|
412
|
-
return refreshedToken || "token-not-found";
|
|
413
|
-
}
|
|
285
|
+
const result = await profileRedirectAPI(authProxyPath);
|
|
286
|
+
if (result.response.status === 200 && result.data.redirectUrl) {
|
|
287
|
+
window.location.replace(result.data.redirectUrl);
|
|
288
|
+
return;
|
|
414
289
|
}
|
|
415
|
-
|
|
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
|
+
);
|
|
416
297
|
}
|
|
417
|
-
return token_ls;
|
|
418
298
|
}
|
|
419
|
-
async function deleteAccountAction({
|
|
420
|
-
dispatch,
|
|
421
|
-
domainName,
|
|
422
|
-
storageKeys,
|
|
423
|
-
onError,
|
|
424
|
-
token
|
|
425
|
-
}) {
|
|
299
|
+
async function deleteAccountAction(ctx) {
|
|
300
|
+
const { dispatch, authProxyPath, onError } = ctx;
|
|
426
301
|
try {
|
|
427
|
-
const result = await deleteAccountAPI(
|
|
302
|
+
const result = await deleteAccountAPI(authProxyPath);
|
|
428
303
|
if (result.response.status === 200) {
|
|
429
|
-
resetUser(dispatch
|
|
304
|
+
resetUser(dispatch);
|
|
430
305
|
return true;
|
|
431
306
|
}
|
|
432
307
|
return false;
|
|
433
308
|
} catch (error) {
|
|
434
|
-
onError(
|
|
309
|
+
onError(
|
|
310
|
+
error instanceof Error ? error : new Error("Delete account error")
|
|
311
|
+
);
|
|
435
312
|
return false;
|
|
436
313
|
}
|
|
437
314
|
}
|
|
438
|
-
var resetUser = (dispatch
|
|
439
|
-
localStorage.removeItem(storageKeys.accessToken);
|
|
440
|
-
localStorage.removeItem(storageKeys.refreshToken);
|
|
315
|
+
var resetUser = (dispatch) => {
|
|
441
316
|
return dispatch({
|
|
442
317
|
type: LOGIN,
|
|
443
318
|
payload: {
|
|
@@ -448,120 +323,87 @@ var resetUser = (dispatch, storageKeys) => {
|
|
|
448
323
|
});
|
|
449
324
|
};
|
|
450
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
|
+
|
|
451
352
|
// src/constants.ts
|
|
452
|
-
var TOKEN_LS = "dauth_state";
|
|
453
|
-
var REFRESH_TOKEN_LS = "dauth_refresh_token";
|
|
454
353
|
var AUTH_CODE_PARAM = "code";
|
|
455
354
|
|
|
456
355
|
// src/api/utils/routes.ts
|
|
457
356
|
var routes = {
|
|
458
|
-
signin: "signin"
|
|
459
|
-
updateUser: "update-user"
|
|
357
|
+
signin: "signin"
|
|
460
358
|
};
|
|
461
359
|
|
|
462
360
|
// src/index.tsx
|
|
463
361
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
464
362
|
var defaultOnError = (error) => console.error(error);
|
|
465
363
|
var DauthProvider = (props) => {
|
|
466
|
-
const {
|
|
467
|
-
|
|
468
|
-
|
|
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
|
+
);
|
|
469
376
|
(0, import_react.useEffect)(() => {
|
|
470
377
|
setDauthUrl(dauthUrl);
|
|
471
378
|
}, [dauthUrl]);
|
|
472
|
-
const storageKeys = (0, import_react.useMemo)(
|
|
473
|
-
() => ({
|
|
474
|
-
accessToken: storageKey?.accessToken ?? TOKEN_LS,
|
|
475
|
-
refreshToken: storageKey?.refreshToken ?? REFRESH_TOKEN_LS
|
|
476
|
-
}),
|
|
477
|
-
[storageKey?.accessToken, storageKey?.refreshToken]
|
|
478
|
-
);
|
|
479
379
|
const handleError = (0, import_react.useCallback)(
|
|
480
380
|
(error) => (onError ?? defaultOnError)(error),
|
|
481
381
|
[onError]
|
|
482
382
|
);
|
|
483
383
|
const ctx = (0, import_react.useMemo)(
|
|
484
|
-
() => ({ dispatch,
|
|
485
|
-
[
|
|
384
|
+
() => ({ dispatch, authProxyPath, onError: handleError }),
|
|
385
|
+
[authProxyPath, handleError]
|
|
486
386
|
);
|
|
487
|
-
const scheduleRefresh = (0, import_react.useCallback)(() => {
|
|
488
|
-
if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
|
|
489
|
-
const token = localStorage.getItem(storageKeys.accessToken);
|
|
490
|
-
if (!token) return;
|
|
491
|
-
try {
|
|
492
|
-
const payloadB64 = token.split(".")[1];
|
|
493
|
-
if (!payloadB64) return;
|
|
494
|
-
const payload = JSON.parse(atob(payloadB64));
|
|
495
|
-
const expiresIn = (payload.exp || 0) * 1e3 - Date.now();
|
|
496
|
-
const refreshIn = Math.max(expiresIn - 5 * 60 * 1e3, 1e4);
|
|
497
|
-
refreshTimerRef.current = setTimeout(async () => {
|
|
498
|
-
await refreshSessionAction(ctx);
|
|
499
|
-
scheduleRefresh();
|
|
500
|
-
}, refreshIn);
|
|
501
|
-
} catch (_) {
|
|
502
|
-
refreshTimerRef.current = setTimeout(
|
|
503
|
-
async () => {
|
|
504
|
-
await refreshSessionAction(ctx);
|
|
505
|
-
scheduleRefresh();
|
|
506
|
-
},
|
|
507
|
-
5 * 60 * 1e3
|
|
508
|
-
);
|
|
509
|
-
}
|
|
510
|
-
}, [ctx, storageKeys.accessToken]);
|
|
511
|
-
(0, import_react.useEffect)(() => {
|
|
512
|
-
(async () => {
|
|
513
|
-
const queryString = window.location.search;
|
|
514
|
-
if (!queryString) return;
|
|
515
|
-
const urlParams = new URLSearchParams(queryString);
|
|
516
|
-
const code = urlParams.get(AUTH_CODE_PARAM);
|
|
517
|
-
if (code && !dauthState.isAuthenticated) {
|
|
518
|
-
return exchangeCodeAction({ ...ctx, code });
|
|
519
|
-
}
|
|
520
|
-
})();
|
|
521
|
-
}, []);
|
|
522
|
-
(0, import_react.useEffect)(() => {
|
|
523
|
-
(async () => {
|
|
524
|
-
const urlParams = new URLSearchParams(window.location.search);
|
|
525
|
-
if (urlParams.get(AUTH_CODE_PARAM)) return;
|
|
526
|
-
const refreshToken = localStorage.getItem(storageKeys.refreshToken);
|
|
527
|
-
if (refreshToken && !dauthState.isAuthenticated) {
|
|
528
|
-
return setAutoLoginAction(ctx);
|
|
529
|
-
} else {
|
|
530
|
-
return dispatch({
|
|
531
|
-
type: SET_IS_LOADING,
|
|
532
|
-
payload: { isLoading: false }
|
|
533
|
-
});
|
|
534
|
-
}
|
|
535
|
-
})();
|
|
536
|
-
}, []);
|
|
537
387
|
(0, import_react.useEffect)(() => {
|
|
538
|
-
|
|
539
|
-
|
|
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);
|
|
540
394
|
}
|
|
541
|
-
|
|
542
|
-
if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
|
|
543
|
-
};
|
|
544
|
-
}, [dauthState.isAuthenticated, scheduleRefresh]);
|
|
395
|
+
}, []);
|
|
545
396
|
const loginWithRedirect = (0, import_react.useCallback)(() => {
|
|
546
397
|
const base = `${getClientBasePath()}/${domainName}/${routes.signin}`;
|
|
547
398
|
const url = env ? `${base}?env=${encodeURIComponent(env)}` : base;
|
|
548
399
|
return window.location.replace(url);
|
|
549
400
|
}, [domainName, env]);
|
|
550
|
-
const logout = (0, import_react.useCallback)(
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
domainName,
|
|
555
|
-
storageKeys
|
|
556
|
-
});
|
|
557
|
-
}, [domainName, storageKeys]);
|
|
558
|
-
const getAccessToken = (0, import_react.useCallback)(async () => {
|
|
559
|
-
const token = await getAccessTokenAction(ctx);
|
|
560
|
-
return token;
|
|
561
|
-
}, [ctx]);
|
|
401
|
+
const logout = (0, import_react.useCallback)(
|
|
402
|
+
() => logoutAction(ctx),
|
|
403
|
+
[ctx]
|
|
404
|
+
);
|
|
562
405
|
const updateUser = (0, import_react.useCallback)(
|
|
563
406
|
async (fields) => {
|
|
564
|
-
const token_ls = localStorage.getItem(storageKeys.accessToken);
|
|
565
407
|
const {
|
|
566
408
|
name,
|
|
567
409
|
lastname,
|
|
@@ -586,36 +428,23 @@ var DauthProvider = (props) => {
|
|
|
586
428
|
country,
|
|
587
429
|
metadata
|
|
588
430
|
};
|
|
589
|
-
return
|
|
590
|
-
...ctx,
|
|
591
|
-
user,
|
|
592
|
-
token: token_ls
|
|
593
|
-
});
|
|
431
|
+
return updateUserAction(ctx, user);
|
|
594
432
|
},
|
|
595
|
-
[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]
|
|
596
442
|
);
|
|
597
|
-
const updateUserWithRedirect = (0, import_react.useCallback)(() => {
|
|
598
|
-
const token_ls = localStorage.getItem(storageKeys.accessToken);
|
|
599
|
-
if (!token_ls) return;
|
|
600
|
-
return window.location.replace(
|
|
601
|
-
`${getClientBasePath()}/${domainName}/${routes.updateUser}/${token_ls}`
|
|
602
|
-
);
|
|
603
|
-
}, [domainName, storageKeys.accessToken]);
|
|
604
|
-
const deleteAccount = (0, import_react.useCallback)(async () => {
|
|
605
|
-
const token_ls = localStorage.getItem(storageKeys.accessToken);
|
|
606
|
-
if (!token_ls) return false;
|
|
607
|
-
if (refreshTimerRef.current) clearTimeout(refreshTimerRef.current);
|
|
608
|
-
return await deleteAccountAction({
|
|
609
|
-
...ctx,
|
|
610
|
-
token: token_ls
|
|
611
|
-
});
|
|
612
|
-
}, [ctx, storageKeys.accessToken]);
|
|
613
443
|
const memoProvider = (0, import_react.useMemo)(
|
|
614
444
|
() => ({
|
|
615
445
|
...dauthState,
|
|
616
446
|
loginWithRedirect,
|
|
617
447
|
logout,
|
|
618
|
-
getAccessToken,
|
|
619
448
|
updateUser,
|
|
620
449
|
updateUserWithRedirect,
|
|
621
450
|
deleteAccount
|
|
@@ -624,7 +453,6 @@ var DauthProvider = (props) => {
|
|
|
624
453
|
dauthState,
|
|
625
454
|
loginWithRedirect,
|
|
626
455
|
logout,
|
|
627
|
-
getAccessToken,
|
|
628
456
|
updateUser,
|
|
629
457
|
updateUserWithRedirect,
|
|
630
458
|
deleteAccount
|