@nice2dev/auth 1.0.20 → 1.0.22
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/NiceCaptcha.d.ts +54 -12
- package/dist/NiceCaptcha.d.ts.map +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +629 -392
- package/dist/index.mjs.map +1 -1
- package/dist/style.css +1 -0
- package/package.json +1 -1
package/dist/NiceCaptcha.d.ts
CHANGED
|
@@ -1,21 +1,56 @@
|
|
|
1
1
|
import { default as React } from 'react';
|
|
2
|
-
/** Captcha provider type. */
|
|
2
|
+
/** Captcha provider type (third-party SDK shell). */
|
|
3
3
|
export type CaptchaProvider = 'custom' | 'recaptcha' | 'hcaptcha' | 'turnstile';
|
|
4
|
+
/** Built-in, self-contained challenge variant. */
|
|
5
|
+
export type CaptchaVariant = 'checkbox' | 'math' | 'text' | 'slider';
|
|
6
|
+
/** Difficulty of a built-in challenge. */
|
|
7
|
+
export type CaptchaDifficulty = 'easy' | 'medium' | 'hard';
|
|
8
|
+
/** Imperative handle exposed via `ref`. */
|
|
9
|
+
export interface NiceCaptchaHandle {
|
|
10
|
+
/** Reset to the initial unsolved state with a fresh challenge. */
|
|
11
|
+
reset: () => void;
|
|
12
|
+
/** Generate a fresh challenge (also fires `onRefresh`). */
|
|
13
|
+
refresh: () => void;
|
|
14
|
+
/** The root DOM element, if mounted. */
|
|
15
|
+
container: HTMLDivElement | null;
|
|
16
|
+
}
|
|
4
17
|
/** Props for the {@link NiceCaptcha} component. */
|
|
5
18
|
export interface NiceCaptchaProps {
|
|
6
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Built-in challenge variant. When set, NiceCaptcha renders a self-contained
|
|
21
|
+
* challenge instead of the third-party `provider` shell.
|
|
22
|
+
*/
|
|
23
|
+
variant?: CaptchaVariant;
|
|
24
|
+
/** Difficulty of the built-in challenge. @default 'medium' */
|
|
25
|
+
difficulty?: CaptchaDifficulty;
|
|
26
|
+
/** Whether to show the refresh button (built-in variants). @default true */
|
|
27
|
+
refreshable?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Optional async server-side verification hook. Receives the locally
|
|
30
|
+
* generated token and resolves to `true` when the server accepts it.
|
|
31
|
+
* NOTE: built-in challenges are a UX/deterrent layer only — real bot
|
|
32
|
+
* protection must be enforced on the server. Use this hook to do so.
|
|
33
|
+
*/
|
|
34
|
+
verifyToken?: (token: string) => Promise<boolean>;
|
|
35
|
+
/** Time-to-live (ms) of a solved state; after it elapses `onExpire` fires and the challenge resets. */
|
|
36
|
+
expiresMs?: number;
|
|
37
|
+
/** Label for the checkbox variant. @default "I'm not a robot" */
|
|
38
|
+
label?: string;
|
|
39
|
+
/** Captcha provider (used when `variant` is not set). */
|
|
7
40
|
provider?: CaptchaProvider;
|
|
8
41
|
/** Site key for the captcha provider. */
|
|
9
42
|
siteKey?: string;
|
|
10
|
-
/** Fires when captcha is verified with a token. */
|
|
43
|
+
/** Fires when the captcha is verified with a token. */
|
|
11
44
|
onVerify?: (token: string) => void;
|
|
12
|
-
/** Fires when captcha expires. */
|
|
45
|
+
/** Fires when a solved captcha expires. */
|
|
13
46
|
onExpire?: () => void;
|
|
14
|
-
/** Fires on
|
|
47
|
+
/** Fires on a verification error (e.g. `verifyToken` rejected). */
|
|
15
48
|
onError?: (error: unknown) => void;
|
|
16
|
-
/**
|
|
49
|
+
/** Fires when the challenge is refreshed. */
|
|
50
|
+
onRefresh?: () => void;
|
|
51
|
+
/** Theme. @default 'light' */
|
|
17
52
|
theme?: 'light' | 'dark';
|
|
18
|
-
/** Size. */
|
|
53
|
+
/** Size. @default 'normal' */
|
|
19
54
|
size?: 'normal' | 'compact' | 'invisible';
|
|
20
55
|
/** Custom render for provider='custom'. */
|
|
21
56
|
customRender?: React.ReactNode;
|
|
@@ -23,10 +58,17 @@ export interface NiceCaptchaProps {
|
|
|
23
58
|
style?: React.CSSProperties;
|
|
24
59
|
}
|
|
25
60
|
/**
|
|
26
|
-
* {@link NiceCaptcha} —
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
61
|
+
* {@link NiceCaptcha} — captcha widget with two modes:
|
|
62
|
+
*
|
|
63
|
+
* 1. **Built-in challenges** (set `variant`): self-contained `checkbox`, `math`,
|
|
64
|
+
* `text` or `slider` challenges with refresh, difficulty levels, a11y and an
|
|
65
|
+
* optional async server-verification hook (`verifyToken`).
|
|
66
|
+
* 2. **Provider shell** (set `provider`): a consistent wrapper around reCAPTCHA,
|
|
67
|
+
* hCaptcha or Turnstile — inject the actual SDK via `customRender` or load the
|
|
68
|
+
* provider script externally.
|
|
69
|
+
*
|
|
70
|
+
* SECURITY: built-in challenges run client-side and are a UX/deterrent layer
|
|
71
|
+
* only. Enforce real verification on the server (see `verifyToken`).
|
|
30
72
|
*/
|
|
31
|
-
export declare const NiceCaptcha: React.ForwardRefExoticComponent<NiceCaptchaProps & React.RefAttributes<
|
|
73
|
+
export declare const NiceCaptcha: React.ForwardRefExoticComponent<NiceCaptchaProps & React.RefAttributes<NiceCaptchaHandle>>;
|
|
32
74
|
//# sourceMappingURL=NiceCaptcha.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NiceCaptcha.d.ts","sourceRoot":"","sources":["../src/NiceCaptcha.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"NiceCaptcha.d.ts","sourceRoot":"","sources":["../src/NiceCaptcha.tsx"],"names":[],"mappings":"AAAA,OAAO,KAQN,MAAM,OAAO,CAAC;AAEf,OAAO,mBAAmB,CAAC;AAE3B,qDAAqD;AACrD,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,WAAW,GAAG,UAAU,GAAG,WAAW,CAAC;AAEhF,kDAAkD;AAClD,MAAM,MAAM,cAAc,GAAG,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,CAAC;AAErE,0CAA0C;AAC1C,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,QAAQ,GAAG,MAAM,CAAC;AAE3D,2CAA2C;AAC3C,MAAM,WAAW,iBAAiB;IAChC,kEAAkE;IAClE,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,2DAA2D;IAC3D,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,wCAAwC;IACxC,SAAS,EAAE,cAAc,GAAG,IAAI,CAAC;CAClC;AAED,mDAAmD;AACnD,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,8DAA8D;IAC9D,UAAU,CAAC,EAAE,iBAAiB,CAAC;IAC/B,4EAA4E;IAC5E,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAClD,uGAAuG;IACvG,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,iEAAiE;IACjE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,yDAAyD;IACzD,QAAQ,CAAC,EAAE,eAAe,CAAC;IAC3B,yCAAyC;IACzC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,uDAAuD;IACvD,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,2CAA2C;IAC3C,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAC;IACtB,mEAAmE;IACnE,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;IACnC,6CAA6C;IAC7C,SAAS,CAAC,EAAE,MAAM,IAAI,CAAC;IACvB,8BAA8B;IAC9B,KAAK,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IACzB,8BAA8B;IAC9B,IAAI,CAAC,EAAE,QAAQ,GAAG,SAAS,GAAG,WAAW,CAAC;IAC1C,2CAA2C;IAC3C,YAAY,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC/B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;CAC7B;AA4FD;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,WAAW,4FAuUtB,CAAC"}
|
package/dist/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("react"),e=require("react/jsx-runtime");function B(n={}){const{onLogin:t,onRefresh:o,onLogout:d,storageKey:h="nice_auth",persistAuth:f=!1}=n,[r,m]=s.useState(()=>{if(f&&typeof window<"u")try{const i=localStorage.getItem(h);if(i)return{...JSON.parse(i),loading:!1,error:null}}catch{}return{isAuthenticated:!1,user:null,tokens:null,loading:!1,error:null}}),c=s.useCallback(i=>{m(u=>{const p={...u,...i};return f&&typeof window<"u"&&(p.isAuthenticated?localStorage.setItem(h,JSON.stringify({isAuthenticated:!0,user:p.user})):localStorage.removeItem(h)),p})},[f,h]),b=s.useCallback(async(i,u)=>{c({loading:!0,error:null});try{if(t){const p=await t(i,u);return c({isAuthenticated:!0,user:p.user,tokens:p.tokens,loading:!1}),!0}return c({loading:!1,error:"No login handler configured"}),!1}catch(p){return c({loading:!1,error:(p==null?void 0:p.message)??"Login failed"}),!1}},[t,c]),a=s.useCallback(()=>{d==null||d(),c({isAuthenticated:!1,user:null,tokens:null,loading:!1,error:null})},[d,c]),w=s.useCallback(async()=>{var i;if(!((i=r.tokens)!=null&&i.refreshToken)||!o)return!1;try{const u=await o(r.tokens.refreshToken);return c({tokens:u}),!0}catch{return a(),!1}},[r.tokens,o,c,a]),g=s.useCallback((i,u)=>{c({isAuthenticated:!0,user:i,tokens:u,loading:!1,error:null})},[c]),k=s.useCallback((...i)=>r.user?i.every(u=>r.user.roles.includes(u)):!1,[r.user]),_=s.useCallback((...i)=>r.user?i.every(u=>r.user.permissions.includes(u)):!1,[r.user]),v=s.useCallback(()=>{c({error:null})},[c]);return{...r,login:b,logout:a,refreshToken:w,setAuth:g,hasRoles:k,hasPermissions:_,clearError:v}}const D=s.forwardRef(({onSubmit:n,loading:t=!1,error:o,showRememberMe:d=!0,showForgotPassword:h=!0,onForgotPassword:f,showRegisterLink:r=!0,onRegisterClick:m,title:c="Sign In",submitLabel:b="Sign In",logo:a,footer:w,className:g,style:k,emailLabel:_="Email",passwordLabel:v="Password",tracelessPolicy:i,privacyMessage:u},p)=>{const[y,x]=s.useState(""),[S,N]=s.useState(""),[A,F]=s.useState(!1),R=s.useMemo(()=>!(!d||i&&!i.cookiesAllowed),[d,i]),T=s.useCallback(C=>{C.preventDefault();const $=(i==null?void 0:i.cookiesAllowed)===!1?!1:A;n==null||n({email:y,password:S,rememberMe:$})},[y,S,A,n,i]);return e.jsxs("form",{ref:p,className:`nice-login-form${g?` ${g}`:""}`,style:k,onSubmit:T,noValidate:!0,children:[a&&e.jsx("div",{className:"nice-login-form__logo",children:a}),e.jsx("h2",{className:"nice-login-form__title",children:c}),o&&e.jsx("div",{className:"nice-login-form__error",role:"alert",children:o}),e.jsxs("div",{className:"nice-login-form__field",children:[e.jsx("label",{htmlFor:"nice-login-email",children:_}),e.jsx("input",{id:"nice-login-email",className:"nice-input",type:"email",value:y,onChange:C=>x(C.target.value),autoComplete:"email",required:!0,disabled:t})]}),e.jsxs("div",{className:"nice-login-form__field",children:[e.jsx("label",{htmlFor:"nice-login-password",children:v}),e.jsx("input",{id:"nice-login-password",className:"nice-input",type:"password",value:S,onChange:C=>N(C.target.value),autoComplete:"current-password",required:!0,disabled:t})]}),e.jsxs("div",{className:"nice-login-form__options",children:[R&&e.jsxs("label",{className:"nice-login-form__remember",children:[e.jsx("input",{type:"checkbox",checked:A,onChange:C=>F(C.target.checked),disabled:t}),e.jsx("span",{children:"Remember me"})]}),!R&&d&&u&&e.jsx("span",{className:"nice-login-form__privacy-notice",children:u}),h&&e.jsx("button",{type:"button",className:"nice-login-form__forgot",onClick:f,disabled:t,children:"Forgot password?"})]}),e.jsx("button",{type:"submit",className:"nice-btn nice-btn--primary nice-login-form__submit",disabled:t||!y||!S,children:t?"…":b}),r&&e.jsxs("div",{className:"nice-login-form__register",children:[e.jsx("span",{children:"Don't have an account? "}),e.jsx("button",{type:"button",onClick:m,disabled:t,children:"Register"})]}),w&&e.jsx("div",{className:"nice-login-form__footer",children:w})]})});D.displayName="NiceLoginForm";const G={0:"Very weak",1:"Weak",2:"Fair",3:"Strong",4:"Very strong"},Q={0:"#d9534f",1:"#f0ad4e",2:"#5bc0de",3:"#5cb85c",4:"#2e7d32"};function E(n){if(!n)return 0;let t=0;return n.length>=8&&t++,n.length>=12&&t++,/[a-z]/.test(n)&&/[A-Z]/.test(n)&&t++,/\d/.test(n)&&t++,/[^a-zA-Z0-9]/.test(n)&&t++,Math.min(4,t)}const P=s.forwardRef(({password:n,strength:t,showLabel:o=!0,labels:d=G,className:h},f)=>{const r=t??E(n),m=Q[r],c=(r+1)/5*100;return e.jsxs("div",{ref:f,className:`nice-password-strength${h?` ${h}`:""}`,children:[e.jsx("div",{className:"nice-password-strength__bar",children:e.jsx("div",{className:"nice-password-strength__fill",style:{width:`${c}%`,backgroundColor:m}})}),o&&e.jsx("span",{className:"nice-password-strength__label",style:{color:m},children:d[r]})]})});P.displayName="NicePasswordStrength";const L=s.forwardRef(({onSubmit:n,loading:t=!1,error:o,onLoginClick:d,showPasswordStrength:h=!0,minPasswordStrength:f=2,termsUrl:r,privacyUrl:m,title:c="Create Account",logo:b,footer:a,className:w,style:g},k)=>{const[_,v]=s.useState(""),[i,u]=s.useState(""),[p,y]=s.useState(""),[x,S]=s.useState(""),[N,A]=s.useState(""),[F,R]=s.useState(!1),T=E(x),C=x===N,$=_&&p&&x&&C&&F&&T>=f&&!t,l=s.useCallback(j=>{j.preventDefault(),$&&(n==null||n({firstName:_,lastName:i,email:p,password:x,acceptTerms:F}))},[$,_,i,p,x,F,n]);return e.jsxs("form",{ref:k,className:`nice-register-form${w?` ${w}`:""}`,style:g,onSubmit:l,noValidate:!0,children:[b&&e.jsx("div",{className:"nice-register-form__logo",children:b}),e.jsx("h2",{className:"nice-register-form__title",children:c}),o&&e.jsx("div",{className:"nice-register-form__error",role:"alert",children:o}),e.jsxs("div",{className:"nice-register-form__row",children:[e.jsxs("div",{className:"nice-register-form__field",children:[e.jsx("label",{htmlFor:"nice-reg-first",children:"First name *"}),e.jsx("input",{id:"nice-reg-first",className:"nice-input",type:"text",value:_,onChange:j=>v(j.target.value),required:!0,disabled:t,autoComplete:"given-name"})]}),e.jsxs("div",{className:"nice-register-form__field",children:[e.jsx("label",{htmlFor:"nice-reg-last",children:"Last name"}),e.jsx("input",{id:"nice-reg-last",className:"nice-input",type:"text",value:i,onChange:j=>u(j.target.value),disabled:t,autoComplete:"family-name"})]})]}),e.jsxs("div",{className:"nice-register-form__field",children:[e.jsx("label",{htmlFor:"nice-reg-email",children:"Email *"}),e.jsx("input",{id:"nice-reg-email",className:"nice-input",type:"email",value:p,onChange:j=>y(j.target.value),required:!0,disabled:t,autoComplete:"email"})]}),e.jsxs("div",{className:"nice-register-form__field",children:[e.jsx("label",{htmlFor:"nice-reg-pass",children:"Password *"}),e.jsx("input",{id:"nice-reg-pass",className:"nice-input",type:"password",value:x,onChange:j=>S(j.target.value),required:!0,disabled:t,autoComplete:"new-password"}),h&&x.length>0&&e.jsx(P,{password:x})]}),e.jsxs("div",{className:"nice-register-form__field",children:[e.jsx("label",{htmlFor:"nice-reg-confirm",children:"Confirm password *"}),e.jsx("input",{id:"nice-reg-confirm",className:"nice-input",type:"password",value:N,onChange:j=>A(j.target.value),required:!0,disabled:t,autoComplete:"new-password"}),N&&!C&&e.jsx("span",{className:"nice-error-text",children:"Passwords do not match"})]}),e.jsxs("label",{className:"nice-register-form__terms",children:[e.jsx("input",{type:"checkbox",checked:F,onChange:j=>R(j.target.checked),disabled:t}),e.jsxs("span",{children:["I accept the"," ",r?e.jsx("a",{href:r,target:"_blank",rel:"noopener noreferrer",children:"Terms"}):"Terms",m&&e.jsxs(e.Fragment,{children:[" ","and"," ",e.jsx("a",{href:m,target:"_blank",rel:"noopener noreferrer",children:"Privacy Policy"})]})," *"]})]}),e.jsx("button",{type:"submit",className:"nice-btn nice-btn--primary nice-register-form__submit",disabled:!$,children:t?"…":"Register"}),d&&e.jsxs("div",{className:"nice-register-form__login",children:[e.jsx("span",{children:"Already have an account? "}),e.jsx("button",{type:"button",onClick:d,children:"Sign in"})]}),a&&e.jsx("div",{className:"nice-register-form__footer",children:a})]})});L.displayName="NiceRegistrationForm";const M=s.forwardRef(({requireCurrentPassword:n=!0,onSubmit:t,loading:o=!1,error:d,success:h,minPasswordStrength:f=2,title:r="Change Password",className:m,style:c},b)=>{const[a,w]=s.useState(""),[g,k]=s.useState(""),[_,v]=s.useState(""),i=E(g),u=g===_,p=(!n||a.length>0)&&g.length>0&&u&&i>=f&&!o,y=s.useCallback(x=>{x.preventDefault(),p&&(t==null||t({currentPassword:n?a:void 0,newPassword:g}))},[p,a,g,n,t]);return e.jsxs("form",{ref:b,className:`nice-change-password${m?` ${m}`:""}`,style:c,onSubmit:y,noValidate:!0,children:[e.jsx("h3",{className:"nice-change-password__title",children:r}),d&&e.jsx("div",{className:"nice-change-password__error",role:"alert",children:d}),h&&e.jsx("div",{className:"nice-change-password__success",role:"status",children:h}),n&&e.jsxs("div",{className:"nice-change-password__field",children:[e.jsx("label",{htmlFor:"nice-cp-current",children:"Current password"}),e.jsx("input",{id:"nice-cp-current",className:"nice-input",type:"password",value:a,onChange:x=>w(x.target.value),autoComplete:"current-password",disabled:o,required:!0})]}),e.jsxs("div",{className:"nice-change-password__field",children:[e.jsx("label",{htmlFor:"nice-cp-new",children:"New password"}),e.jsx("input",{id:"nice-cp-new",className:"nice-input",type:"password",value:g,onChange:x=>k(x.target.value),autoComplete:"new-password",disabled:o,required:!0}),g.length>0&&e.jsx(P,{password:g})]}),e.jsxs("div",{className:"nice-change-password__field",children:[e.jsx("label",{htmlFor:"nice-cp-confirm",children:"Confirm new password"}),e.jsx("input",{id:"nice-cp-confirm",className:"nice-input",type:"password",value:_,onChange:x=>v(x.target.value),autoComplete:"new-password",disabled:o,required:!0}),_&&!u&&e.jsx("span",{className:"nice-error-text",children:"Passwords do not match"})]}),e.jsx("button",{type:"submit",className:"nice-btn nice-btn--primary",disabled:!p,children:o?"…":"Change Password"})]})});M.displayName="NiceChangePassword";const I=s.forwardRef(({secret:n,qrCodeUrl:t,issuer:o,accountName:d,onVerify:h,loading:f=!1,error:r,onCancel:m,recoveryCodes:c,step:b="setup",onStepChange:a,className:w,style:g},k)=>{const[_,v]=s.useState(""),[i,u]=s.useState(!1),[p,y]=s.useState(!1),x=s.useCallback(N=>{N.preventDefault(),_.length>=6&&(h==null||h(_))},[_,h]),S=`otpauth://totp/${o?`${o}:`:""}${d??"user"}?secret=${n}${o?`&issuer=${o}`:""}&digits=6&period=30`;return e.jsxs("div",{ref:k,className:`nice-2fa-setup${w?` ${w}`:""}`,style:g,children:[e.jsx("h3",{className:"nice-2fa-setup__title",children:"Two-Factor Authentication"}),b==="setup"&&e.jsxs("div",{className:"nice-2fa-setup__step",children:[e.jsx("p",{className:"nice-2fa-setup__instruction",children:"Scan this QR code with your authenticator app (Google Authenticator, Authy, etc.):"}),t?e.jsx("img",{src:t,alt:"2FA QR Code",className:"nice-2fa-setup__qr"}):e.jsxs("div",{className:"nice-2fa-setup__qr-placeholder",children:[e.jsx("span",{children:"QR Code"}),e.jsx("code",{className:"nice-2fa-setup__uri",children:S})]}),e.jsxs("div",{className:"nice-2fa-setup__secret",children:[e.jsx("span",{children:"Or enter manually: "}),i?e.jsx("code",{className:"nice-2fa-setup__secret-key",children:n}):e.jsx("button",{type:"button",onClick:()=>u(!0),children:"Show secret key"})]}),e.jsx("button",{type:"button",className:"nice-btn nice-btn--primary",onClick:()=>a==null?void 0:a("verify"),children:"Next — Verify"})]}),b==="verify"&&e.jsxs("form",{className:"nice-2fa-setup__step",onSubmit:x,children:[e.jsx("p",{className:"nice-2fa-setup__instruction",children:"Enter the 6-digit code from your authenticator app:"}),r&&e.jsx("div",{className:"nice-2fa-setup__error",role:"alert",children:r}),e.jsx("input",{className:"nice-input nice-2fa-setup__code-input",type:"text",inputMode:"numeric",maxLength:6,value:_,onChange:N=>v(N.target.value.replace(/\D/g,"").slice(0,6)),placeholder:"000000",autoFocus:!0,disabled:f,autoComplete:"one-time-code"}),e.jsxs("div",{className:"nice-2fa-setup__actions",children:[e.jsx("button",{type:"button",className:"nice-btn",onClick:()=>a==null?void 0:a("setup"),children:"Back"}),e.jsx("button",{type:"submit",className:"nice-btn nice-btn--primary",disabled:f||_.length<6,children:f?"…":"Verify"})]})]}),b==="recovery"&&c&&e.jsxs("div",{className:"nice-2fa-setup__step",children:[e.jsx("p",{className:"nice-2fa-setup__instruction",children:"Save these recovery codes in a safe place. Each code can only be used once:"}),e.jsx("div",{className:"nice-2fa-setup__recovery-codes",children:c.map((N,A)=>e.jsx("code",{className:"nice-2fa-setup__recovery-code",children:N},A))}),e.jsx("button",{type:"button",className:"nice-btn",onClick:()=>{var N;(N=navigator.clipboard)==null||N.writeText(c.join(`
|
|
2
|
-
`)),y(!0)},children:p?"Copied!":"Copy all codes"}),m&&e.jsx("button",{type:"button",className:"nice-btn nice-btn--primary",onClick:m,children:"Done"})]}),m&&b!=="recovery"&&e.jsx("button",{type:"button",className:"nice-2fa-setup__cancel",onClick:m,children:"Cancel"})]})});I.displayName="NiceTwoFaSetup";const O=s.forwardRef(({provider:n="custom",siteKey:t,onVerify:o,onExpire:d,onError:h,theme:f="light",size:r="normal",customRender:m,className:c,style:b},a)=>n==="custom"&&m?e.jsx("div",{ref:a,className:`nice-captcha nice-captcha--custom${c?` ${c}`:""}`,style:b,children:m}):e.jsx("div",{ref:a,className:`nice-captcha nice-captcha--${n} nice-captcha--${f}${r==="compact"?" nice-captcha--compact":""}${c?` ${c}`:""}`,style:b,"data-sitekey":t,"data-theme":f,"data-size":r,children:e.jsxs("div",{className:"nice-captcha__placeholder",children:[e.jsxs("span",{children:["🔒 Captcha (",n,")"]}),e.jsxs("p",{children:["Load the ",n," SDK to activate verification."]})]})}));O.displayName="NiceCaptcha";const z=[{id:"google",label:"Google",icon:"🔵",color:"#4285f4",textColor:"#fff"},{id:"microsoft",label:"Microsoft",icon:"🟦",color:"#00a4ef",textColor:"#fff"},{id:"discord",label:"Discord",icon:"💬",color:"#5865f2",textColor:"#fff"},{id:"spotify",label:"Spotify",icon:"🎵",color:"#1db954",textColor:"#fff"},{id:"github",label:"GitHub",icon:"🐙",color:"#24292e",textColor:"#fff"}],q=s.forwardRef(({providers:n=z,onProviderClick:t,disabled:o=!1,loadingProvider:d,direction:h="vertical",separatorText:f="or continue with",showSeparator:r=!0,className:m,style:c},b)=>e.jsxs("div",{ref:b,className:`nice-oauth nice-oauth--${h}${m?` ${m}`:""}`,style:c,children:[r&&e.jsxs("div",{className:"nice-oauth__separator",children:[e.jsx("hr",{}),e.jsx("span",{children:f}),e.jsx("hr",{})]}),e.jsx("div",{className:"nice-oauth__buttons",children:n.map(a=>e.jsxs("button",{type:"button",className:`nice-oauth__btn${d===a.id?" nice-oauth__btn--loading":""}`,style:{backgroundColor:a.color,color:a.textColor},onClick:()=>t==null?void 0:t(a.id),disabled:o||!!d,children:[a.icon&&e.jsx("span",{className:"nice-oauth__btn-icon",children:a.icon}),e.jsx("span",{className:"nice-oauth__btn-label",children:d===a.id?"…":a.label})]},a.id))})]}));q.displayName="NiceOAuthButtons";const J=s.forwardRef(function(t,o){const{tokens:d,onCreate:h,onRevoke:f,availableScopes:r=[],maxTokens:m=10,title:c="API Tokens",className:b,style:a}=t,[w,g]=s.useState(!1),[k,_]=s.useState(""),[v,i]=s.useState([]),[u,p]=s.useState("90"),[y,x]=s.useState(null),[S,N]=s.useState(!1),[A,F]=s.useState(null),R=s.useCallback(async()=>{if(!(!h||!k.trim())){N(!0);try{const l=u?parseInt(u,10):void 0,j=await h(k.trim(),v,l);x(j),_(""),i([]),g(!1)}finally{N(!1)}}},[h,k,v,u]),T=s.useCallback(async l=>{if(f){F(l);try{await f(l)}finally{F(null)}}},[f]),C=l=>{i(j=>j.includes(l)?j.filter(V=>V!==l):[...j,l])},$=d.length<m;return e.jsxs("div",{ref:o,className:`nice-token-management ${b??""}`,style:a,children:[e.jsxs("div",{className:"nice-token-management__header",children:[e.jsx("h3",{children:c}),$&&e.jsx("button",{className:"nice-token-management__new-btn",onClick:()=>g(!0),disabled:w,children:"+ New Token"})]}),y&&e.jsxs("div",{className:"nice-token-management__reveal",children:[e.jsxs("p",{children:[e.jsx("strong",{children:"New token created!"})," Copy it now — it won't be shown again."]}),e.jsx("code",{className:"nice-token-management__token-value",children:y}),e.jsx("button",{onClick:()=>x(null),children:"Dismiss"})]}),w&&e.jsxs("div",{className:"nice-token-management__create-form",children:[e.jsxs("label",{children:["Name",e.jsx("input",{type:"text",value:k,onChange:l=>_(l.target.value),placeholder:"My API token",maxLength:64})]}),r.length>0&&e.jsxs("fieldset",{children:[e.jsx("legend",{children:"Scopes"}),r.map(l=>e.jsxs("label",{className:"nice-token-management__scope",children:[e.jsx("input",{type:"checkbox",checked:v.includes(l),onChange:()=>C(l)}),l]},l))]}),e.jsxs("label",{children:["Expires in (days)",e.jsx("input",{type:"number",value:u,onChange:l=>p(l.target.value),min:1,max:365})]}),e.jsxs("div",{className:"nice-token-management__create-actions",children:[e.jsx("button",{onClick:R,disabled:S||!k.trim(),children:S?"Creating…":"Create Token"}),e.jsx("button",{onClick:()=>g(!1),children:"Cancel"})]})]}),d.length===0?e.jsx("p",{className:"nice-token-management__empty",children:"No API tokens yet."}):e.jsxs("table",{className:"nice-token-management__table",children:[e.jsx("thead",{children:e.jsxs("tr",{children:[e.jsx("th",{children:"Name"}),e.jsx("th",{children:"Prefix"}),e.jsx("th",{children:"Created"}),e.jsx("th",{children:"Expires"}),e.jsx("th",{children:"Last Used"}),e.jsx("th",{children:"Scopes"}),e.jsx("th",{})]})}),e.jsx("tbody",{children:d.map(l=>{var j;return e.jsxs("tr",{children:[e.jsx("td",{children:l.name}),e.jsx("td",{children:e.jsxs("code",{children:[l.prefix,"…"]})}),e.jsx("td",{children:l.createdAt}),e.jsx("td",{children:l.expiresAt??"—"}),e.jsx("td",{children:l.lastUsedAt??"Never"}),e.jsx("td",{children:((j=l.scopes)==null?void 0:j.join(", "))||"—"}),e.jsx("td",{children:e.jsx("button",{className:"nice-token-management__revoke-btn",onClick:()=>T(l.id),disabled:A===l.id,children:A===l.id?"Revoking…":"Revoke"})})]},l.id)})})]})]})});exports.NiceCaptcha=O;exports.NiceChangePassword=M;exports.NiceLoginForm=D;exports.NiceOAuthButtons=q;exports.NicePasswordStrength=P;exports.NiceRegistrationForm=L;exports.NiceTokenManagement=J;exports.NiceTwoFaSetup=I;exports.calcPasswordStrength=E;exports.useAuth=B;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react"),e=require("react/jsx-runtime");function ae(c={}){const{onLogin:s,onRefresh:n,onLogout:a,storageKey:i="nice_auth",persistAuth:l=!1}=c,[o,p]=t.useState(()=>{if(l&&typeof window<"u")try{const u=localStorage.getItem(i);if(u)return{...JSON.parse(u),loading:!1,error:null}}catch{}return{isAuthenticated:!1,user:null,tokens:null,loading:!1,error:null}}),d=t.useCallback(u=>{p(m=>{const f={...m,...u};return l&&typeof window<"u"&&(f.isAuthenticated?localStorage.setItem(i,JSON.stringify({isAuthenticated:!0,user:f.user})):localStorage.removeItem(i)),f})},[l,i]),y=t.useCallback(async(u,m)=>{d({loading:!0,error:null});try{if(s){const f=await s(u,m);return d({isAuthenticated:!0,user:f.user,tokens:f.tokens,loading:!1}),!0}return d({loading:!1,error:"No login handler configured"}),!1}catch(f){return d({loading:!1,error:(f==null?void 0:f.message)??"Login failed"}),!1}},[s,d]),r=t.useCallback(()=>{a==null||a(),d({isAuthenticated:!1,user:null,tokens:null,loading:!1,error:null})},[a,d]),v=t.useCallback(async()=>{var u;if(!((u=o.tokens)!=null&&u.refreshToken)||!n)return!1;try{const m=await n(o.tokens.refreshToken);return d({tokens:m}),!0}catch{return r(),!1}},[o.tokens,n,d,r]),g=t.useCallback((u,m)=>{d({isAuthenticated:!0,user:u,tokens:m,loading:!1,error:null})},[d]),k=t.useCallback((...u)=>o.user?u.every(m=>o.user.roles.includes(m)):!1,[o.user]),x=t.useCallback((...u)=>o.user?u.every(m=>o.user.permissions.includes(m)):!1,[o.user]),R=t.useCallback(()=>{d({error:null})},[d]);return{...o,login:y,logout:r,refreshToken:v,setAuth:g,hasRoles:k,hasPermissions:x,clearError:R}}const Q=t.forwardRef(({onSubmit:c,loading:s=!1,error:n,showRememberMe:a=!0,showForgotPassword:i=!0,onForgotPassword:l,showRegisterLink:o=!0,onRegisterClick:p,title:d="Sign In",submitLabel:y="Sign In",logo:r,footer:v,className:g,style:k,emailLabel:x="Email",passwordLabel:R="Password",tracelessPolicy:u,privacyMessage:m},f)=>{const[S,b]=t.useState(""),[F,w]=t.useState(""),[M,N]=t.useState(!1),T=t.useMemo(()=>!(!a||u&&!u.cookiesAllowed),[a,u]),I=t.useCallback(C=>{C.preventDefault();const A=(u==null?void 0:u.cookiesAllowed)===!1?!1:M;c==null||c({email:S,password:F,rememberMe:A})},[S,F,M,c,u]);return e.jsxs("form",{ref:f,className:`nice-login-form${g?` ${g}`:""}`,style:k,onSubmit:I,noValidate:!0,children:[r&&e.jsx("div",{className:"nice-login-form__logo",children:r}),e.jsx("h2",{className:"nice-login-form__title",children:d}),n&&e.jsx("div",{className:"nice-login-form__error",role:"alert",children:n}),e.jsxs("div",{className:"nice-login-form__field",children:[e.jsx("label",{htmlFor:"nice-login-email",children:x}),e.jsx("input",{id:"nice-login-email",className:"nice-input",type:"email",value:S,onChange:C=>b(C.target.value),autoComplete:"email",required:!0,disabled:s})]}),e.jsxs("div",{className:"nice-login-form__field",children:[e.jsx("label",{htmlFor:"nice-login-password",children:R}),e.jsx("input",{id:"nice-login-password",className:"nice-input",type:"password",value:F,onChange:C=>w(C.target.value),autoComplete:"current-password",required:!0,disabled:s})]}),e.jsxs("div",{className:"nice-login-form__options",children:[T&&e.jsxs("label",{className:"nice-login-form__remember",children:[e.jsx("input",{type:"checkbox",checked:M,onChange:C=>N(C.target.checked),disabled:s}),e.jsx("span",{children:"Remember me"})]}),!T&&a&&m&&e.jsx("span",{className:"nice-login-form__privacy-notice",children:m}),i&&e.jsx("button",{type:"button",className:"nice-login-form__forgot",onClick:l,disabled:s,children:"Forgot password?"})]}),e.jsx("button",{type:"submit",className:"nice-btn nice-btn--primary nice-login-form__submit",disabled:s||!S||!F,children:s?"…":y}),o&&e.jsxs("div",{className:"nice-login-form__register",children:[e.jsx("span",{children:"Don't have an account? "}),e.jsx("button",{type:"button",onClick:p,disabled:s,children:"Register"})]}),v&&e.jsx("div",{className:"nice-login-form__footer",children:v})]})});Q.displayName="NiceLoginForm";const ne={0:"Very weak",1:"Weak",2:"Fair",3:"Strong",4:"Very strong"},ce={0:"#d9534f",1:"#f0ad4e",2:"#5bc0de",3:"#5cb85c",4:"#2e7d32"};function B(c){if(!c)return 0;let s=0;return c.length>=8&&s++,c.length>=12&&s++,/[a-z]/.test(c)&&/[A-Z]/.test(c)&&s++,/\d/.test(c)&&s++,/[^a-zA-Z0-9]/.test(c)&&s++,Math.min(4,s)}const K=t.forwardRef(({password:c,strength:s,showLabel:n=!0,labels:a=ne,className:i},l)=>{const o=s??B(c),p=ce[o],d=(o+1)/5*100;return e.jsxs("div",{ref:l,className:`nice-password-strength${i?` ${i}`:""}`,children:[e.jsx("div",{className:"nice-password-strength__bar",children:e.jsx("div",{className:"nice-password-strength__fill",style:{width:`${d}%`,backgroundColor:p}})}),n&&e.jsx("span",{className:"nice-password-strength__label",style:{color:p},children:a[o]})]})});K.displayName="NicePasswordStrength";const U=t.forwardRef(({onSubmit:c,loading:s=!1,error:n,onLoginClick:a,showPasswordStrength:i=!0,minPasswordStrength:l=2,termsUrl:o,privacyUrl:p,title:d="Create Account",logo:y,footer:r,className:v,style:g},k)=>{const[x,R]=t.useState(""),[u,m]=t.useState(""),[f,S]=t.useState(""),[b,F]=t.useState(""),[w,M]=t.useState(""),[N,T]=t.useState(!1),I=B(b),C=b===w,A=x&&f&&b&&C&&N&&I>=l&&!s,h=t.useCallback(_=>{_.preventDefault(),A&&(c==null||c({firstName:x,lastName:u,email:f,password:b,acceptTerms:N}))},[A,x,u,f,b,N,c]);return e.jsxs("form",{ref:k,className:`nice-register-form${v?` ${v}`:""}`,style:g,onSubmit:h,noValidate:!0,children:[y&&e.jsx("div",{className:"nice-register-form__logo",children:y}),e.jsx("h2",{className:"nice-register-form__title",children:d}),n&&e.jsx("div",{className:"nice-register-form__error",role:"alert",children:n}),e.jsxs("div",{className:"nice-register-form__row",children:[e.jsxs("div",{className:"nice-register-form__field",children:[e.jsx("label",{htmlFor:"nice-reg-first",children:"First name *"}),e.jsx("input",{id:"nice-reg-first",className:"nice-input",type:"text",value:x,onChange:_=>R(_.target.value),required:!0,disabled:s,autoComplete:"given-name"})]}),e.jsxs("div",{className:"nice-register-form__field",children:[e.jsx("label",{htmlFor:"nice-reg-last",children:"Last name"}),e.jsx("input",{id:"nice-reg-last",className:"nice-input",type:"text",value:u,onChange:_=>m(_.target.value),disabled:s,autoComplete:"family-name"})]})]}),e.jsxs("div",{className:"nice-register-form__field",children:[e.jsx("label",{htmlFor:"nice-reg-email",children:"Email *"}),e.jsx("input",{id:"nice-reg-email",className:"nice-input",type:"email",value:f,onChange:_=>S(_.target.value),required:!0,disabled:s,autoComplete:"email"})]}),e.jsxs("div",{className:"nice-register-form__field",children:[e.jsx("label",{htmlFor:"nice-reg-pass",children:"Password *"}),e.jsx("input",{id:"nice-reg-pass",className:"nice-input",type:"password",value:b,onChange:_=>F(_.target.value),required:!0,disabled:s,autoComplete:"new-password"}),i&&b.length>0&&e.jsx(K,{password:b})]}),e.jsxs("div",{className:"nice-register-form__field",children:[e.jsx("label",{htmlFor:"nice-reg-confirm",children:"Confirm password *"}),e.jsx("input",{id:"nice-reg-confirm",className:"nice-input",type:"password",value:w,onChange:_=>M(_.target.value),required:!0,disabled:s,autoComplete:"new-password"}),w&&!C&&e.jsx("span",{className:"nice-error-text",children:"Passwords do not match"})]}),e.jsxs("label",{className:"nice-register-form__terms",children:[e.jsx("input",{type:"checkbox",checked:N,onChange:_=>T(_.target.checked),disabled:s}),e.jsxs("span",{children:["I accept the"," ",o?e.jsx("a",{href:o,target:"_blank",rel:"noopener noreferrer",children:"Terms"}):"Terms",p&&e.jsxs(e.Fragment,{children:[" ","and"," ",e.jsx("a",{href:p,target:"_blank",rel:"noopener noreferrer",children:"Privacy Policy"})]})," *"]})]}),e.jsx("button",{type:"submit",className:"nice-btn nice-btn--primary nice-register-form__submit",disabled:!A,children:s?"…":"Register"}),a&&e.jsxs("div",{className:"nice-register-form__login",children:[e.jsx("span",{children:"Already have an account? "}),e.jsx("button",{type:"button",onClick:a,children:"Sign in"})]}),r&&e.jsx("div",{className:"nice-register-form__footer",children:r})]})});U.displayName="NiceRegistrationForm";const W=t.forwardRef(({requireCurrentPassword:c=!0,onSubmit:s,loading:n=!1,error:a,success:i,minPasswordStrength:l=2,title:o="Change Password",className:p,style:d},y)=>{const[r,v]=t.useState(""),[g,k]=t.useState(""),[x,R]=t.useState(""),u=B(g),m=g===x,f=(!c||r.length>0)&&g.length>0&&m&&u>=l&&!n,S=t.useCallback(b=>{b.preventDefault(),f&&(s==null||s({currentPassword:c?r:void 0,newPassword:g}))},[f,r,g,c,s]);return e.jsxs("form",{ref:y,className:`nice-change-password${p?` ${p}`:""}`,style:d,onSubmit:S,noValidate:!0,children:[e.jsx("h3",{className:"nice-change-password__title",children:o}),a&&e.jsx("div",{className:"nice-change-password__error",role:"alert",children:a}),i&&e.jsx("div",{className:"nice-change-password__success",role:"status",children:i}),c&&e.jsxs("div",{className:"nice-change-password__field",children:[e.jsx("label",{htmlFor:"nice-cp-current",children:"Current password"}),e.jsx("input",{id:"nice-cp-current",className:"nice-input",type:"password",value:r,onChange:b=>v(b.target.value),autoComplete:"current-password",disabled:n,required:!0})]}),e.jsxs("div",{className:"nice-change-password__field",children:[e.jsx("label",{htmlFor:"nice-cp-new",children:"New password"}),e.jsx("input",{id:"nice-cp-new",className:"nice-input",type:"password",value:g,onChange:b=>k(b.target.value),autoComplete:"new-password",disabled:n,required:!0}),g.length>0&&e.jsx(K,{password:g})]}),e.jsxs("div",{className:"nice-change-password__field",children:[e.jsx("label",{htmlFor:"nice-cp-confirm",children:"Confirm new password"}),e.jsx("input",{id:"nice-cp-confirm",className:"nice-input",type:"password",value:x,onChange:b=>R(b.target.value),autoComplete:"new-password",disabled:n,required:!0}),x&&!m&&e.jsx("span",{className:"nice-error-text",children:"Passwords do not match"})]}),e.jsx("button",{type:"submit",className:"nice-btn nice-btn--primary",disabled:!f,children:n?"…":"Change Password"})]})});W.displayName="NiceChangePassword";const J=t.forwardRef(({secret:c,qrCodeUrl:s,issuer:n,accountName:a,onVerify:i,loading:l=!1,error:o,onCancel:p,recoveryCodes:d,step:y="setup",onStepChange:r,className:v,style:g},k)=>{const[x,R]=t.useState(""),[u,m]=t.useState(!1),[f,S]=t.useState(!1),b=t.useCallback(w=>{w.preventDefault(),x.length>=6&&(i==null||i(x))},[x,i]),F=`otpauth://totp/${n?`${n}:`:""}${a??"user"}?secret=${c}${n?`&issuer=${n}`:""}&digits=6&period=30`;return e.jsxs("div",{ref:k,className:`nice-2fa-setup${v?` ${v}`:""}`,style:g,children:[e.jsx("h3",{className:"nice-2fa-setup__title",children:"Two-Factor Authentication"}),y==="setup"&&e.jsxs("div",{className:"nice-2fa-setup__step",children:[e.jsx("p",{className:"nice-2fa-setup__instruction",children:"Scan this QR code with your authenticator app (Google Authenticator, Authy, etc.):"}),s?e.jsx("img",{src:s,alt:"2FA QR Code",className:"nice-2fa-setup__qr"}):e.jsxs("div",{className:"nice-2fa-setup__qr-placeholder",children:[e.jsx("span",{children:"QR Code"}),e.jsx("code",{className:"nice-2fa-setup__uri",children:F})]}),e.jsxs("div",{className:"nice-2fa-setup__secret",children:[e.jsx("span",{children:"Or enter manually: "}),u?e.jsx("code",{className:"nice-2fa-setup__secret-key",children:c}):e.jsx("button",{type:"button",onClick:()=>m(!0),children:"Show secret key"})]}),e.jsx("button",{type:"button",className:"nice-btn nice-btn--primary",onClick:()=>r==null?void 0:r("verify"),children:"Next — Verify"})]}),y==="verify"&&e.jsxs("form",{className:"nice-2fa-setup__step",onSubmit:b,children:[e.jsx("p",{className:"nice-2fa-setup__instruction",children:"Enter the 6-digit code from your authenticator app:"}),o&&e.jsx("div",{className:"nice-2fa-setup__error",role:"alert",children:o}),e.jsx("input",{className:"nice-input nice-2fa-setup__code-input",type:"text",inputMode:"numeric",maxLength:6,value:x,onChange:w=>R(w.target.value.replace(/\D/g,"").slice(0,6)),placeholder:"000000",autoFocus:!0,disabled:l,autoComplete:"one-time-code"}),e.jsxs("div",{className:"nice-2fa-setup__actions",children:[e.jsx("button",{type:"button",className:"nice-btn",onClick:()=>r==null?void 0:r("setup"),children:"Back"}),e.jsx("button",{type:"submit",className:"nice-btn nice-btn--primary",disabled:l||x.length<6,children:l?"…":"Verify"})]})]}),y==="recovery"&&d&&e.jsxs("div",{className:"nice-2fa-setup__step",children:[e.jsx("p",{className:"nice-2fa-setup__instruction",children:"Save these recovery codes in a safe place. Each code can only be used once:"}),e.jsx("div",{className:"nice-2fa-setup__recovery-codes",children:d.map((w,M)=>e.jsx("code",{className:"nice-2fa-setup__recovery-code",children:w},M))}),e.jsx("button",{type:"button",className:"nice-btn",onClick:()=>{var w;(w=navigator.clipboard)==null||w.writeText(d.join(`
|
|
2
|
+
`)),S(!0)},children:f?"Copied!":"Copy all codes"}),p&&e.jsx("button",{type:"button",className:"nice-btn nice-btn--primary",onClick:p,children:"Done"})]}),p&&y!=="recovery"&&e.jsx("button",{type:"button",className:"nice-2fa-setup__cancel",onClick:p,children:"Cancel"})]})});J.displayName="NiceTwoFaSetup";const ie=500;function P(c,s){return Math.floor(Math.random()*(s-c+1))+c}function re(){return`${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`}function le(c,s){switch(c){case"math":{const n=s==="easy"?5:s==="hard"?20:10,a=P(1,n),i=P(1,n),l=s!=="easy"&&a>=i&&Math.random()<.5,o=l?a-i:a+i;return{kind:"math",prompt:`${a} ${l?"−":"+"} ${i}`,answer:o}}case"text":{const n=s==="easy"?4:s==="hard"?7:5,a="ABCDEFGHJKMNPQRSTUVWXYZ23456789";let i="";for(let l=0;l<n;l++)i+=a[P(0,a.length-1)];return{kind:"text",text:i}}case"slider":return{kind:"slider",target:P(70,92),tolerance:s==="easy"?8:s==="hard"?2:4};case"checkbox":default:return{kind:"checkbox"}}}function oe(c,s,n){if(!c)return;const a=c.getContext("2d");if(!a)return;const i=c.width,l=c.height;a.clearRect(0,0,i,l),a.fillStyle=n?"#1a1a1a":"#f3f4f6",a.fillRect(0,0,i,l),a.strokeStyle=n?"rgba(255,255,255,0.18)":"rgba(0,0,0,0.18)",a.lineWidth=1;for(let p=0;p<6;p++)a.beginPath(),a.moveTo(P(0,i),P(0,l)),a.lineTo(P(0,i),P(0,l)),a.stroke();a.fillStyle=n?"#e5e7eb":"#111827",a.textBaseline="middle";const o=i/(s.length+1);for(let p=0;p<s.length;p++){const d=P(22,30);a.save(),a.translate(o*(p+1),l/2+P(-4,4)),a.rotate(P(-22,22)*Math.PI/180),a.font=`bold ${d}px monospace`,a.fillText(s[p],-d/3,0),a.restore()}}const Z=t.forwardRef((c,s)=>{const{variant:n,difficulty:a="medium",refreshable:i=!0,verifyToken:l,expiresMs:o,label:p="I'm not a robot",provider:d="custom",siteKey:y,onVerify:r,onExpire:v,onError:g,onRefresh:k,theme:x="light",size:R="normal",customRender:u,className:m,style:f}=c,S=t.useRef(null),b=t.useRef(null),F=t.useRef(),[w,M]=t.useState(0),[N,T]=t.useState("idle"),[I,C]=t.useState(""),[A,h]=t.useState(""),[_,V]=t.useState(0),$=t.useMemo(()=>le(n??"checkbox",a),[n,a,w]),O=t.useCallback(()=>{T("idle"),C(""),h(""),V(0),M(j=>j+1)},[]),z=t.useCallback(()=>{O(),k==null||k()},[O,k]);t.useImperativeHandle(s,()=>({reset:O,refresh:z,container:S.current}),[O,z]),t.useEffect(()=>{n==="text"&&$.kind==="text"&&oe(b.current,$.text,x==="dark")},[n,$,x]),t.useEffect(()=>{if(N!=="verified"||!o)return;const j=setTimeout(()=>{v==null||v(),O()},o);return()=>clearTimeout(j)},[N,o,v,O]);const L=t.useCallback(()=>{const j=`ntd-cap:${n}:${re()}`;l?(T("verifying"),C(""),Promise.resolve().then(()=>l(j)).then(H=>{H?(T("verified"),r==null||r(j)):(T("error"),C("Verification failed. Please try again."),M(se=>se+1),h(""))}).catch(H=>{T("error"),C("Verification error. Please try again."),g==null||g(H)})):(T("verified"),r==null||r(j))},[n,l,r,g]),G=t.useCallback(()=>{T("error"),C("Incorrect, please try again."),M(j=>j+1),h("")},[]),Y=t.useCallback(()=>{N==="verifying"||N==="verified"||(T("verifying"),C(""),F.current=setTimeout(L,ie))},[N,L]);t.useEffect(()=>()=>clearTimeout(F.current),[]);const q=t.useCallback(()=>{$.kind==="math"?A.trim()!==""&&Number(A)===$.answer?L():G():$.kind==="text"&&(A.trim().toUpperCase()===$.text?L():G())},[$,A,L,G]),ee=t.useCallback(j=>{V(j),!($.kind!=="slider"||N==="verified"||N==="verifying")&&Math.abs(j-$.target)<=$.tolerance&&L()},[$,N,L]);if(!n)return d==="custom"&&u?e.jsx("div",{ref:S,className:`nice-captcha nice-captcha--custom${m?` ${m}`:""}`,style:f,children:u}):e.jsx("div",{ref:S,className:`nice-captcha nice-captcha--${d} nice-captcha--${x}${R==="compact"?" nice-captcha--compact":""}${m?` ${m}`:""}`,style:f,"data-sitekey":y,"data-theme":x,"data-size":R,children:e.jsxs("div",{className:"nice-captcha__placeholder",children:[e.jsxs("span",{children:["🔒 Captcha (",d,")"]}),e.jsxs("p",{children:["Load the ",d," SDK to activate verification."]})]})});const D=N==="verifying",E=N==="verified",te=`nice-captcha nice-captcha--${n} nice-captcha--${x} nice-captcha--${N}${R==="compact"?" nice-captcha--compact":""}${m?` ${m}`:""}`;return e.jsxs("div",{ref:S,className:te,style:f,role:"group","aria-label":"Captcha challenge","data-variant":n,"data-status":N,"data-theme":x,children:[n==="checkbox"&&e.jsxs("button",{type:"button",className:"nice-captcha__checkbox",role:"checkbox","aria-checked":E,"aria-busy":D,disabled:D||E,onClick:Y,children:[e.jsx("span",{className:"nice-captcha__box","aria-hidden":"true",children:D?"…":E?"✓":""}),e.jsx("span",{className:"nice-captcha__label",children:p})]}),n==="math"&&$.kind==="math"&&e.jsxs("div",{className:"nice-captcha__challenge",children:[e.jsxs("span",{className:"nice-captcha__prompt","aria-hidden":"true",children:[$.prompt," ="]}),e.jsx("input",{className:"nice-input nice-captcha__answer",type:"number",inputMode:"numeric",value:A,"aria-label":`What is ${$.prompt}?`,disabled:D||E,onChange:j=>h(j.target.value),onKeyDown:j=>{j.key==="Enter"&&(j.preventDefault(),q())}}),e.jsx("button",{type:"button",className:"nice-btn nice-btn--primary nice-captcha__verify",disabled:D||E||A.trim()==="",onClick:q,children:D?"…":"Verify"})]}),n==="text"&&$.kind==="text"&&e.jsxs("div",{className:"nice-captcha__challenge",children:[e.jsx("canvas",{ref:b,className:"nice-captcha__canvas",width:160,height:56,"aria-hidden":"true"}),e.jsx("input",{className:"nice-input nice-captcha__answer",type:"text",autoCapitalize:"characters",autoComplete:"off",value:A,"aria-label":"Type the characters shown in the image",disabled:D||E,onChange:j=>h(j.target.value),onKeyDown:j=>{j.key==="Enter"&&(j.preventDefault(),q())}}),e.jsx("button",{type:"button",className:"nice-btn nice-btn--primary nice-captcha__verify",disabled:D||E||A.trim()==="",onClick:q,children:D?"…":"Verify"})]}),n==="slider"&&$.kind==="slider"&&e.jsxs("div",{className:"nice-captcha__challenge nice-captcha__challenge--slider",children:[e.jsx("span",{className:"nice-captcha__slider-hint","aria-hidden":"true",children:E?"Verified ✓":"Slide to the marker"}),e.jsxs("div",{className:"nice-captcha__track",children:[e.jsx("span",{className:"nice-captcha__target","aria-hidden":"true",style:{left:`${$.target}%`}}),e.jsx("input",{className:"nice-captcha__slider",type:"range",min:0,max:100,value:_,"aria-label":"Slide the handle to the marker to verify","aria-valuetext":`${_}%`,disabled:E||D,onChange:j=>ee(Number(j.target.value))})]})]}),e.jsxs("div",{className:"nice-captcha__footer",children:[e.jsx("div",{className:"nice-captcha__message",role:"status","aria-live":"polite",children:I}),i&&e.jsx("button",{type:"button",className:"nice-captcha__refresh","aria-label":"Refresh challenge",title:"Refresh challenge",onClick:z,children:"↻"})]})]})});Z.displayName="NiceCaptcha";const de=[{id:"google",label:"Google",icon:"🔵",color:"#4285f4",textColor:"#fff"},{id:"microsoft",label:"Microsoft",icon:"🟦",color:"#00a4ef",textColor:"#fff"},{id:"discord",label:"Discord",icon:"💬",color:"#5865f2",textColor:"#fff"},{id:"spotify",label:"Spotify",icon:"🎵",color:"#1db954",textColor:"#fff"},{id:"github",label:"GitHub",icon:"🐙",color:"#24292e",textColor:"#fff"}],X=t.forwardRef(({providers:c=de,onProviderClick:s,disabled:n=!1,loadingProvider:a,direction:i="vertical",separatorText:l="or continue with",showSeparator:o=!0,className:p,style:d},y)=>e.jsxs("div",{ref:y,className:`nice-oauth nice-oauth--${i}${p?` ${p}`:""}`,style:d,children:[o&&e.jsxs("div",{className:"nice-oauth__separator",children:[e.jsx("hr",{}),e.jsx("span",{children:l}),e.jsx("hr",{})]}),e.jsx("div",{className:"nice-oauth__buttons",children:c.map(r=>e.jsxs("button",{type:"button",className:`nice-oauth__btn${a===r.id?" nice-oauth__btn--loading":""}`,style:{backgroundColor:r.color,color:r.textColor},onClick:()=>s==null?void 0:s(r.id),disabled:n||!!a,children:[r.icon&&e.jsx("span",{className:"nice-oauth__btn-icon",children:r.icon}),e.jsx("span",{className:"nice-oauth__btn-label",children:a===r.id?"…":r.label})]},r.id))})]}));X.displayName="NiceOAuthButtons";const ue=t.forwardRef(function(s,n){const{tokens:a,onCreate:i,onRevoke:l,availableScopes:o=[],maxTokens:p=10,title:d="API Tokens",className:y,style:r}=s,[v,g]=t.useState(!1),[k,x]=t.useState(""),[R,u]=t.useState([]),[m,f]=t.useState("90"),[S,b]=t.useState(null),[F,w]=t.useState(!1),[M,N]=t.useState(null),T=t.useCallback(async()=>{if(!(!i||!k.trim())){w(!0);try{const h=m?parseInt(m,10):void 0,_=await i(k.trim(),R,h);b(_),x(""),u([]),g(!1)}finally{w(!1)}}},[i,k,R,m]),I=t.useCallback(async h=>{if(l){N(h);try{await l(h)}finally{N(null)}}},[l]),C=h=>{u(_=>_.includes(h)?_.filter(V=>V!==h):[..._,h])},A=a.length<p;return e.jsxs("div",{ref:n,className:`nice-token-management ${y??""}`,style:r,children:[e.jsxs("div",{className:"nice-token-management__header",children:[e.jsx("h3",{children:d}),A&&e.jsx("button",{className:"nice-token-management__new-btn",onClick:()=>g(!0),disabled:v,children:"+ New Token"})]}),S&&e.jsxs("div",{className:"nice-token-management__reveal",children:[e.jsxs("p",{children:[e.jsx("strong",{children:"New token created!"})," Copy it now — it won't be shown again."]}),e.jsx("code",{className:"nice-token-management__token-value",children:S}),e.jsx("button",{onClick:()=>b(null),children:"Dismiss"})]}),v&&e.jsxs("div",{className:"nice-token-management__create-form",children:[e.jsxs("label",{children:["Name",e.jsx("input",{type:"text",value:k,onChange:h=>x(h.target.value),placeholder:"My API token",maxLength:64})]}),o.length>0&&e.jsxs("fieldset",{children:[e.jsx("legend",{children:"Scopes"}),o.map(h=>e.jsxs("label",{className:"nice-token-management__scope",children:[e.jsx("input",{type:"checkbox",checked:R.includes(h),onChange:()=>C(h)}),h]},h))]}),e.jsxs("label",{children:["Expires in (days)",e.jsx("input",{type:"number",value:m,onChange:h=>f(h.target.value),min:1,max:365})]}),e.jsxs("div",{className:"nice-token-management__create-actions",children:[e.jsx("button",{onClick:T,disabled:F||!k.trim(),children:F?"Creating…":"Create Token"}),e.jsx("button",{onClick:()=>g(!1),children:"Cancel"})]})]}),a.length===0?e.jsx("p",{className:"nice-token-management__empty",children:"No API tokens yet."}):e.jsxs("table",{className:"nice-token-management__table",children:[e.jsx("thead",{children:e.jsxs("tr",{children:[e.jsx("th",{children:"Name"}),e.jsx("th",{children:"Prefix"}),e.jsx("th",{children:"Created"}),e.jsx("th",{children:"Expires"}),e.jsx("th",{children:"Last Used"}),e.jsx("th",{children:"Scopes"}),e.jsx("th",{})]})}),e.jsx("tbody",{children:a.map(h=>{var _;return e.jsxs("tr",{children:[e.jsx("td",{children:h.name}),e.jsx("td",{children:e.jsxs("code",{children:[h.prefix,"…"]})}),e.jsx("td",{children:h.createdAt}),e.jsx("td",{children:h.expiresAt??"—"}),e.jsx("td",{children:h.lastUsedAt??"Never"}),e.jsx("td",{children:((_=h.scopes)==null?void 0:_.join(", "))||"—"}),e.jsx("td",{children:e.jsx("button",{className:"nice-token-management__revoke-btn",onClick:()=>I(h.id),disabled:M===h.id,children:M===h.id?"Revoking…":"Revoke"})})]},h.id)})})]})]})});exports.NiceCaptcha=Z;exports.NiceChangePassword=W;exports.NiceLoginForm=Q;exports.NiceOAuthButtons=X;exports.NicePasswordStrength=K;exports.NiceRegistrationForm=U;exports.NiceTokenManagement=ue;exports.NiceTwoFaSetup=J;exports.calcPasswordStrength=B;exports.useAuth=ae;
|
|
3
3
|
//# sourceMappingURL=index.cjs.map
|