@zhmdff/auth-react 1.0.2 → 1.0.4

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
@@ -18,6 +18,12 @@ interface AuthResult {
18
18
  refreshToken?: string;
19
19
  errorMessage?: string;
20
20
  user?: User;
21
+ /**
22
+ * Optional role and permission collections returned by the backend.
23
+ * These align with Zhmdff.Auth dynamic roles & permissions features.
24
+ */
25
+ roles?: string[];
26
+ permissions?: string[];
21
27
  }
22
28
  type AuthContextType = {
23
29
  accessToken: string | null;
@@ -51,13 +57,27 @@ declare const AuthProvider: ({ children, authUrl, apiUrl }: AuthProviderProps) =
51
57
  declare const useAuth: () => AuthContextType;
52
58
 
53
59
  type FetchOptions = {
54
- method?: "GET" | "POST" | "PUT" | "DELETE";
60
+ /**
61
+ * HTTP method. PATCH is included to support admin endpoints
62
+ * exposed by Zhmdff.Auth (e.g. /admin/users/{id}/roles).
63
+ */
64
+ method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
55
65
  body?: any;
56
66
  token?: string | null;
57
67
  onTokenRefresh?: (token: string) => void;
58
68
  onAuthFail?: () => void;
59
69
  apiUrl?: string;
60
70
  authUrl?: string;
71
+ /**
72
+ * Extra headers to merge into the request. Authorization and
73
+ * Content-Type headers set by this helper take precedence.
74
+ */
75
+ headers?: Record<string, string>;
76
+ /**
77
+ * When true, disables the automatic refresh-on-401 behavior.
78
+ * Useful for auth endpoints themselves (/auth/login, /auth/refresh, /auth/logout).
79
+ */
80
+ disableAutoRefresh?: boolean;
61
81
  };
62
82
  declare function apiFetch(endpoint: string, options?: FetchOptions): Promise<any>;
63
83
 
package/dist/index.d.ts CHANGED
@@ -18,6 +18,12 @@ interface AuthResult {
18
18
  refreshToken?: string;
19
19
  errorMessage?: string;
20
20
  user?: User;
21
+ /**
22
+ * Optional role and permission collections returned by the backend.
23
+ * These align with Zhmdff.Auth dynamic roles & permissions features.
24
+ */
25
+ roles?: string[];
26
+ permissions?: string[];
21
27
  }
22
28
  type AuthContextType = {
23
29
  accessToken: string | null;
@@ -51,13 +57,27 @@ declare const AuthProvider: ({ children, authUrl, apiUrl }: AuthProviderProps) =
51
57
  declare const useAuth: () => AuthContextType;
52
58
 
53
59
  type FetchOptions = {
54
- method?: "GET" | "POST" | "PUT" | "DELETE";
60
+ /**
61
+ * HTTP method. PATCH is included to support admin endpoints
62
+ * exposed by Zhmdff.Auth (e.g. /admin/users/{id}/roles).
63
+ */
64
+ method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
55
65
  body?: any;
56
66
  token?: string | null;
57
67
  onTokenRefresh?: (token: string) => void;
58
68
  onAuthFail?: () => void;
59
69
  apiUrl?: string;
60
70
  authUrl?: string;
71
+ /**
72
+ * Extra headers to merge into the request. Authorization and
73
+ * Content-Type headers set by this helper take precedence.
74
+ */
75
+ headers?: Record<string, string>;
76
+ /**
77
+ * When true, disables the automatic refresh-on-401 behavior.
78
+ * Useful for auth endpoints themselves (/auth/login, /auth/refresh, /auth/logout).
79
+ */
80
+ disableAutoRefresh?: boolean;
61
81
  };
62
82
  declare function apiFetch(endpoint: string, options?: FetchOptions): Promise<any>;
63
83
 
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var g=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var E=(o,t)=>{for(var i in t)g(o,i,{get:t[i],enumerable:!0})},U=(o,t,i,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of m(t))!P.call(o,s)&&s!==i&&g(o,s,{get:()=>t[s],enumerable:!(a=w(t,s))||a.enumerable});return o};var C=o=>U(g({},"__esModule",{value:!0}),o);var S={};E(S,{AuthContext:()=>A,AuthProvider:()=>$,apiFetch:()=>k,useAuth:()=>R});module.exports=C(S);var c=require("react");async function k(o,t={}){let{method:i="GET",body:a,token:s,onTokenRefresh:h,onAuthFail:f,apiUrl:T="/api",authUrl:y="/auth"}=t,n={"Content-Type":"application/json"};s&&(n.Authorization=`Bearer ${s}`);let d={method:i,headers:n,credentials:"include"};a&&i!=="GET"&&(d.body=JSON.stringify(a));let u=await fetch(`${T}${o}`,d);if(u.status===401&&s&&h)try{let l=await fetch(`${y}/refresh`,{method:"POST",credentials:"include"});if(l.ok){let r=(await l.json()).accessToken;h(r),n.Authorization=`Bearer ${r}`;let p=await fetch(`${T}${o}`,{...d,headers:n});if(!p.ok)throw new Error(await p.text());return p.json()}else throw f?.(),new Error("Session expired")}catch(l){throw f?.(),l}if(!u.ok){let l=await u.text(),e="An error occurred";try{let r=JSON.parse(l);e=r.message||r||e}catch{e=l||e}throw new Error(e)}return u.json()}var x=require("react/jsx-runtime"),A=(0,c.createContext)({accessToken:null,setAccessToken:()=>{},user:null,setUser:()=>{},isLoading:!0,checkAuth:async()=>!1,logout:async()=>{},fetch:async()=>{throw new Error("AuthContext.fetch not implemented")}}),$=({children:o,authUrl:t,apiUrl:i})=>{let[a,s]=(0,c.useState)(null),[h,f]=(0,c.useState)(null),[T,y]=(0,c.useState)(!0);(0,c.useEffect)(()=>{(async()=>(await u(),y(!1)))()},[]);let n=(e,r)=>{s(e),f(r)},d=async(e,r={})=>k(e,{...r,token:a,apiUrl:i||"",authUrl:t,onTokenRefresh:p=>{n(p,h)},onAuthFail:()=>{n(null,null)}}),u=async()=>{try{let e=await fetch(`${t}/refresh`,{method:"POST",credentials:"include"});if(!e.ok)return n(null,null),!1;let r=await e.json();return r.success&&r.accessToken&&r.user?(n(r.accessToken,r.user),!0):(n(null,null),!1)}catch{return n(null,null),!1}},l=async()=>{try{await fetch(`${t}/logout`,{method:"POST",credentials:"include",headers:a?{Authorization:`Bearer ${a}`}:{}})}catch(e){console.error("Logout failed:",e)}finally{n(null,null)}};return(0,x.jsx)(A.Provider,{value:{accessToken:a,setAccessToken:e=>n(e,h),user:h,setUser:f,isLoading:T,checkAuth:u,logout:l,fetch:d},children:o})},R=()=>(0,c.useContext)(A);0&&(module.exports={AuthContext,AuthProvider,apiFetch,useAuth});
1
+ "use strict";var k=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var E=(n,r)=>{for(var c in r)k(n,c,{get:r[c],enumerable:!0})},U=(n,r,c,s)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of R(r))!C.call(n,o)&&o!==c&&k(n,o,{get:()=>r[o],enumerable:!(s=P(r,o))||s.enumerable});return n};var $=n=>U(k({},"__esModule",{value:!0}),n);var F={};E(F,{AuthContext:()=>w,AuthProvider:()=>S,apiFetch:()=>x,useAuth:()=>v});module.exports=$(F);var i=require("react");async function x(n,r={}){let{method:c="GET",body:s,token:o,onTokenRefresh:h,onAuthFail:d,apiUrl:g="/api",authUrl:T="/auth",headers:a,disableAutoRefresh:A=!1}=r,u={"Content-Type":"application/json",...a||{}};o&&(u.Authorization=`Bearer ${o}`);let f={method:c,headers:u,credentials:"include"};s&&c!=="GET"&&(f.body=JSON.stringify(s));let t=await fetch(`${g}${n}`,f);if(!A&&t.status===401&&o&&h)try{let e=await fetch(`${T}/refresh`,{method:"POST",credentials:"include"});if(e.ok){let p=(await e.json()).accessToken;h(p),u.Authorization=`Bearer ${p}`;let y=await fetch(`${g}${n}`,{...f,headers:u});if(!y.ok)throw new Error(await y.text());return y.json()}else throw d?.(),new Error("Session expired")}catch(e){throw d?.(),e}if(!t.ok){let e=await t.text(),l="An error occurred";try{let p=JSON.parse(e);l=p.message||p||l}catch{l=e||l}throw new Error(l)}return t.json()}var m=require("react/jsx-runtime"),w=(0,i.createContext)({accessToken:null,setAccessToken:()=>{},user:null,setUser:()=>{},isLoading:!0,checkAuth:async()=>!1,logout:async()=>{},fetch:async()=>{throw new Error("AuthContext.fetch not implemented")}}),S=({children:n,authUrl:r,apiUrl:c})=>{let[s,o]=(0,i.useState)(null),[h,d]=(0,i.useState)(null),[g,T]=(0,i.useState)(!0);(0,i.useEffect)(()=>{(async()=>(await u(),T(!1)))()},[]);let a=(t,e)=>{o(t),d(e)},A=async(t,e={})=>x(t,{...e,token:s,apiUrl:c||"",authUrl:r,onTokenRefresh:l=>{a(l,h)},onAuthFail:()=>{a(null,null)}}),u=async()=>{try{let t=await fetch(`${r}/refresh`,{method:"POST",credentials:"include"});if(!t.ok)return a(null,null),!1;let e=await t.json();return e.success&&e.accessToken&&e.user?(a(e.accessToken,e.user),!0):(a(null,null),!1)}catch{return a(null,null),!1}},f=async()=>{try{await fetch(`${r}/logout`,{method:"POST",credentials:"include",headers:s?{Authorization:`Bearer ${s}`}:{}})}catch(t){console.error("Logout failed:",t)}finally{a(null,null)}};return(0,m.jsx)(w.Provider,{value:{accessToken:s,setAccessToken:t=>a(t,h),user:h,setUser:d,isLoading:g,checkAuth:u,logout:f,fetch:A},children:n})},v=()=>(0,i.useContext)(w);0&&(module.exports={AuthContext,AuthProvider,apiFetch,useAuth});
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{createContext as A,useContext as x,useState as y,useEffect as w}from"react";async function g(f,c={}){let{method:d="GET",body:o,token:i,onTokenRefresh:a,onAuthFail:l,apiUrl:p="/api",authUrl:T="/auth"}=c,r={"Content-Type":"application/json"};i&&(r.Authorization=`Bearer ${i}`);let u={method:d,headers:r,credentials:"include"};o&&d!=="GET"&&(u.body=JSON.stringify(o));let s=await fetch(`${p}${f}`,u);if(s.status===401&&i&&a)try{let n=await fetch(`${T}/refresh`,{method:"POST",credentials:"include"});if(n.ok){let t=(await n.json()).accessToken;a(t),r.Authorization=`Bearer ${t}`;let h=await fetch(`${p}${f}`,{...u,headers:r});if(!h.ok)throw new Error(await h.text());return h.json()}else throw l?.(),new Error("Session expired")}catch(n){throw l?.(),n}if(!s.ok){let n=await s.text(),e="An error occurred";try{let t=JSON.parse(n);e=t.message||t||e}catch{e=n||e}throw new Error(e)}return s.json()}import{jsx as m}from"react/jsx-runtime";var k=A({accessToken:null,setAccessToken:()=>{},user:null,setUser:()=>{},isLoading:!0,checkAuth:async()=>!1,logout:async()=>{},fetch:async()=>{throw new Error("AuthContext.fetch not implemented")}}),R=({children:f,authUrl:c,apiUrl:d})=>{let[o,i]=y(null),[a,l]=y(null),[p,T]=y(!0);w(()=>{(async()=>(await s(),T(!1)))()},[]);let r=(e,t)=>{i(e),l(t)},u=async(e,t={})=>g(e,{...t,token:o,apiUrl:d||"",authUrl:c,onTokenRefresh:h=>{r(h,a)},onAuthFail:()=>{r(null,null)}}),s=async()=>{try{let e=await fetch(`${c}/refresh`,{method:"POST",credentials:"include"});if(!e.ok)return r(null,null),!1;let t=await e.json();return t.success&&t.accessToken&&t.user?(r(t.accessToken,t.user),!0):(r(null,null),!1)}catch{return r(null,null),!1}},n=async()=>{try{await fetch(`${c}/logout`,{method:"POST",credentials:"include",headers:o?{Authorization:`Bearer ${o}`}:{}})}catch(e){console.error("Logout failed:",e)}finally{r(null,null)}};return m(k.Provider,{value:{accessToken:o,setAccessToken:e=>r(e,a),user:a,setUser:l,isLoading:p,checkAuth:s,logout:n,fetch:u},children:f})},S=()=>x(k);export{k as AuthContext,R as AuthProvider,g as apiFetch,S as useAuth};
1
+ import{createContext as w,useContext as m,useState as y,useEffect as P}from"react";async function k(d,i={}){let{method:f="GET",body:o,token:c,onTokenRefresh:a,onAuthFail:l,apiUrl:p="/api",authUrl:g="/auth",headers:r,disableAutoRefresh:T=!1}=i,s={"Content-Type":"application/json",...r||{}};c&&(s.Authorization=`Bearer ${c}`);let u={method:f,headers:s,credentials:"include"};o&&f!=="GET"&&(u.body=JSON.stringify(o));let t=await fetch(`${p}${d}`,u);if(!T&&t.status===401&&c&&a)try{let e=await fetch(`${g}/refresh`,{method:"POST",credentials:"include"});if(e.ok){let h=(await e.json()).accessToken;a(h),s.Authorization=`Bearer ${h}`;let A=await fetch(`${p}${d}`,{...u,headers:s});if(!A.ok)throw new Error(await A.text());return A.json()}else throw l?.(),new Error("Session expired")}catch(e){throw l?.(),e}if(!t.ok){let e=await t.text(),n="An error occurred";try{let h=JSON.parse(e);n=h.message||h||n}catch{n=e||n}throw new Error(n)}return t.json()}import{jsx as R}from"react/jsx-runtime";var x=w({accessToken:null,setAccessToken:()=>{},user:null,setUser:()=>{},isLoading:!0,checkAuth:async()=>!1,logout:async()=>{},fetch:async()=>{throw new Error("AuthContext.fetch not implemented")}}),v=({children:d,authUrl:i,apiUrl:f})=>{let[o,c]=y(null),[a,l]=y(null),[p,g]=y(!0);P(()=>{(async()=>(await s(),g(!1)))()},[]);let r=(t,e)=>{c(t),l(e)},T=async(t,e={})=>k(t,{...e,token:o,apiUrl:f||"",authUrl:i,onTokenRefresh:n=>{r(n,a)},onAuthFail:()=>{r(null,null)}}),s=async()=>{try{let t=await fetch(`${i}/refresh`,{method:"POST",credentials:"include"});if(!t.ok)return r(null,null),!1;let e=await t.json();return e.success&&e.accessToken&&e.user?(r(e.accessToken,e.user),!0):(r(null,null),!1)}catch{return r(null,null),!1}},u=async()=>{try{await fetch(`${i}/logout`,{method:"POST",credentials:"include",headers:o?{Authorization:`Bearer ${o}`}:{}})}catch(t){console.error("Logout failed:",t)}finally{r(null,null)}};return R(x.Provider,{value:{accessToken:o,setAccessToken:t=>r(t,a),user:a,setUser:l,isLoading:p,checkAuth:s,logout:u,fetch:T},children:d})},F=()=>m(x);export{x as AuthContext,v as AuthProvider,k as apiFetch,F as useAuth};
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@zhmdff/auth-react",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Plug and play authentication library for React/Next.js",
5
5
  "main": "./dist/index.js",
6
+ "private": false,
6
7
  "module": "./dist/index.mjs",
7
8
  "types": "./dist/index.d.ts",
8
9
  "files": [