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