@donotdev/auth 0.2.1 → 0.2.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.
Files changed (51) hide show
  1. package/dist/AuthStore.d.ts.map +1 -1
  2. package/dist/AuthStore.js +1 -1
  3. package/dist/authHelpers.js +1 -1
  4. package/dist/components/AccountLinking.js +1 -1
  5. package/dist/components/AuthFallback.js +1 -1
  6. package/dist/components/AuthPartnerButton.d.ts.map +1 -1
  7. package/dist/components/AuthPartnerButton.js +1 -1
  8. package/dist/components/DeleteAccountDialogs.js +1 -1
  9. package/dist/components/EmailLinkForm.js +1 -1
  10. package/dist/components/EmailPasswordForm.d.ts +8 -18
  11. package/dist/components/EmailPasswordForm.d.ts.map +1 -1
  12. package/dist/components/EmailPasswordForm.js +1 -2
  13. package/dist/components/EmailVerification.js +1 -1
  14. package/dist/components/Feature.js +1 -1
  15. package/dist/components/FeatureGuard.js +1 -1
  16. package/dist/components/LoginModal.js +1 -1
  17. package/dist/components/MultipleAuthProviders.js +1 -1
  18. package/dist/components/PasswordResetCallback.d.ts.map +1 -1
  19. package/dist/components/PasswordResetCallback.js +1 -2
  20. package/dist/components/PasswordStrengthIndicator.d.ts.map +1 -1
  21. package/dist/components/PasswordStrengthIndicator.js +1 -1
  22. package/dist/constants.d.ts.map +1 -1
  23. package/dist/emailAuth/fields.d.ts +108 -0
  24. package/dist/emailAuth/fields.d.ts.map +1 -0
  25. package/dist/emailAuth/fields.js +1 -0
  26. package/dist/emailAuth/flows.d.ts +120 -0
  27. package/dist/emailAuth/flows.d.ts.map +1 -0
  28. package/dist/emailAuth/flows.js +1 -0
  29. package/dist/emailAuth/rules.d.ts +36 -0
  30. package/dist/emailAuth/rules.d.ts.map +1 -0
  31. package/dist/emailAuth/rules.js +2 -0
  32. package/dist/hooks/useAuthMethodToggle.js +1 -1
  33. package/dist/hooks/useAuthPartner.d.ts.map +1 -1
  34. package/dist/hooks/useAuthPartner.js +1 -1
  35. package/dist/hooks/useDeleteAccount.d.ts +15 -1
  36. package/dist/hooks/useDeleteAccount.d.ts.map +1 -1
  37. package/dist/hooks/useDeleteAccount.js +1 -1
  38. package/dist/hooks/useEmailLinkForm.js +1 -1
  39. package/dist/hooks/useGoogleOneTap.d.ts +2 -1
  40. package/dist/hooks/useGoogleOneTap.d.ts.map +1 -1
  41. package/dist/hooks/useGoogleOneTap.js +1 -1
  42. package/dist/hooks/useNetworkStatus.d.ts.map +1 -1
  43. package/dist/index.d.ts +6 -1
  44. package/dist/index.d.ts.map +1 -1
  45. package/dist/index.js +1 -1
  46. package/dist/symbol-index.json +1 -1
  47. package/dist/tsconfig.tsbuildinfo +1 -1
  48. package/dist/useAuth.d.ts.map +1 -1
  49. package/dist/useAuth.js +1 -1
  50. package/guidelines/AUTH.md +14 -0
  51. package/package.json +5 -5
@@ -1 +1 @@
1
- {"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../src/useAuth.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAKV,OAAO,EAER,MAAM,gBAAgB,CAAC;AAmKxB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AACH,wBAAgB,OAAO,CAAC,CAAC,SAAS,MAAM,OAAO,EAAE,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAsXnE"}
1
+ {"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../src/useAuth.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAKV,OAAO,EAER,MAAM,gBAAgB,CAAC;AA0MxB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AACH,wBAAgB,OAAO,CAAC,CAAC,SAAS,MAAM,OAAO,EAAE,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAuYnE"}
package/dist/useAuth.js CHANGED
@@ -1 +1 @@
1
- "use client";import{useEffect as H,useCallback as s,useRef as J}from"react";import{DEGRADED_AUTH_API as w,FEATURE_STATUS as l,handleError as K,hasProvider as Q,getProvider as X,isClient as i,USER_ROLES as R,SUBSCRIPTION_TIERS as ee,hasRoleAccess as y,hasTierAccess as te,getGlobalSingleton as ne,useAuthSessionSnapshot as ie}from"@donotdev/core";import{useAuthStore as u}from"./AuthStore";import{getAuthMethod as C}from"./constants";const e=ne("auth-provider",()=>({instance:null,initializing:!1,errorNotified:!1}));function se(){const a=n=>{const c=u.getState();if(y(c.user?.role,R.ADMIN))return!0;const o=c.userProfile?.permissions;return o?o.includes(n):!1};return{navigate:n=>{const c=u.getState();return n===!1?!0:n===!0?!!c.user:!(n.required&&!c.user||n.role&&!y(c.user?.role,n.role)||n.tier&&!te(c.userSubscription?.tier,n.tier)||n.validate&&c.userSubscription&&c.user?.role&&!n.validate(c.user.role,c.userSubscription.tier))},view:n=>a(`${n}:view`),edit:n=>a(`${n}:edit`),delete:n=>a(`${n}:delete`),create:n=>a(`${n}:create`),has:n=>a(n),perform:(n,c)=>{const o=u.getState();if(y(o.user?.role,R.ADMIN))return!0;if(!a(n))return!1;if(c){const d=o.userProfile?.capabilities;if(d?.[n]){const h=d[n];for(const[g,m]of Object.entries(c))if(typeof h[g]=="number"&&typeof m=="number"&&m>h[g])return!1}}return!0}}}const ae=se(),b=new Set(["user","loading","error","status","userProfile","userSubscription","emailVerification","passwordResetPending","partnerError","getPartnerState","setPartnerState"]);function re(a){const n=J(null);H(()=>{if(!i())return;const{status:t}=u.getState();return t===l.READY||e.initializing?void 0:(e.initializing=!0,(async()=>{try{if(Q("auth"))e.instance=X("auth");else{const{getFirebaseAuth:r}=await import("@donotdev/firebase");e.instance=r()}if(!e.instance)return;"setStore"in e.instance&&e.instance.setStore(u.getState()),u.getState().setAuthService(e.instance),await e.instance.initialize(),n.current=e.instance.onAuthStateChanged(()=>{}),e.errorNotified=!1}catch(r){u.getState().setStatus(l.DEGRADED),e.errorNotified||(K(r,{userMessage:"Authentication initialization failed",context:{operation:"useAuth.initialize"},severity:"warning",showNotification:!1}),e.errorNotified=!0)}finally{e.initializing=!1}})(),()=>{n.current?.(),n.current=null,e.initializing=!1})},[]);const c=s(async(t,r)=>!i()||!e.instance?null:e.instance.signInWithEmail(t,r),[]),o=s(async(t,r,f)=>!i()||!e.instance?null:e.instance.createUserWithEmail(t,r,f),[]),d=s(async(t,r)=>{if(!i()||!e.instance)return null;const f=C(r);return e.instance.signInWithPartner(t,f)},[]),h=s(async(t,r)=>{if(!i()||!e.instance)return null;const f=C(r);return e.instance.linkWithPartner(t,f)},[]),g=s(async t=>!i()||!e.instance?null:e.instance.signInWithGoogleCredential(t),[]),m=s(async()=>{if(!(!i()||!e.instance))return e.instance.signOut()},[]),W=s(async t=>{if(!(!i()||!e.instance))return e.instance.sendPasswordResetEmail(t)},[]),v=s(async t=>{if(!(!i()||!e.instance))return e.instance.updatePassword(t)},[]),T=s(async t=>!i()||!e.instance?.verifyPasswordResetCode?null:e.instance.verifyPasswordResetCode(t),[]),D=s(async(t,r)=>{if(!(!i()||!e.instance?.confirmPasswordReset))return e.instance.confirmPasswordReset(t,r)},[]),N=s(async()=>{if(!(!i()||!e.instance))return e.instance.sendEmailVerification()},[]),U=s(async(t,r)=>{if(!(!i()||!e.instance))return e.instance.sendSignInLinkToEmail(t,r)},[]),G=s(async(t,r)=>!i()||!e.instance?null:e.instance.signInWithEmailLink(t,r),[]),k=s(t=>!i()||!e.instance?!1:e.instance.isSignInWithEmailLink(t),[]),L=s(async()=>!i()||!e.instance?null:e.instance.getCurrentUser(),[]),z=s(async()=>!i()||!e.instance?{status:"pending"}:e.instance.getEmailVerificationStatus(),[]),M=s(async()=>!i()||!e.instance?!1:e.instance.isEmailVerificationEnabled(),[]),V=s(async t=>!i()||!e.instance?!1:e.instance.hasRole(t),[]),O=s(async t=>!i()||!e.instance?!1:e.instance.hasTier(t),[]),F=s(async t=>!i()||!e.instance?!1:e.instance.hasFeature(t),[]),_=s(()=>i()?u.getState().getCustomClaims():{},[]),$=s(t=>i()?u.getState().getCustomClaim(t):null,[]),x=s(async t=>{if(!(!i()||!e.instance))return e.instance.reauthenticateWithPassword(t)},[]),Y=s(async(t,r)=>{if(!(!i()||!e.instance))return e.instance.reauthenticateWithProvider(t,r)},[]),Z=s(async t=>{if(!(!i()||!e.instance))return e.instance.deleteAccount(t)},[]),j=s(async t=>{if(!(!i()||!e.instance?.setRememberMe))return e.instance.setRememberMe(t)},[]),A={signInWithEmail:c,createUserWithEmail:o,signInWithPartner:d,linkWithPartner:h,signInWithGoogleCredential:g,signOut:m,sendPasswordResetEmail:W,updatePassword:v,verifyPasswordResetCode:T,confirmPasswordReset:D,sendEmailVerification:N,getEmailVerificationStatus:z,isEmailVerificationEnabled:M,sendSignInLinkToEmail:U,signInWithEmailLink:G,isSignInWithEmailLink:k,getCurrentUser:L,reauthenticateWithPassword:x,reauthenticateWithProvider:Y,deleteAccount:Z,setRememberMe:j,hasRole:V,hasTier:O,hasFeature:F,getCustomClaims:_,getCustomClaim:$},I=new Set(["isAuthenticated","userRole","userTier","can"]),q=u(t=>b.has(a)?t[a]:null),B=u(t=>I.has(a)?a==="isAuthenticated"?!!t.user:a==="userRole"?t.user?.role||R.GUEST:a==="userTier"?t.userSubscription?.tier||ee.FREE:a==="can"?ae:null:null),P=u(t=>t.status),S=ie(),p=P===l.INITIALIZING&&S!==null,E=p?l.READY:P;if(a==="status")return E;if(p){if(a==="user")return S.user;if(a==="isAuthenticated")return!!S.user;if(a==="userRole")return S.user?.role||R.GUEST}return E===l.INITIALIZING||E===l.DEGRADED||E===l.ERROR?w[a]:a==="isAvailable"?!0:b.has(a)?q:a in A?A[a]:I.has(a)?B:w[a]}export{re as useAuth};
1
+ "use client";import{useEffect as J,useCallback as s}from"react";import{DoNotDevError as K,ERROR_CODES as Q,DEGRADED_AUTH_API as C,FEATURE_STATUS as l,handleError as X,hasProvider as ee,getProvider as te,isClient as r,USER_ROLES as p,SUBSCRIPTION_TIERS as ne,hasRoleAccess as y,hasTierAccess as ie,getGlobalSingleton as se,useAuthSessionSnapshot as re}from"@donotdev/core";import{useAuthStore as c}from"./AuthStore";import{getAuthMethod as v}from"./constants";const e=se("auth-provider",()=>({instance:null,initializing:!1,errorNotified:!1,unsubscribe:null}));function ae(){const n=i=>{const u=c.getState();if(y(u.user?.role,p.ADMIN))return!0;const o=u.userProfile?.permissions;return o?o.includes(i):!1};return{navigate:i=>{const u=c.getState();return i===!1?!0:i===!0?!!u.user:!(i.required&&!u.user||i.role&&!y(u.user?.role,i.role)||i.tier&&!ie(u.userSubscription?.tier,i.tier)||i.validate&&u.userSubscription&&u.user?.role&&!i.validate(u.user.role,u.userSubscription.tier))},view:i=>n(`${i}:view`),edit:i=>n(`${i}:edit`),delete:i=>n(`${i}:delete`),create:i=>n(`${i}:create`),has:i=>n(i),perform:(i,u)=>{const o=c.getState();if(y(o.user?.role,p.ADMIN))return!0;if(!n(i))return!1;if(u){const d=o.userProfile?.capabilities;if(d?.[i]){const m=d[i];for(const[g,E]of Object.entries(u))if(typeof m[g]=="number"&&typeof E=="number"&&E>m[g])return!1}}return!0}}}let f=null;function ue(n){return f&&f.user===n.user&&f.userProfile===n.userProfile&&f.userSubscription===n.userSubscription||(f={user:n.user,userProfile:n.userProfile,userSubscription:n.userSubscription,api:ae()}),f.api}const T=new Set(["user","loading","error","status","userProfile","userSubscription","emailVerification","passwordResetPending","partnerError","getPartnerState","setPartnerState"]);function ce(n){return T.has(n)}function oe(n){J(()=>{if(!r())return;const{status:t}=c.getState();return t===l.READY||e.initializing?void 0:(e.initializing=!0,(async()=>{try{if(ee("auth"))e.instance=te("auth");else{const{getFirebaseAuth:a}=await import("@donotdev/firebase");e.instance=a()}if(!e.instance)return;"setStore"in e.instance&&e.instance.setStore(c.getState()),c.getState().setAuthService(e.instance),await e.instance.initialize(),e.unsubscribe||(e.unsubscribe=e.instance.onAuthStateChanged(()=>{})),e.errorNotified=!1}catch(a){c.getState().setStatus(l.DEGRADED),e.errorNotified||(X(a,{userMessage:"Authentication initialization failed",context:{operation:"useAuth.initialize"},severity:"warning",showNotification:!1}),e.errorNotified=!0)}finally{e.initializing=!1}})(),()=>{e.initializing=!1})},[]);const i=s(async(t,a)=>!r()||!e.instance?null:e.instance.signInWithEmail(t,a),[]),u=s(async(t,a,h)=>!r()||!e.instance?null:e.instance.createUserWithEmail(t,a,h),[]),o=s(async(t,a)=>{if(!r()||!e.instance)return null;const h=v(a);return e.instance.signInWithPartner(t,h)},[]),d=s(async(t,a)=>{if(!r()||!e.instance)return null;const h=v(a);return e.instance.linkWithPartner(t,h)},[]),m=s(async t=>!r()||!e.instance?null:e.instance.signInWithGoogleCredential(t),[]),g=s(async()=>{if(!(!r()||!e.instance))return e.instance.signOut()},[]),E=s(async t=>{if(!(!r()||!e.instance))return e.instance.sendPasswordResetEmail(t)},[]),W=s(async t=>{if(!(!r()||!e.instance))return e.instance.updatePassword(t)},[]),D=s(async t=>!r()||!e.instance?.verifyPasswordResetCode?null:e.instance.verifyPasswordResetCode(t),[]),N=s(async(t,a)=>{if(!(!r()||!e.instance?.confirmPasswordReset))return e.instance.confirmPasswordReset(t,a)},[]),M=s(async t=>{if(!(!r()||!e.instance))return e.instance.sendEmailVerification(t)},[]),U=s(async(t,a)=>{if(!(!r()||!e.instance))return e.instance.sendSignInLinkToEmail(t,a)},[]),G=s(async(t,a)=>!r()||!e.instance?null:e.instance.signInWithEmailLink(t,a),[]),L=s(t=>!r()||!e.instance?!1:e.instance.isSignInWithEmailLink(t),[]),k=s(async()=>!r()||!e.instance?null:e.instance.getCurrentUser(),[]),z=s(async()=>!r()||!e.instance?{status:"pending"}:e.instance.getEmailVerificationStatus(),[]),O=s(async()=>!r()||!e.instance?!1:e.instance.isEmailVerificationEnabled(),[]),V=s(async t=>!r()||!e.instance?!1:e.instance.hasRole(t),[]),_=s(async t=>!r()||!e.instance?!1:e.instance.hasTier(t),[]),F=s(async t=>!r()||!e.instance?!1:e.instance.hasFeature(t),[]),$=s(()=>r()?c.getState().getCustomClaims():{},[]),x=s(t=>r()?c.getState().getCustomClaim(t):null,[]),Y=s(async t=>{if(!(!r()||!e.instance))return e.instance.reauthenticateWithPassword(t)},[]),Z=s(async(t,a)=>{if(!(!r()||!e.instance))return e.instance.reauthenticateWithProvider(t,a)},[]),j=s(async t=>{if(!(!r()||!e.instance))return e.instance.deleteAccount(t)},[]),b=s(()=>{const t=e.instance;return!r()||!t?.setRememberMe?!1:t.supportsRememberMe?t.supportsRememberMe():!0},[]),q=s(async t=>{if(!b()||!e.instance?.setRememberMe)throw new K("Remember me is not supported by this auth provider",Q.UNIMPLEMENTED,{source:"auth"});return e.instance.setRememberMe(t)},[b]),P={signInWithEmail:i,createUserWithEmail:u,signInWithPartner:o,linkWithPartner:d,signInWithGoogleCredential:m,signOut:g,sendPasswordResetEmail:E,updatePassword:W,verifyPasswordResetCode:D,confirmPasswordReset:N,sendEmailVerification:M,getEmailVerificationStatus:z,isEmailVerificationEnabled:O,sendSignInLinkToEmail:U,signInWithEmailLink:G,isSignInWithEmailLink:L,getCurrentUser:k,reauthenticateWithPassword:Y,reauthenticateWithProvider:Z,deleteAccount:j,setRememberMe:q,canRememberMe:b,hasRole:V,hasTier:_,hasFeature:F,getCustomClaims:$,getCustomClaim:x},A=new Set(["isAuthenticated","userRole","userTier","can"]),B=c(t=>ce(n)?t[n]:null),H=c(t=>A.has(n)?n==="isAuthenticated"?!!t.user:n==="userRole"?t.user?.role||p.GUEST:n==="userTier"?t.userSubscription?.tier||ne.FREE:n==="can"?ue(t):null:null),I=c(t=>t.status),S=re(),w=I===l.INITIALIZING&&S!==null,R=w?l.READY:I;if(n==="status")return R;if(w){if(n==="user")return S.user;if(n==="isAuthenticated")return!!S.user;if(n==="userRole")return S.user?.role||p.GUEST}return R===l.INITIALIZING||R===l.DEGRADED||R===l.ERROR?C[n]:n==="isAvailable"?!0:T.has(n)?B:n in P?P[n]:A.has(n)?H:C[n]}export{oe as useAuth};
@@ -97,6 +97,20 @@ export const meta: PageMeta = {
97
97
 
98
98
  **SEO note:** Auth-guarded routes are still indexed by default. Add `noindex: true` to page meta if the route should be hidden from search engines.
99
99
 
100
+ ### Next + Supabase cookie session (generated proxy)
101
+
102
+ With `createBrowserClient` (session in cookies), `dndev` generates `src/proxy.next.ts`. On every page request (not `api`, `_next`, files):
103
+
104
+ - **No auth cookie** (`sb-<ref>-auth-token`, `hasSupabaseSessionCookie`): signed out, nothing verified or written; `auth` pages redirect to `authRoute`, every other page is served from the cache (see "Cached pages" below).
105
+ - **Auth cookie:** verifies the Supabase session and refreshes an expired one (`refreshSupabaseSession`, `@donotdev/supabase/server`, the @supabase/ssr `getClaims()` pattern), then redirects on the verified result only: `auth` pages send a signed-out visitor to `authRoute` (`?returnTo=`), `signedInRedirect` pages send a signed-in one to their target. Refresh failed / session revoked = signed out; Supabase unreachable = no redirect (the page's guards decide).
106
+ - Signed in or unknown: the page renders per request with the refreshed cookies (internal rewrite to `src/app/dndev-session/…`, `src/app/[lang]/dndev-session/…` with 2+ languages; a direct request to that path is a 404). The refreshed cookies are written on every response.
107
+
108
+ **Cached pages.** The generated `src/app/(site)` layout renders `ClientLayout` without request data: every page is static, served from the cache and re-rendered at most hourly (`revalidate = 3600`, ISR), one entry per URL (page and language). Guests, crawlers and LLMs get it; so does every visitor of an app whose session the server cannot read (Firebase, localStorage session: no per-request tree, no proxy needed for it). A page that reads the query string (`useSearchParams`, `useLocation().search`) renders that page in the browser; use `useRoutePath()` when only the path is needed. The framework generates the root layout (`src/app/layout.next.tsx`, `src/app/[lang]/layout.next.tsx` with 2+ languages) and the 404 pages; it renders the app's HTML shell `src/app/AppDocument.tsx` (cli `app-next` template) with the page's `<html lang>`. Both trees sit under it, so sign-in and sign-out stay client-side navigations. An `AppDocument` that renders `ClientLayout`, imports `next/headers` or exports `dynamic`, or an app-owned root layout / root `not-found`, fails the build.
109
+
110
+ Needs `NEXT_PUBLIC_SUPABASE_URL` and `NEXT_PUBLIC_SUPABASE_PUBLIC_KEY` (the proxy throws without them). Without a proxy, a cookie whose access token expired reads as "unknown": the first paint waits for the provider instead of showing a user the browser may no longer have.
111
+
112
+ **Query cache follows the identity.** When the signed-in user changes (sign-in, sign-out, other user, or the provider settling on another user than the server snapshot), every cached query is reset before the new identity renders (`syncQueryCacheIdentity`, called by the auth store). The provider confirming the server's user keeps the hydrated cache.
113
+
100
114
  ---
101
115
 
102
116
  ## Components
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@donotdev/auth",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -43,10 +43,10 @@
43
43
  "type-check": "bunx tsc --noEmit"
44
44
  },
45
45
  "peerDependencies": {
46
- "@donotdev/components": "^0.2.1",
47
- "@donotdev/core": "^0.2.1",
48
- "@donotdev/firebase": "^0.2.0",
49
- "@donotdev/supabase": "^0.2.0",
46
+ "@donotdev/components": "^0.2.3",
47
+ "@donotdev/core": "^0.2.3",
48
+ "@donotdev/firebase": "^0.2.3",
49
+ "@donotdev/supabase": "^0.2.3",
50
50
  "lucide-react": "*",
51
51
  "react": "^19.3.1",
52
52
  "react-dom": "^19.3.1",