@zhmdff/auth-react 1.0.5 → 1.0.7
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.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +8 -2
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var y=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var $=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var S=(n,t)=>{for(var s in t)y(n,s,{get:t[s],enumerable:!0})},b=(n,t,s,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of $(t))!v.call(n,r)&&r!==s&&y(n,r,{get:()=>t[r],enumerable:!(a=E(t,r))||a.enumerable});return n};var F=n=>b(y({},"__esModule",{value:!0}),n);var G={};S(G,{AuthContext:()=>R,AuthGuard:()=>U,AuthProvider:()=>O,apiFetch:()=>k,useAuth:()=>w});module.exports=F(G);var l=require("react");async function k(n,t={}){let{method:s="GET",body:a,token:r,onTokenRefresh:p,onAuthFail:i,apiUrl:h="/api",authUrl:
|
|
1
|
+
"use strict";var y=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var $=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var S=(n,t)=>{for(var s in t)y(n,s,{get:t[s],enumerable:!0})},b=(n,t,s,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of $(t))!v.call(n,r)&&r!==s&&y(n,r,{get:()=>t[r],enumerable:!(a=E(t,r))||a.enumerable});return n};var F=n=>b(y({},"__esModule",{value:!0}),n);var G={};S(G,{AuthContext:()=>R,AuthGuard:()=>U,AuthProvider:()=>O,apiFetch:()=>k,useAuth:()=>w});module.exports=F(G);var l=require("react");async function k(n,t={}){let{method:s="GET",body:a,token:r,onTokenRefresh:p,onAuthFail:i,apiUrl:h="/api",authUrl:d="/auth",headers:m,disableAutoRefresh:c=!1}=t,g={"Content-Type":"application/json",...m||{}};r&&(g.Authorization=`Bearer ${r}`);let A={method:s,headers:g,credentials:"include"};a&&s!=="GET"&&(A.body=JSON.stringify(a));let T=await fetch(`${h}${n}`,A);if(!c&&T.status===401&&r&&p)try{let e=await fetch(`${d}/refresh`,{method:"POST",credentials:"include"});if(e.ok){let f=(await e.json()).accessToken;p(f),g.Authorization=`Bearer ${f}`;let x=await fetch(`${h}${n}`,{...A,headers:g});if(!x.ok)throw new Error(await x.text());return x.json()}else throw i?.(),new Error("Session expired")}catch(e){throw i?.(),e}if(!T.ok){let e=await T.text(),o="An error occurred";try{let f=JSON.parse(e);o=f.errorMessage||f.message||f||o}catch{o=e||o}throw new Error(o)}return T.json()}var P=require("react/jsx-runtime"),R=(0,l.createContext)({accessToken:null,setAccessToken:()=>{},user:null,setUser:()=>{},isLoading:!0,checkAuth:async()=>!1,logout:async()=>{},fetch:async()=>{throw new Error("AuthContext.fetch not implemented")},loginPath:"/login"}),O=({children:n,authUrl:t,apiUrl:s,loginPath:a="/login"})=>{let[r,p]=(0,l.useState)(null),[i,h]=(0,l.useState)(null),[d,m]=(0,l.useState)(!0);(0,l.useEffect)(()=>{(async()=>(await A(),m(!1)))()},[]);let c=(e,o)=>{p(e),h(o)},g=async(e,o={})=>k(e,{...o,token:r,apiUrl:s||"",authUrl:t,onTokenRefresh:f=>{c(f,i)},onAuthFail:()=>{c(null,null)}}),A=async()=>{try{let e=await fetch(`${t}/refresh`,{method:"POST",credentials:"include"});if(!e.ok)return c(null,null),!1;let o=await e.json();return o.success&&o.accessToken&&o.user?(c(o.accessToken,o.user),!0):(c(null,null),!1)}catch{return c(null,null),!1}},T=async()=>{try{await fetch(`${t}/logout`,{method:"POST",credentials:"include",headers:r?{Authorization:`Bearer ${r}`}:{}})}catch(e){console.error("Logout failed:",e)}finally{c(null,null)}};return(0,P.jsx)(R.Provider,{value:{accessToken:r,setAccessToken:e=>c(e,i),user:i,setUser:h,isLoading:d,checkAuth:A,logout:T,fetch:g,loginPath:a},children:n})},w=()=>(0,l.useContext)(R);var C=require("react");var u=require("react/jsx-runtime"),U=({children:n,loadingComponent:t,onUnauthenticated:s,isPublic:a=!1,allowedRoles:r,forbiddenComponent:p})=>{let{user:i,isLoading:h,loginPath:d}=w();return(0,C.useEffect)(()=>{!h&&!i&&!a&&(s?s():d&&(window.location.href=d))},[i,h,a,s,d]),h?(0,u.jsx)(u.Fragment,{children:t||null}):!i&&!a?(0,u.jsx)(u.Fragment,{children:t||null}):i&&r&&r.length>0&&!r.includes(i.role)?(0,u.jsx)(u.Fragment,{children:p||null}):(0,u.jsx)(u.Fragment,{children:n})};0&&(module.exports={AuthContext,AuthGuard,AuthProvider,apiFetch,useAuth});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createContext as P,useContext as C,useState as y,useEffect as E}from"react";async function k(h,i={}){let{method:a="GET",body:c,token:r,onTokenRefresh:f,onAuthFail:o,apiUrl:s="/api",authUrl:
|
|
1
|
+
import{createContext as P,useContext as C,useState as y,useEffect as E}from"react";async function k(h,i={}){let{method:a="GET",body:c,token:r,onTokenRefresh:f,onAuthFail:o,apiUrl:s="/api",authUrl:u="/auth",headers:A,disableAutoRefresh:n=!1}=i,d={"Content-Type":"application/json",...A||{}};r&&(d.Authorization=`Bearer ${r}`);let p={method:a,headers:d,credentials:"include"};c&&a!=="GET"&&(p.body=JSON.stringify(c));let g=await fetch(`${s}${h}`,p);if(!n&&g.status===401&&r&&f)try{let e=await fetch(`${u}/refresh`,{method:"POST",credentials:"include"});if(e.ok){let l=(await e.json()).accessToken;f(l),d.Authorization=`Bearer ${l}`;let x=await fetch(`${s}${h}`,{...p,headers:d});if(!x.ok)throw new Error(await x.text());return x.json()}else throw o?.(),new Error("Session expired")}catch(e){throw o?.(),e}if(!g.ok){let e=await g.text(),t="An error occurred";try{let l=JSON.parse(e);t=l.errorMessage||l.message||l||t}catch{t=e||t}throw new Error(t)}return g.json()}import{jsx as $}from"react/jsx-runtime";var R=P({accessToken:null,setAccessToken:()=>{},user:null,setUser:()=>{},isLoading:!0,checkAuth:async()=>!1,logout:async()=>{},fetch:async()=>{throw new Error("AuthContext.fetch not implemented")},loginPath:"/login"}),G=({children:h,authUrl:i,apiUrl:a,loginPath:c="/login"})=>{let[r,f]=y(null),[o,s]=y(null),[u,A]=y(!0);E(()=>{(async()=>(await p(),A(!1)))()},[]);let n=(e,t)=>{f(e),s(t)},d=async(e,t={})=>k(e,{...t,token:r,apiUrl:a||"",authUrl:i,onTokenRefresh:l=>{n(l,o)},onAuthFail:()=>{n(null,null)}}),p=async()=>{try{let e=await fetch(`${i}/refresh`,{method:"POST",credentials:"include"});if(!e.ok)return n(null,null),!1;let t=await e.json();return t.success&&t.accessToken&&t.user?(n(t.accessToken,t.user),!0):(n(null,null),!1)}catch{return n(null,null),!1}},g=async()=>{try{await fetch(`${i}/logout`,{method:"POST",credentials:"include",headers:r?{Authorization:`Bearer ${r}`}:{}})}catch(e){console.error("Logout failed:",e)}finally{n(null,null)}};return $(R.Provider,{value:{accessToken:r,setAccessToken:e=>n(e,o),user:o,setUser:s,isLoading:u,checkAuth:p,logout:g,fetch:d,loginPath:c},children:h})},w=()=>C(R);import{useEffect as v}from"react";import{Fragment as T,jsx as m}from"react/jsx-runtime";var J=({children:h,loadingComponent:i,onUnauthenticated:a,isPublic:c=!1,allowedRoles:r,forbiddenComponent:f})=>{let{user:o,isLoading:s,loginPath:u}=w();return v(()=>{!s&&!o&&!c&&(a?a():u&&(window.location.href=u))},[o,s,c,a,u]),s?m(T,{children:i||null}):!o&&!c?m(T,{children:i||null}):o&&r&&r.length>0&&!r.includes(o.role)?m(T,{children:f||null}):m(T,{children:h})};export{R as AuthContext,J as AuthGuard,G as AuthProvider,k as apiFetch,w as useAuth};
|
package/package.json
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhmdff/auth-react",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "Plug and play authentication library for React/Next.js",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"private": false,
|
|
7
7
|
"module": "./dist/index.mjs",
|
|
8
|
-
"
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
9
15
|
"files": [
|
|
10
16
|
"dist"
|
|
11
17
|
],
|