dauth-context-react 6.3.0 → 6.5.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.mjs CHANGED
@@ -11,7 +11,8 @@ import {
11
11
  // src/initialDauthState.ts
12
12
  var initialDauthState = {
13
13
  user: {
14
- language: (typeof window !== "undefined" ? window.document.documentElement.getAttribute("lang") : null) || "es"
14
+ language: (typeof window !== "undefined" ? window.document.documentElement.getAttribute("lang") : null) || "es",
15
+ theme: "system"
15
16
  },
16
17
  domain: {},
17
18
  isLoading: true,
@@ -24,7 +25,8 @@ var initialDauthState = {
24
25
  deleteAccount: () => Promise.resolve(false),
25
26
  getPasskeyCredentials: () => Promise.resolve([]),
26
27
  registerPasskey: () => Promise.resolve(null),
27
- deletePasskeyCredential: () => Promise.resolve(false)
28
+ deletePasskeyCredential: () => Promise.resolve(false),
29
+ uploadAvatar: () => Promise.resolve(false)
28
30
  };
29
31
  var initialDauthState_default = initialDauthState;
30
32
 
@@ -186,6 +188,18 @@ async function deletePasskeyCredentialAPI(basePath, credentialId) {
186
188
  const data = await response.json();
187
189
  return { response, data };
188
190
  }
191
+ async function uploadAvatarAPI(basePath, file) {
192
+ const formData = new FormData();
193
+ formData.append("avatar", file);
194
+ const response = await fetch(`${basePath}/avatar`, {
195
+ method: "POST",
196
+ headers: { "X-CSRF-Token": getCsrfToken() },
197
+ credentials: "include",
198
+ body: formData
199
+ });
200
+ const data = await response.json();
201
+ return { response, data };
202
+ }
189
203
 
190
204
  // src/webauthn.ts
191
205
  function base64urlToBuffer(base64url) {
@@ -441,6 +455,30 @@ async function deletePasskeyCredentialAction(ctx, credentialId) {
441
455
  return false;
442
456
  }
443
457
  }
458
+ async function uploadAvatarAction(ctx, file) {
459
+ const { dispatch, authProxyPath, onError } = ctx;
460
+ try {
461
+ const result = await uploadAvatarAPI(authProxyPath, file);
462
+ if (result.response.status === 200) {
463
+ dispatch({
464
+ type: UPDATE_USER,
465
+ payload: result.data.user
466
+ });
467
+ return true;
468
+ }
469
+ onError(
470
+ new Error(
471
+ "Avatar upload error: " + result.data.message
472
+ )
473
+ );
474
+ return false;
475
+ } catch (error) {
476
+ onError(
477
+ error instanceof Error ? error : new Error("Avatar upload error")
478
+ );
479
+ return false;
480
+ }
481
+ }
444
482
  var resetUser = (dispatch) => {
445
483
  return dispatch({
446
484
  type: LOGIN,
@@ -750,7 +788,8 @@ function DauthProfileModal({
750
788
  logout,
751
789
  getPasskeyCredentials,
752
790
  registerPasskey,
753
- deletePasskeyCredential
791
+ deletePasskeyCredential,
792
+ uploadAvatar
754
793
  } = useDauth();
755
794
  const isDesktop = useMediaQuery("(min-width: 641px)");
756
795
  const phase = useModalAnimation(open);
@@ -958,19 +997,21 @@ function DauthProfileModal({
958
997
  [deletePasskeyCredential]
959
998
  );
960
999
  const handleAvatarClick = useCallback(() => {
961
- if (onAvatarUpload) {
962
- avatarInputRef.current?.click();
963
- }
964
- }, [onAvatarUpload]);
1000
+ avatarInputRef.current?.click();
1001
+ }, []);
965
1002
  const handleAvatarChange = useCallback(
966
1003
  async (e) => {
967
1004
  const file = e.target.files?.[0];
968
- if (!file || !onAvatarUpload) return;
1005
+ if (!file) return;
969
1006
  setUploadingAvatar(true);
970
1007
  try {
971
- const url = await onAvatarUpload(file);
972
- if (url) {
973
- await updateUser({ avatar: url });
1008
+ if (onAvatarUpload) {
1009
+ const url = await onAvatarUpload(file);
1010
+ if (url) {
1011
+ await updateUser({ avatar: url });
1012
+ }
1013
+ } else {
1014
+ await uploadAvatar(file);
974
1015
  }
975
1016
  } catch {
976
1017
  }
@@ -979,7 +1020,7 @@ function DauthProfileModal({
979
1020
  avatarInputRef.current.value = "";
980
1021
  }
981
1022
  },
982
- [onAvatarUpload, updateUser]
1023
+ [onAvatarUpload, updateUser, uploadAvatar]
983
1024
  );
984
1025
  const handleSignOut = useCallback(() => {
985
1026
  logout();
@@ -1121,7 +1162,7 @@ function DauthProfileModal({
1121
1162
  {
1122
1163
  style: {
1123
1164
  ...avatarCircle,
1124
- cursor: onAvatarUpload ? "pointer" : "default",
1165
+ cursor: "pointer",
1125
1166
  position: "relative"
1126
1167
  },
1127
1168
  onClick: handleAvatarClick,
@@ -1138,12 +1179,12 @@ function DauthProfileModal({
1138
1179
  }
1139
1180
  }
1140
1181
  ) : avatarInitial,
1141
- onAvatarUpload && !uploadingAvatar && /* @__PURE__ */ jsx("div", { style: avatarOverlay, children: /* @__PURE__ */ jsx(IconCamera, {}) })
1182
+ !uploadingAvatar && /* @__PURE__ */ jsx("div", { style: avatarOverlay, children: /* @__PURE__ */ jsx(IconCamera, {}) })
1142
1183
  ]
1143
1184
  }
1144
1185
  ),
1145
1186
  /* @__PURE__ */ jsx("div", { style: emailText, children: user.email }),
1146
- onAvatarUpload && /* @__PURE__ */ jsx(
1187
+ /* @__PURE__ */ jsx(
1147
1188
  "input",
1148
1189
  {
1149
1190
  ref: avatarInputRef,
@@ -2176,10 +2217,12 @@ var DauthProvider = (props) => {
2176
2217
  telPrefix,
2177
2218
  telSuffix,
2178
2219
  language,
2220
+ theme,
2179
2221
  avatar,
2180
2222
  birthDate,
2181
2223
  country,
2182
- metadata
2224
+ metadata,
2225
+ customFields
2183
2226
  } = fields;
2184
2227
  const user = {
2185
2228
  name,
@@ -2188,10 +2231,12 @@ var DauthProvider = (props) => {
2188
2231
  telPrefix,
2189
2232
  telSuffix,
2190
2233
  language,
2234
+ theme,
2191
2235
  avatar,
2192
2236
  birthDate,
2193
2237
  country,
2194
- metadata
2238
+ metadata,
2239
+ customFields
2195
2240
  };
2196
2241
  return updateUserAction(ctx, user);
2197
2242
  },
@@ -2213,6 +2258,10 @@ var DauthProvider = (props) => {
2213
2258
  (credentialId) => deletePasskeyCredentialAction(ctx, credentialId),
2214
2259
  [ctx]
2215
2260
  );
2261
+ const uploadAvatar = useCallback2(
2262
+ (file) => uploadAvatarAction(ctx, file),
2263
+ [ctx]
2264
+ );
2216
2265
  const memoProvider = useMemo2(
2217
2266
  () => ({
2218
2267
  ...dauthState,
@@ -2222,7 +2271,8 @@ var DauthProvider = (props) => {
2222
2271
  deleteAccount,
2223
2272
  getPasskeyCredentials,
2224
2273
  registerPasskey,
2225
- deletePasskeyCredential
2274
+ deletePasskeyCredential,
2275
+ uploadAvatar
2226
2276
  }),
2227
2277
  [
2228
2278
  dauthState,
@@ -2232,7 +2282,8 @@ var DauthProvider = (props) => {
2232
2282
  deleteAccount,
2233
2283
  getPasskeyCredentials,
2234
2284
  registerPasskey,
2235
- deletePasskeyCredential
2285
+ deletePasskeyCredential,
2286
+ uploadAvatar
2236
2287
  ]
2237
2288
  );
2238
2289
  return /* @__PURE__ */ jsx2(DauthContext.Provider, { value: memoProvider, children });