@nice2dev/auth 0.1.0 → 1.0.3

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
@@ -1,7 +1,7 @@
1
1
  import { useState as _, useCallback as A, forwardRef as T } from "react";
2
2
  import { jsxs as t, jsx as e, Fragment as q } from "react/jsx-runtime";
3
3
  function W(a = {}) {
4
- const { onLogin: n, onRefresh: d, onLogout: m, storageKey: h = "nice_auth", persistAuth: f = !1 } = a, [i, p] = _(() => {
4
+ const { onLogin: n, onRefresh: d, onLogout: u, storageKey: h = "nice_auth", persistAuth: f = !1 } = a, [i, m] = _(() => {
5
5
  if (f && typeof window < "u")
6
6
  try {
7
7
  const o = localStorage.getItem(h);
@@ -11,24 +11,24 @@ function W(a = {}) {
11
11
  }
12
12
  return { isAuthenticated: !1, user: null, tokens: null, loading: !1, error: null };
13
13
  }), s = A((o) => {
14
- p((l) => {
15
- const u = { ...l, ...o };
16
- return f && typeof window < "u" && (u.isAuthenticated ? localStorage.setItem(h, JSON.stringify({ isAuthenticated: !0, user: u.user, tokens: u.tokens })) : localStorage.removeItem(h)), u;
14
+ m((l) => {
15
+ const p = { ...l, ...o };
16
+ return f && typeof window < "u" && (p.isAuthenticated ? localStorage.setItem(h, JSON.stringify({ isAuthenticated: !0, user: p.user })) : localStorage.removeItem(h)), p;
17
17
  });
18
18
  }, [f, h]), w = A(async (o, l) => {
19
19
  s({ loading: !0, error: null });
20
20
  try {
21
21
  if (n) {
22
- const u = await n(o, l);
23
- return s({ isAuthenticated: !0, user: u.user, tokens: u.tokens, loading: !1 }), !0;
22
+ const p = await n(o, l);
23
+ return s({ isAuthenticated: !0, user: p.user, tokens: p.tokens, loading: !1 }), !0;
24
24
  }
25
25
  return s({ loading: !1, error: "No login handler configured" }), !1;
26
- } catch (u) {
27
- return s({ loading: !1, error: (u == null ? void 0 : u.message) ?? "Login failed" }), !1;
26
+ } catch (p) {
27
+ return s({ loading: !1, error: (p == null ? void 0 : p.message) ?? "Login failed" }), !1;
28
28
  }
29
29
  }, [n, s]), c = A(() => {
30
- m == null || m(), s({ isAuthenticated: !1, user: null, tokens: null, loading: !1, error: null });
31
- }, [m, s]), k = A(async () => {
30
+ u == null || u(), s({ isAuthenticated: !1, user: null, tokens: null, loading: !1, error: null });
31
+ }, [u, s]), k = A(async () => {
32
32
  var o;
33
33
  if (!((o = i.tokens) != null && o.refreshToken) || !d) return !1;
34
34
  try {
@@ -57,11 +57,11 @@ const V = T(({
57
57
  onSubmit: a,
58
58
  loading: n = !1,
59
59
  error: d,
60
- showRememberMe: m = !0,
60
+ showRememberMe: u = !0,
61
61
  showForgotPassword: h = !0,
62
62
  onForgotPassword: f,
63
63
  showRegisterLink: i = !0,
64
- onRegisterClick: p,
64
+ onRegisterClick: m,
65
65
  title: s = "Sign In",
66
66
  submitLabel: w = "Sign In",
67
67
  logo: c,
@@ -71,7 +71,7 @@ const V = T(({
71
71
  emailLabel: y = "Email",
72
72
  passwordLabel: $ = "Password"
73
73
  }, o) => {
74
- const [l, u] = _(""), [F, g] = _(""), [P, v] = _(!1), S = A((x) => {
74
+ const [l, p] = _(""), [F, g] = _(""), [P, v] = _(!1), S = A((x) => {
75
75
  x.preventDefault(), a == null || a({ email: l, password: F, rememberMe: P });
76
76
  }, [l, F, P, a]);
77
77
  return /* @__PURE__ */ t(
@@ -95,7 +95,7 @@ const V = T(({
95
95
  className: "nice-input",
96
96
  type: "email",
97
97
  value: l,
98
- onChange: (x) => u(x.target.value),
98
+ onChange: (x) => p(x.target.value),
99
99
  autoComplete: "email",
100
100
  required: !0,
101
101
  disabled: n
@@ -119,7 +119,7 @@ const V = T(({
119
119
  )
120
120
  ] }),
121
121
  /* @__PURE__ */ t("div", { className: "nice-login-form__options", children: [
122
- m && /* @__PURE__ */ t("label", { className: "nice-login-form__remember", children: [
122
+ u && /* @__PURE__ */ t("label", { className: "nice-login-form__remember", children: [
123
123
  /* @__PURE__ */ e("input", { type: "checkbox", checked: P, onChange: (x) => v(x.target.checked), disabled: n }),
124
124
  /* @__PURE__ */ e("span", { children: "Remember me" })
125
125
  ] }),
@@ -128,7 +128,7 @@ const V = T(({
128
128
  /* @__PURE__ */ e("button", { type: "submit", className: "nice-btn nice-btn--primary nice-login-form__submit", disabled: n || !l || !F, children: n ? "…" : w }),
129
129
  i && /* @__PURE__ */ t("div", { className: "nice-login-form__register", children: [
130
130
  /* @__PURE__ */ e("span", { children: "Don't have an account? " }),
131
- /* @__PURE__ */ e("button", { type: "button", onClick: p, disabled: n, children: "Register" })
131
+ /* @__PURE__ */ e("button", { type: "button", onClick: m, disabled: n, children: "Register" })
132
132
  ] }),
133
133
  k && /* @__PURE__ */ e("div", { className: "nice-login-form__footer", children: k })
134
134
  ]
@@ -158,13 +158,13 @@ const M = T(({
158
158
  password: a,
159
159
  strength: n,
160
160
  showLabel: d = !0,
161
- labels: m = j,
161
+ labels: u = j,
162
162
  className: h
163
163
  }, f) => {
164
- const i = n ?? I(a), p = B[i], s = (i + 1) / 5 * 100;
164
+ const i = n ?? I(a), m = B[i], s = (i + 1) / 5 * 100;
165
165
  return /* @__PURE__ */ t("div", { ref: f, className: `nice-password-strength${h ? ` ${h}` : ""}`, children: [
166
- /* @__PURE__ */ e("div", { className: "nice-password-strength__bar", children: /* @__PURE__ */ e("div", { className: "nice-password-strength__fill", style: { width: `${s}%`, backgroundColor: p } }) }),
167
- d && /* @__PURE__ */ e("span", { className: "nice-password-strength__label", style: { color: p }, children: m[i] })
166
+ /* @__PURE__ */ e("div", { className: "nice-password-strength__bar", children: /* @__PURE__ */ e("div", { className: "nice-password-strength__fill", style: { width: `${s}%`, backgroundColor: m } }) }),
167
+ d && /* @__PURE__ */ e("span", { className: "nice-password-strength__label", style: { color: m }, children: u[i] })
168
168
  ] });
169
169
  });
170
170
  M.displayName = "NicePasswordStrength";
@@ -172,20 +172,20 @@ const G = T(({
172
172
  onSubmit: a,
173
173
  loading: n = !1,
174
174
  error: d,
175
- onLoginClick: m,
175
+ onLoginClick: u,
176
176
  showPasswordStrength: h = !0,
177
177
  minPasswordStrength: f = 2,
178
178
  termsUrl: i,
179
- privacyUrl: p,
179
+ privacyUrl: m,
180
180
  title: s = "Create Account",
181
181
  logo: w,
182
182
  footer: c,
183
183
  className: k,
184
184
  style: b
185
185
  }, C) => {
186
- const [y, $] = _(""), [o, l] = _(""), [u, F] = _(""), [g, P] = _(""), [v, S] = _(""), [x, D] = _(!1), L = I(g), E = g === v, R = y && u && g && E && x && L >= f && !n, r = A((N) => {
187
- N.preventDefault(), R && (a == null || a({ firstName: y, lastName: o, email: u, password: g, acceptTerms: x }));
188
- }, [R, y, o, u, g, x, a]);
186
+ const [y, $] = _(""), [o, l] = _(""), [p, F] = _(""), [g, P] = _(""), [v, S] = _(""), [x, D] = _(!1), L = I(g), E = g === v, R = y && p && g && E && x && L >= f && !n, r = A((N) => {
187
+ N.preventDefault(), R && (a == null || a({ firstName: y, lastName: o, email: p, password: g, acceptTerms: x }));
188
+ }, [R, y, o, p, g, x, a]);
189
189
  return /* @__PURE__ */ t(
190
190
  "form",
191
191
  {
@@ -210,7 +210,7 @@ const G = T(({
210
210
  ] }),
211
211
  /* @__PURE__ */ t("div", { className: "nice-register-form__field", children: [
212
212
  /* @__PURE__ */ e("label", { htmlFor: "nice-reg-email", children: "Email *" }),
213
- /* @__PURE__ */ e("input", { id: "nice-reg-email", className: "nice-input", type: "email", value: u, onChange: (N) => F(N.target.value), required: !0, disabled: n, autoComplete: "email" })
213
+ /* @__PURE__ */ e("input", { id: "nice-reg-email", className: "nice-input", type: "email", value: p, onChange: (N) => F(N.target.value), required: !0, disabled: n, autoComplete: "email" })
214
214
  ] }),
215
215
  /* @__PURE__ */ t("div", { className: "nice-register-form__field", children: [
216
216
  /* @__PURE__ */ e("label", { htmlFor: "nice-reg-pass", children: "Password *" }),
@@ -228,17 +228,17 @@ const G = T(({
228
228
  "I accept the",
229
229
  " ",
230
230
  i ? /* @__PURE__ */ e("a", { href: i, target: "_blank", rel: "noopener noreferrer", children: "Terms" }) : "Terms",
231
- p && /* @__PURE__ */ t(q, { children: [
231
+ m && /* @__PURE__ */ t(q, { children: [
232
232
  " and ",
233
- /* @__PURE__ */ e("a", { href: p, target: "_blank", rel: "noopener noreferrer", children: "Privacy Policy" })
233
+ /* @__PURE__ */ e("a", { href: m, target: "_blank", rel: "noopener noreferrer", children: "Privacy Policy" })
234
234
  ] }),
235
235
  " *"
236
236
  ] })
237
237
  ] }),
238
238
  /* @__PURE__ */ e("button", { type: "submit", className: "nice-btn nice-btn--primary nice-register-form__submit", disabled: !R, children: n ? "…" : "Register" }),
239
- m && /* @__PURE__ */ t("div", { className: "nice-register-form__login", children: [
239
+ u && /* @__PURE__ */ t("div", { className: "nice-register-form__login", children: [
240
240
  /* @__PURE__ */ e("span", { children: "Already have an account? " }),
241
- /* @__PURE__ */ e("button", { type: "button", onClick: m, children: "Sign in" })
241
+ /* @__PURE__ */ e("button", { type: "button", onClick: u, children: "Sign in" })
242
242
  ] }),
243
243
  c && /* @__PURE__ */ e("div", { className: "nice-register-form__footer", children: c })
244
244
  ]
@@ -250,30 +250,30 @@ const Q = T(({
250
250
  requireCurrentPassword: a = !0,
251
251
  onSubmit: n,
252
252
  loading: d = !1,
253
- error: m,
253
+ error: u,
254
254
  success: h,
255
255
  minPasswordStrength: f = 2,
256
256
  title: i = "Change Password",
257
- className: p,
257
+ className: m,
258
258
  style: s
259
259
  }, w) => {
260
- const [c, k] = _(""), [b, C] = _(""), [y, $] = _(""), o = I(b), l = b === y, u = (!a || c.length > 0) && b.length > 0 && l && o >= f && !d, F = A((g) => {
261
- g.preventDefault(), u && (n == null || n({
260
+ const [c, k] = _(""), [b, C] = _(""), [y, $] = _(""), o = I(b), l = b === y, p = (!a || c.length > 0) && b.length > 0 && l && o >= f && !d, F = A((g) => {
261
+ g.preventDefault(), p && (n == null || n({
262
262
  currentPassword: a ? c : void 0,
263
263
  newPassword: b
264
264
  }));
265
- }, [u, c, b, a, n]);
265
+ }, [p, c, b, a, n]);
266
266
  return /* @__PURE__ */ t(
267
267
  "form",
268
268
  {
269
269
  ref: w,
270
- className: `nice-change-password${p ? ` ${p}` : ""}`,
270
+ className: `nice-change-password${m ? ` ${m}` : ""}`,
271
271
  style: s,
272
272
  onSubmit: F,
273
273
  noValidate: !0,
274
274
  children: [
275
275
  /* @__PURE__ */ e("h3", { className: "nice-change-password__title", children: i }),
276
- m && /* @__PURE__ */ e("div", { className: "nice-change-password__error", role: "alert", children: m }),
276
+ u && /* @__PURE__ */ e("div", { className: "nice-change-password__error", role: "alert", children: u }),
277
277
  h && /* @__PURE__ */ e("div", { className: "nice-change-password__success", role: "status", children: h }),
278
278
  a && /* @__PURE__ */ t("div", { className: "nice-change-password__field", children: [
279
279
  /* @__PURE__ */ e("label", { htmlFor: "nice-cp-current", children: "Current password" }),
@@ -325,7 +325,7 @@ const Q = T(({
325
325
  ),
326
326
  y && !l && /* @__PURE__ */ e("span", { className: "nice-error-text", children: "Passwords do not match" })
327
327
  ] }),
328
- /* @__PURE__ */ e("button", { type: "submit", className: "nice-btn nice-btn--primary", disabled: !u, children: d ? "…" : "Change Password" })
328
+ /* @__PURE__ */ e("button", { type: "submit", className: "nice-btn nice-btn--primary", disabled: !p, children: d ? "…" : "Change Password" })
329
329
  ]
330
330
  }
331
331
  );
@@ -335,20 +335,20 @@ const z = T(({
335
335
  secret: a,
336
336
  qrCodeUrl: n,
337
337
  issuer: d,
338
- accountName: m,
338
+ accountName: u,
339
339
  onVerify: h,
340
340
  loading: f = !1,
341
341
  error: i,
342
- onCancel: p,
342
+ onCancel: m,
343
343
  recoveryCodes: s,
344
344
  step: w = "setup",
345
345
  onStepChange: c,
346
346
  className: k,
347
347
  style: b
348
348
  }, C) => {
349
- const [y, $] = _(""), [o, l] = _(!1), [u, F] = _(!1), g = A((v) => {
349
+ const [y, $] = _(""), [o, l] = _(!1), [p, F] = _(!1), g = A((v) => {
350
350
  v.preventDefault(), y.length >= 6 && (h == null || h(y));
351
- }, [y, h]), P = `otpauth://totp/${d ? `${d}:` : ""}${m ?? "user"}?secret=${a}${d ? `&issuer=${d}` : ""}&digits=6&period=30`;
351
+ }, [y, h]), P = `otpauth://totp/${d ? `${d}:` : ""}${u ?? "user"}?secret=${a}${d ? `&issuer=${d}` : ""}&digits=6&period=30`;
352
352
  return /* @__PURE__ */ t("div", { ref: C, className: `nice-2fa-setup${k ? ` ${k}` : ""}`, style: b, children: [
353
353
  /* @__PURE__ */ e("h3", { className: "nice-2fa-setup__title", children: "Two-Factor Authentication" }),
354
354
  w === "setup" && /* @__PURE__ */ t("div", { className: "nice-2fa-setup__step", children: [
@@ -399,12 +399,12 @@ const z = T(({
399
399
  (v = navigator.clipboard) == null || v.writeText(s.join(`
400
400
  `)), F(!0);
401
401
  },
402
- children: u ? "Copied!" : "Copy all codes"
402
+ children: p ? "Copied!" : "Copy all codes"
403
403
  }
404
404
  ),
405
- p && /* @__PURE__ */ e("button", { type: "button", className: "nice-btn nice-btn--primary", onClick: p, children: "Done" })
405
+ m && /* @__PURE__ */ e("button", { type: "button", className: "nice-btn nice-btn--primary", onClick: m, children: "Done" })
406
406
  ] }),
407
- p && w !== "recovery" && /* @__PURE__ */ e("button", { type: "button", className: "nice-2fa-setup__cancel", onClick: p, children: "Cancel" })
407
+ m && w !== "recovery" && /* @__PURE__ */ e("button", { type: "button", className: "nice-2fa-setup__cancel", onClick: m, children: "Cancel" })
408
408
  ] });
409
409
  });
410
410
  z.displayName = "NiceTwoFaSetup";
@@ -412,14 +412,14 @@ const J = T(({
412
412
  provider: a = "custom",
413
413
  siteKey: n,
414
414
  onVerify: d,
415
- onExpire: m,
415
+ onExpire: u,
416
416
  onError: h,
417
417
  theme: f = "light",
418
418
  size: i = "normal",
419
- customRender: p,
419
+ customRender: m,
420
420
  className: s,
421
421
  style: w
422
- }, c) => a === "custom" && p ? /* @__PURE__ */ e("div", { ref: c, className: `nice-captcha nice-captcha--custom${s ? ` ${s}` : ""}`, style: w, children: p }) : /* @__PURE__ */ e(
422
+ }, c) => a === "custom" && m ? /* @__PURE__ */ e("div", { ref: c, className: `nice-captcha nice-captcha--custom${s ? ` ${s}` : ""}`, style: w, children: m }) : /* @__PURE__ */ e(
423
423
  "div",
424
424
  {
425
425
  ref: c,
@@ -453,17 +453,17 @@ const K = [
453
453
  providers: a = K,
454
454
  onProviderClick: n,
455
455
  disabled: d = !1,
456
- loadingProvider: m,
456
+ loadingProvider: u,
457
457
  direction: h = "vertical",
458
458
  separatorText: f = "or continue with",
459
459
  showSeparator: i = !0,
460
- className: p,
460
+ className: m,
461
461
  style: s
462
462
  }, w) => /* @__PURE__ */ t(
463
463
  "div",
464
464
  {
465
465
  ref: w,
466
- className: `nice-oauth nice-oauth--${h}${p ? ` ${p}` : ""}`,
466
+ className: `nice-oauth nice-oauth--${h}${m ? ` ${m}` : ""}`,
467
467
  style: s,
468
468
  children: [
469
469
  i && /* @__PURE__ */ t("div", { className: "nice-oauth__separator", children: [
@@ -475,13 +475,13 @@ const K = [
475
475
  "button",
476
476
  {
477
477
  type: "button",
478
- className: `nice-oauth__btn${m === c.id ? " nice-oauth__btn--loading" : ""}`,
478
+ className: `nice-oauth__btn${u === c.id ? " nice-oauth__btn--loading" : ""}`,
479
479
  style: { backgroundColor: c.color, color: c.textColor },
480
480
  onClick: () => n == null ? void 0 : n(c.id),
481
- disabled: d || !!m,
481
+ disabled: d || !!u,
482
482
  children: [
483
483
  c.icon && /* @__PURE__ */ e("span", { className: "nice-oauth__btn-icon", children: c.icon }),
484
- /* @__PURE__ */ e("span", { className: "nice-oauth__btn-label", children: m === c.id ? "…" : c.label })
484
+ /* @__PURE__ */ e("span", { className: "nice-oauth__btn-label", children: u === c.id ? "…" : c.label })
485
485
  ]
486
486
  },
487
487
  c.id
@@ -492,15 +492,15 @@ const K = [
492
492
  U.displayName = "NiceOAuthButtons";
493
493
  const X = T(function(n, d) {
494
494
  const {
495
- tokens: m,
495
+ tokens: u,
496
496
  onCreate: h,
497
497
  onRevoke: f,
498
498
  availableScopes: i = [],
499
- maxTokens: p = 10,
499
+ maxTokens: m = 10,
500
500
  title: s = "API Tokens",
501
501
  className: w,
502
502
  style: c
503
- } = n, [k, b] = _(!1), [C, y] = _(""), [$, o] = _([]), [l, u] = _("90"), [F, g] = _(null), [P, v] = _(!1), [S, x] = _(null), D = A(async () => {
503
+ } = n, [k, b] = _(!1), [C, y] = _(""), [$, o] = _([]), [l, p] = _("90"), [F, g] = _(null), [P, v] = _(!1), [S, x] = _(null), D = A(async () => {
504
504
  if (!(!h || !C.trim())) {
505
505
  v(!0);
506
506
  try {
@@ -521,7 +521,7 @@ const X = T(function(n, d) {
521
521
  }
522
522
  }, [f]), E = (r) => {
523
523
  o((N) => N.includes(r) ? N.filter((O) => O !== r) : [...N, r]);
524
- }, R = m.length < p;
524
+ }, R = u.length < m;
525
525
  return /* @__PURE__ */ t("div", { ref: d, className: `nice-token-management ${w ?? ""}`, style: c, children: [
526
526
  /* @__PURE__ */ t("div", { className: "nice-token-management__header", children: [
527
527
  /* @__PURE__ */ e("h3", { children: s }),
@@ -570,7 +570,7 @@ const X = T(function(n, d) {
570
570
  {
571
571
  type: "number",
572
572
  value: l,
573
- onChange: (r) => u(r.target.value),
573
+ onChange: (r) => p(r.target.value),
574
574
  min: 1,
575
575
  max: 365
576
576
  }
@@ -581,7 +581,7 @@ const X = T(function(n, d) {
581
581
  /* @__PURE__ */ e("button", { onClick: () => b(!1), children: "Cancel" })
582
582
  ] })
583
583
  ] }),
584
- m.length === 0 ? /* @__PURE__ */ e("p", { className: "nice-token-management__empty", children: "No API tokens yet." }) : /* @__PURE__ */ t("table", { className: "nice-token-management__table", children: [
584
+ u.length === 0 ? /* @__PURE__ */ e("p", { className: "nice-token-management__empty", children: "No API tokens yet." }) : /* @__PURE__ */ t("table", { className: "nice-token-management__table", children: [
585
585
  /* @__PURE__ */ e("thead", { children: /* @__PURE__ */ t("tr", { children: [
586
586
  /* @__PURE__ */ e("th", { children: "Name" }),
587
587
  /* @__PURE__ */ e("th", { children: "Prefix" }),
@@ -591,7 +591,7 @@ const X = T(function(n, d) {
591
591
  /* @__PURE__ */ e("th", { children: "Scopes" }),
592
592
  /* @__PURE__ */ e("th", {})
593
593
  ] }) }),
594
- /* @__PURE__ */ e("tbody", { children: m.map((r) => {
594
+ /* @__PURE__ */ e("tbody", { children: u.map((r) => {
595
595
  var N;
596
596
  return /* @__PURE__ */ t("tr", { children: [
597
597
  /* @__PURE__ */ e("td", { children: r.name }),