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.d.mts CHANGED
@@ -1,5 +1,6 @@
1
1
  import React$1 from 'react';
2
2
 
3
+ type ThemeMode = 'dark' | 'light' | 'system';
3
4
  interface IDauthUser {
4
5
  _id: string;
5
6
  name: string;
@@ -8,6 +9,7 @@ interface IDauthUser {
8
9
  email: string;
9
10
  isVerified: boolean;
10
11
  language: string;
12
+ theme: ThemeMode;
11
13
  avatar: {
12
14
  id: string;
13
15
  url: string;
@@ -79,13 +81,14 @@ interface IDauthState {
79
81
  getPasskeyCredentials: () => Promise<IPasskeyCredential[]>;
80
82
  registerPasskey: (name?: string) => Promise<IPasskeyCredential | null>;
81
83
  deletePasskeyCredential: (credentialId: string) => Promise<boolean>;
84
+ uploadAvatar: (file: File) => Promise<boolean>;
82
85
  }
83
86
  interface DauthProfileModalProps {
84
87
  open: boolean;
85
88
  onClose: () => void;
86
- /** Optional: provide a function to handle avatar upload.
89
+ /** Optional override for avatar upload.
87
90
  * Receives a File, should return the URL string.
88
- * If not provided, the avatar edit button is hidden. */
91
+ * If not provided, uses the built-in upload via the auth proxy. */
89
92
  onAvatarUpload?: (file: File) => Promise<string>;
90
93
  }
91
94
  interface IDauthProviderProps {
@@ -104,4 +107,4 @@ declare function DauthProfileModal({ open, onClose, onAvatarUpload, }: DauthProf
104
107
  declare const DauthProvider: React$1.FC<IDauthProviderProps>;
105
108
  declare const useDauth: () => IDauthState;
106
109
 
107
- export { DauthProfileModal, type DauthProfileModalProps, DauthProvider, type ICustomField, type IDauthAuthMethods, type IDauthProviderProps, type IFormField, type IModalTheme, type IPasskeyCredential, useDauth };
110
+ export { DauthProfileModal, type DauthProfileModalProps, DauthProvider, type ICustomField, type IDauthAuthMethods, type IDauthProviderProps, type IFormField, type IModalTheme, type IPasskeyCredential, type ThemeMode, useDauth };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import React$1 from 'react';
2
2
 
3
+ type ThemeMode = 'dark' | 'light' | 'system';
3
4
  interface IDauthUser {
4
5
  _id: string;
5
6
  name: string;
@@ -8,6 +9,7 @@ interface IDauthUser {
8
9
  email: string;
9
10
  isVerified: boolean;
10
11
  language: string;
12
+ theme: ThemeMode;
11
13
  avatar: {
12
14
  id: string;
13
15
  url: string;
@@ -79,13 +81,14 @@ interface IDauthState {
79
81
  getPasskeyCredentials: () => Promise<IPasskeyCredential[]>;
80
82
  registerPasskey: (name?: string) => Promise<IPasskeyCredential | null>;
81
83
  deletePasskeyCredential: (credentialId: string) => Promise<boolean>;
84
+ uploadAvatar: (file: File) => Promise<boolean>;
82
85
  }
83
86
  interface DauthProfileModalProps {
84
87
  open: boolean;
85
88
  onClose: () => void;
86
- /** Optional: provide a function to handle avatar upload.
89
+ /** Optional override for avatar upload.
87
90
  * Receives a File, should return the URL string.
88
- * If not provided, the avatar edit button is hidden. */
91
+ * If not provided, uses the built-in upload via the auth proxy. */
89
92
  onAvatarUpload?: (file: File) => Promise<string>;
90
93
  }
91
94
  interface IDauthProviderProps {
@@ -104,4 +107,4 @@ declare function DauthProfileModal({ open, onClose, onAvatarUpload, }: DauthProf
104
107
  declare const DauthProvider: React$1.FC<IDauthProviderProps>;
105
108
  declare const useDauth: () => IDauthState;
106
109
 
107
- export { DauthProfileModal, type DauthProfileModalProps, DauthProvider, type ICustomField, type IDauthAuthMethods, type IDauthProviderProps, type IFormField, type IModalTheme, type IPasskeyCredential, useDauth };
110
+ export { DauthProfileModal, type DauthProfileModalProps, DauthProvider, type ICustomField, type IDauthAuthMethods, type IDauthProviderProps, type IFormField, type IModalTheme, type IPasskeyCredential, type ThemeMode, useDauth };
package/dist/index.js CHANGED
@@ -30,7 +30,8 @@ var import_react2 = require("react");
30
30
  // src/initialDauthState.ts
31
31
  var initialDauthState = {
32
32
  user: {
33
- language: (typeof window !== "undefined" ? window.document.documentElement.getAttribute("lang") : null) || "es"
33
+ language: (typeof window !== "undefined" ? window.document.documentElement.getAttribute("lang") : null) || "es",
34
+ theme: "system"
34
35
  },
35
36
  domain: {},
36
37
  isLoading: true,
@@ -43,7 +44,8 @@ var initialDauthState = {
43
44
  deleteAccount: () => Promise.resolve(false),
44
45
  getPasskeyCredentials: () => Promise.resolve([]),
45
46
  registerPasskey: () => Promise.resolve(null),
46
- deletePasskeyCredential: () => Promise.resolve(false)
47
+ deletePasskeyCredential: () => Promise.resolve(false),
48
+ uploadAvatar: () => Promise.resolve(false)
47
49
  };
48
50
  var initialDauthState_default = initialDauthState;
49
51
 
@@ -205,6 +207,18 @@ async function deletePasskeyCredentialAPI(basePath, credentialId) {
205
207
  const data = await response.json();
206
208
  return { response, data };
207
209
  }
210
+ async function uploadAvatarAPI(basePath, file) {
211
+ const formData = new FormData();
212
+ formData.append("avatar", file);
213
+ const response = await fetch(`${basePath}/avatar`, {
214
+ method: "POST",
215
+ headers: { "X-CSRF-Token": getCsrfToken() },
216
+ credentials: "include",
217
+ body: formData
218
+ });
219
+ const data = await response.json();
220
+ return { response, data };
221
+ }
208
222
 
209
223
  // src/webauthn.ts
210
224
  function base64urlToBuffer(base64url) {
@@ -460,6 +474,30 @@ async function deletePasskeyCredentialAction(ctx, credentialId) {
460
474
  return false;
461
475
  }
462
476
  }
477
+ async function uploadAvatarAction(ctx, file) {
478
+ const { dispatch, authProxyPath, onError } = ctx;
479
+ try {
480
+ const result = await uploadAvatarAPI(authProxyPath, file);
481
+ if (result.response.status === 200) {
482
+ dispatch({
483
+ type: UPDATE_USER,
484
+ payload: result.data.user
485
+ });
486
+ return true;
487
+ }
488
+ onError(
489
+ new Error(
490
+ "Avatar upload error: " + result.data.message
491
+ )
492
+ );
493
+ return false;
494
+ } catch (error) {
495
+ onError(
496
+ error instanceof Error ? error : new Error("Avatar upload error")
497
+ );
498
+ return false;
499
+ }
500
+ }
463
501
  var resetUser = (dispatch) => {
464
502
  return dispatch({
465
503
  type: LOGIN,
@@ -763,7 +801,8 @@ function DauthProfileModal({
763
801
  logout,
764
802
  getPasskeyCredentials,
765
803
  registerPasskey,
766
- deletePasskeyCredential
804
+ deletePasskeyCredential,
805
+ uploadAvatar
767
806
  } = useDauth();
768
807
  const isDesktop = useMediaQuery("(min-width: 641px)");
769
808
  const phase = useModalAnimation(open);
@@ -971,19 +1010,21 @@ function DauthProfileModal({
971
1010
  [deletePasskeyCredential]
972
1011
  );
973
1012
  const handleAvatarClick = (0, import_react.useCallback)(() => {
974
- if (onAvatarUpload) {
975
- avatarInputRef.current?.click();
976
- }
977
- }, [onAvatarUpload]);
1013
+ avatarInputRef.current?.click();
1014
+ }, []);
978
1015
  const handleAvatarChange = (0, import_react.useCallback)(
979
1016
  async (e) => {
980
1017
  const file = e.target.files?.[0];
981
- if (!file || !onAvatarUpload) return;
1018
+ if (!file) return;
982
1019
  setUploadingAvatar(true);
983
1020
  try {
984
- const url = await onAvatarUpload(file);
985
- if (url) {
986
- await updateUser({ avatar: url });
1021
+ if (onAvatarUpload) {
1022
+ const url = await onAvatarUpload(file);
1023
+ if (url) {
1024
+ await updateUser({ avatar: url });
1025
+ }
1026
+ } else {
1027
+ await uploadAvatar(file);
987
1028
  }
988
1029
  } catch {
989
1030
  }
@@ -992,7 +1033,7 @@ function DauthProfileModal({
992
1033
  avatarInputRef.current.value = "";
993
1034
  }
994
1035
  },
995
- [onAvatarUpload, updateUser]
1036
+ [onAvatarUpload, updateUser, uploadAvatar]
996
1037
  );
997
1038
  const handleSignOut = (0, import_react.useCallback)(() => {
998
1039
  logout();
@@ -1134,7 +1175,7 @@ function DauthProfileModal({
1134
1175
  {
1135
1176
  style: {
1136
1177
  ...avatarCircle,
1137
- cursor: onAvatarUpload ? "pointer" : "default",
1178
+ cursor: "pointer",
1138
1179
  position: "relative"
1139
1180
  },
1140
1181
  onClick: handleAvatarClick,
@@ -1151,12 +1192,12 @@ function DauthProfileModal({
1151
1192
  }
1152
1193
  }
1153
1194
  ) : avatarInitial,
1154
- onAvatarUpload && !uploadingAvatar && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: avatarOverlay, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(IconCamera, {}) })
1195
+ !uploadingAvatar && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: avatarOverlay, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(IconCamera, {}) })
1155
1196
  ]
1156
1197
  }
1157
1198
  ),
1158
1199
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: emailText, children: user.email }),
1159
- onAvatarUpload && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1200
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1160
1201
  "input",
1161
1202
  {
1162
1203
  ref: avatarInputRef,
@@ -2189,10 +2230,12 @@ var DauthProvider = (props) => {
2189
2230
  telPrefix,
2190
2231
  telSuffix,
2191
2232
  language,
2233
+ theme,
2192
2234
  avatar,
2193
2235
  birthDate,
2194
2236
  country,
2195
- metadata
2237
+ metadata,
2238
+ customFields
2196
2239
  } = fields;
2197
2240
  const user = {
2198
2241
  name,
@@ -2201,10 +2244,12 @@ var DauthProvider = (props) => {
2201
2244
  telPrefix,
2202
2245
  telSuffix,
2203
2246
  language,
2247
+ theme,
2204
2248
  avatar,
2205
2249
  birthDate,
2206
2250
  country,
2207
- metadata
2251
+ metadata,
2252
+ customFields
2208
2253
  };
2209
2254
  return updateUserAction(ctx, user);
2210
2255
  },
@@ -2226,6 +2271,10 @@ var DauthProvider = (props) => {
2226
2271
  (credentialId) => deletePasskeyCredentialAction(ctx, credentialId),
2227
2272
  [ctx]
2228
2273
  );
2274
+ const uploadAvatar = (0, import_react2.useCallback)(
2275
+ (file) => uploadAvatarAction(ctx, file),
2276
+ [ctx]
2277
+ );
2229
2278
  const memoProvider = (0, import_react2.useMemo)(
2230
2279
  () => ({
2231
2280
  ...dauthState,
@@ -2235,7 +2284,8 @@ var DauthProvider = (props) => {
2235
2284
  deleteAccount,
2236
2285
  getPasskeyCredentials,
2237
2286
  registerPasskey,
2238
- deletePasskeyCredential
2287
+ deletePasskeyCredential,
2288
+ uploadAvatar
2239
2289
  }),
2240
2290
  [
2241
2291
  dauthState,
@@ -2245,7 +2295,8 @@ var DauthProvider = (props) => {
2245
2295
  deleteAccount,
2246
2296
  getPasskeyCredentials,
2247
2297
  registerPasskey,
2248
- deletePasskeyCredential
2298
+ deletePasskeyCredential,
2299
+ uploadAvatar
2249
2300
  ]
2250
2301
  );
2251
2302
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(DauthContext.Provider, { value: memoProvider, children });