@zhmdff/auth-react 1.0.7 → 1.0.9

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
@@ -34,6 +34,7 @@ type AuthContextType = {
34
34
  checkAuth: () => Promise<boolean>;
35
35
  logout: () => Promise<void>;
36
36
  fetch: (endpoint: string, options?: any) => Promise<any>;
37
+ loginWithGoogle: (returnUrl?: string) => void;
37
38
  loginPath?: string;
38
39
  };
39
40
  interface LoginRequest {
@@ -83,6 +84,11 @@ type FetchOptions = {
83
84
  * Useful for auth endpoints themselves (/auth/login, /auth/refresh, /auth/logout).
84
85
  */
85
86
  disableAutoRefresh?: boolean;
87
+ /**
88
+ * If true (default), returns res.json().
89
+ * If false, returns the raw Response object (useful for blobs/files).
90
+ */
91
+ handleJsonResponse?: boolean;
86
92
  };
87
93
  declare function apiFetch(endpoint: string, options?: FetchOptions): Promise<any>;
88
94
 
@@ -119,4 +125,28 @@ interface AuthGuardProps {
119
125
  */
120
126
  declare const AuthGuard: ({ children, loadingComponent, onUnauthenticated, isPublic, allowedRoles, forbiddenComponent }: AuthGuardProps) => react_jsx_runtime.JSX.Element;
121
127
 
122
- export { type AuthConfig, AuthContext, type AuthContextType, AuthGuard, type AuthGuardProps, AuthProvider, type AuthProviderProps, type AuthResult, type FetchOptions, type LoginRequest, type RegisterRequest, type User, apiFetch, useAuth };
128
+ interface GoogleLoginButtonProps {
129
+ /**
130
+ * Optional return URL to redirect back to after successful login.
131
+ */
132
+ returnUrl?: string;
133
+ /**
134
+ * Custom text for the button (default: "Sign in with Google").
135
+ */
136
+ text?: string;
137
+ /**
138
+ * Optional className for custom styling.
139
+ */
140
+ className?: string;
141
+ /**
142
+ * Optional style object for custom styling.
143
+ */
144
+ style?: React.CSSProperties;
145
+ }
146
+ /**
147
+ * A premium Google login button that initiates the OAuth flow.
148
+ * Note: Requires the Zhmdff.Auth NuGet package with Google OAuth configured on the backend.
149
+ */
150
+ declare const GoogleLoginButton: ({ returnUrl, text, className, style }: GoogleLoginButtonProps) => react_jsx_runtime.JSX.Element;
151
+
152
+ export { type AuthConfig, AuthContext, type AuthContextType, AuthGuard, type AuthGuardProps, AuthProvider, type AuthProviderProps, type AuthResult, type FetchOptions, GoogleLoginButton, type GoogleLoginButtonProps, type LoginRequest, type RegisterRequest, type User, apiFetch, useAuth };
package/dist/index.d.ts CHANGED
@@ -34,6 +34,7 @@ type AuthContextType = {
34
34
  checkAuth: () => Promise<boolean>;
35
35
  logout: () => Promise<void>;
36
36
  fetch: (endpoint: string, options?: any) => Promise<any>;
37
+ loginWithGoogle: (returnUrl?: string) => void;
37
38
  loginPath?: string;
38
39
  };
39
40
  interface LoginRequest {
@@ -83,6 +84,11 @@ type FetchOptions = {
83
84
  * Useful for auth endpoints themselves (/auth/login, /auth/refresh, /auth/logout).
84
85
  */
85
86
  disableAutoRefresh?: boolean;
87
+ /**
88
+ * If true (default), returns res.json().
89
+ * If false, returns the raw Response object (useful for blobs/files).
90
+ */
91
+ handleJsonResponse?: boolean;
86
92
  };
87
93
  declare function apiFetch(endpoint: string, options?: FetchOptions): Promise<any>;
88
94
 
@@ -119,4 +125,28 @@ interface AuthGuardProps {
119
125
  */
120
126
  declare const AuthGuard: ({ children, loadingComponent, onUnauthenticated, isPublic, allowedRoles, forbiddenComponent }: AuthGuardProps) => react_jsx_runtime.JSX.Element;
121
127
 
122
- export { type AuthConfig, AuthContext, type AuthContextType, AuthGuard, type AuthGuardProps, AuthProvider, type AuthProviderProps, type AuthResult, type FetchOptions, type LoginRequest, type RegisterRequest, type User, apiFetch, useAuth };
128
+ interface GoogleLoginButtonProps {
129
+ /**
130
+ * Optional return URL to redirect back to after successful login.
131
+ */
132
+ returnUrl?: string;
133
+ /**
134
+ * Custom text for the button (default: "Sign in with Google").
135
+ */
136
+ text?: string;
137
+ /**
138
+ * Optional className for custom styling.
139
+ */
140
+ className?: string;
141
+ /**
142
+ * Optional style object for custom styling.
143
+ */
144
+ style?: React.CSSProperties;
145
+ }
146
+ /**
147
+ * A premium Google login button that initiates the OAuth flow.
148
+ * Note: Requires the Zhmdff.Auth NuGet package with Google OAuth configured on the backend.
149
+ */
150
+ declare const GoogleLoginButton: ({ returnUrl, text, className, style }: GoogleLoginButtonProps) => react_jsx_runtime.JSX.Element;
151
+
152
+ export { type AuthConfig, AuthContext, type AuthContextType, AuthGuard, type AuthGuardProps, AuthProvider, type AuthProviderProps, type AuthResult, type FetchOptions, GoogleLoginButton, type GoogleLoginButtonProps, type LoginRequest, type RegisterRequest, type User, apiFetch, useAuth };
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: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});
1
+ "use strict";var R=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var G=Object.getOwnPropertyNames;var L=Object.prototype.hasOwnProperty;var F=(s,e)=>{for(var a in e)R(s,a,{get:e[a],enumerable:!0})},$=(s,e,a,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of G(e))!L.call(s,r)&&r!==a&&R(s,r,{get:()=>e[r],enumerable:!(o=E(e,r))||o.enumerable});return s};var B=s=>$(R({},"__esModule",{value:!0}),s);var W={};F(W,{AuthContext:()=>P,AuthGuard:()=>U,AuthProvider:()=>O,GoogleLoginButton:()=>M,apiFetch:()=>k,useAuth:()=>A});module.exports=B(W);var f=require("react");async function k(s,e={}){let{method:a="GET",body:o,token:r,onTokenRefresh:g,onAuthFail:l,apiUrl:i="/api",authUrl:x="/auth",headers:w,disableAutoRefresh:u=!1,handleJsonResponse:b=!0}=e,C=o instanceof FormData||o&&o.constructor&&o.constructor.name==="FormData",m={...C?{}:{"Content-Type":"application/json"},...w||{}};r&&(m.Authorization=`Bearer ${r}`);let y={method:a,headers:m,credentials:"include"};o&&a!=="GET"&&(C?y.body=o:y.body=typeof o=="string"?o:JSON.stringify(o));let n=await fetch(`${i}${s}`,y);if(!u&&n.status===401&&r&&g)try{let t=await fetch(`${x}/refresh`,{method:"POST",credentials:"include"});if(t.ok){let h=(await t.json()).accessToken;g(h),m.Authorization=`Bearer ${h}`;let T=await fetch(`${i}${s}`,{...y,headers:m});if(!T.ok)throw new Error(await T.text());return T.json()}else throw l?.(),new Error("Session expired")}catch(t){throw l?.(),t}if(!n.ok){let t=await n.text(),c="An error occurred";try{let h=JSON.parse(t);c=h.errorMessage||h.message||h||c}catch{c=t||c}throw new Error(c)}return b?n.json():n}var S=require("react/jsx-runtime"),P=(0,f.createContext)({accessToken:null,setAccessToken:()=>{},user:null,setUser:()=>{},isLoading:!0,checkAuth:async()=>!1,logout:async()=>{},fetch:async()=>{throw new Error("AuthContext.fetch not implemented")},loginWithGoogle:()=>{throw new Error("AuthContext.loginWithGoogle not implemented")},loginPath:"/login"}),O=({children:s,authUrl:e,apiUrl:a,loginPath:o="/login"})=>{let[r,g]=(0,f.useState)(null),[l,i]=(0,f.useState)(null),[x,w]=(0,f.useState)(!0);(0,f.useEffect)(()=>{(async()=>(await C(),w(!1)))()},[]);let u=(n,t)=>{g(n),i(t)},b=async(n,t={})=>k(n,{...t,token:r,apiUrl:a||"",authUrl:e,onTokenRefresh:c=>{u(c,l)},onAuthFail:()=>{u(null,null)}}),C=async()=>{try{let n=await fetch(`${e}/refresh`,{method:"POST",credentials:"include"});if(!n.ok)return u(null,null),!1;let t=await n.json();if(t.success&&t.accessToken&&t.user){let c={...t.user},h=t.roles||t.user.roles||t.user.Roles||[];return h.length>0&&(c.roles=h,c.role=h[0]),u(t.accessToken,c),!0}return u(null,null),!1}catch{return u(null,null),!1}},m=async()=>{try{await fetch(`${e}/logout`,{method:"POST",credentials:"include",headers:r?{Authorization:`Bearer ${r}`}:{}})}catch(n){console.error("Logout failed:",n)}finally{u(null,null)}},y=n=>{let c=`${e.endsWith("/")?e.slice(0,-1):e}/google${n?`?returnUrl=${encodeURIComponent(n)}`:""}`;window.location.href=c};return(0,S.jsx)(P.Provider,{value:{accessToken:r,setAccessToken:n=>u(n,l),user:l,setUser:i,isLoading:x,checkAuth:C,logout:m,fetch:b,loginWithGoogle:y,loginPath:o},children:s})},A=()=>(0,f.useContext)(P);var v=require("react");var d=require("react/jsx-runtime"),U=({children:s,loadingComponent:e,onUnauthenticated:a,isPublic:o=!1,allowedRoles:r,forbiddenComponent:g})=>{let{user:l,isLoading:i,loginPath:x}=A();return(0,v.useEffect)(()=>{!i&&!l&&!o&&(a?a():x&&(window.location.href=x))},[l,i,o,a,x]),i?(0,d.jsx)(d.Fragment,{children:e||null}):!l&&!o?(0,d.jsx)(d.Fragment,{children:e||null}):l&&r&&r.length>0&&!r.includes(l.role)?(0,d.jsx)(d.Fragment,{children:g||null}):(0,d.jsx)(d.Fragment,{children:s})};var p=require("react/jsx-runtime"),M=({returnUrl:s,text:e="Sign in with Google",className:a,style:o})=>{let{loginWithGoogle:r}=A(),g=()=>{r(s)},l={display:"flex",alignItems:"center",justifyContent:"center",backgroundColor:"#ffffff",color:"#3c4043",border:"1px solid #dadce0",borderRadius:"24px",padding:"0 16px",height:"40px",fontSize:"14px",fontWeight:500,cursor:"pointer",transition:"background-color 0.2s, box-shadow 0.2s, border-color 0.2s",boxShadow:"0 1px 2px 0 rgba(60,64,67,0.30), 0 1px 3px 1px rgba(60,64,67,0.15)",gap:"10px",outline:"none",...o};return(0,p.jsxs)("button",{onClick:g,style:l,className:a,onMouseOver:i=>{i.currentTarget.style.backgroundColor="#f8f9fa",i.currentTarget.style.boxShadow="0 1px 3px 0 rgba(60,64,67,0.30), 0 4px 8px 3px rgba(60,64,67,0.15)"},onMouseOut:i=>{i.currentTarget.style.backgroundColor="#ffffff",i.currentTarget.style.boxShadow="0 1px 2px 0 rgba(60,64,67,0.30), 0 1px 3px 1px rgba(60,64,67,0.15)"},children:[(0,p.jsxs)("svg",{width:"18",height:"18",viewBox:"0 0 18 18",xmlns:"http://www.w3.org/2000/svg",children:[(0,p.jsx)("path",{d:"M17.64 9.20455C17.64 8.56636 17.5827 7.95273 17.4764 7.36364H9V10.845H13.8436C13.635 11.97 13.0009 12.9232 12.0477 13.5614V15.8195H14.9564C16.6582 14.2527 17.64 11.9455 17.64 9.20455Z",fill:"#4285F4"}),(0,p.jsx)("path",{d:"M9 18C11.43 18 13.4673 17.1941 14.9564 15.8195L12.0477 13.5614C11.2418 14.1014 10.2109 14.4205 9 14.4205C6.65591 14.4205 4.67182 12.8373 3.96409 10.71H0.957273V13.0418C2.43818 15.9832 5.48182 18 9 18Z",fill:"#34A853"}),(0,p.jsx)("path",{d:"M5.03591 10.71C4.85591 10.17 4.75364 9.59318 4.75364 9C4.75364 8.40682 4.85591 7.83 5.03591 7.29V4.95818H0.957273C0.347727 6.17318 0 7.54773 0 9C0 10.4523 0.347727 11.8268 0.957273 13.0418L5.03591 10.71z",fill:"#FBBC05"}),(0,p.jsx)("path",{d:"M9 3.57955C10.3214 3.57955 11.5077 4.03364 12.4405 4.92545L15.0218 2.34409C13.4632 0.891818 11.4259 0 9 0C5.48182 0 2.43818 2.01682 0.957273 4.95818L5.03591 7.29C5.74364 5.16273 7.72773 3.57955 9 3.57955Z",fill:"#EA4335"})]}),(0,p.jsx)("span",{children:e})]})};0&&(module.exports={AuthContext,AuthGuard,AuthProvider,GoogleLoginButton,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: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};
1
+ import{createContext as v,useContext as E,useState as R,useEffect as G}from"react";async function k(h,i={}){let{method:l="GET",body:o,token:n,onTokenRefresh:f,onAuthFail:s,apiUrl:r="/api",authUrl:d="/auth",headers:y,disableAutoRefresh:c=!1,handleJsonResponse:b=!0}=i,x=o instanceof FormData||o&&o.constructor&&o.constructor.name==="FormData",p={...x?{}:{"Content-Type":"application/json"},...y||{}};n&&(p.Authorization=`Bearer ${n}`);let g={method:l,headers:p,credentials:"include"};o&&l!=="GET"&&(x?g.body=o:g.body=typeof o=="string"?o:JSON.stringify(o));let t=await fetch(`${r}${h}`,g);if(!c&&t.status===401&&n&&f)try{let e=await fetch(`${d}/refresh`,{method:"POST",credentials:"include"});if(e.ok){let u=(await e.json()).accessToken;f(u),p.Authorization=`Bearer ${u}`;let T=await fetch(`${r}${h}`,{...g,headers:p});if(!T.ok)throw new Error(await T.text());return T.json()}else throw s?.(),new Error("Session expired")}catch(e){throw s?.(),e}if(!t.ok){let e=await t.text(),a="An error occurred";try{let u=JSON.parse(e);a=u.errorMessage||u.message||u||a}catch{a=e||a}throw new Error(a)}return b?t.json():t}import{jsx as L}from"react/jsx-runtime";var P=v({accessToken:null,setAccessToken:()=>{},user:null,setUser:()=>{},isLoading:!0,checkAuth:async()=>!1,logout:async()=>{},fetch:async()=>{throw new Error("AuthContext.fetch not implemented")},loginWithGoogle:()=>{throw new Error("AuthContext.loginWithGoogle not implemented")},loginPath:"/login"}),W=({children:h,authUrl:i,apiUrl:l,loginPath:o="/login"})=>{let[n,f]=R(null),[s,r]=R(null),[d,y]=R(!0);G(()=>{(async()=>(await x(),y(!1)))()},[]);let c=(t,e)=>{f(t),r(e)},b=async(t,e={})=>k(t,{...e,token:n,apiUrl:l||"",authUrl:i,onTokenRefresh:a=>{c(a,s)},onAuthFail:()=>{c(null,null)}}),x=async()=>{try{let t=await fetch(`${i}/refresh`,{method:"POST",credentials:"include"});if(!t.ok)return c(null,null),!1;let e=await t.json();if(e.success&&e.accessToken&&e.user){let a={...e.user},u=e.roles||e.user.roles||e.user.Roles||[];return u.length>0&&(a.roles=u,a.role=u[0]),c(e.accessToken,a),!0}return c(null,null),!1}catch{return c(null,null),!1}},p=async()=>{try{await fetch(`${i}/logout`,{method:"POST",credentials:"include",headers:n?{Authorization:`Bearer ${n}`}:{}})}catch(t){console.error("Logout failed:",t)}finally{c(null,null)}},g=t=>{let a=`${i.endsWith("/")?i.slice(0,-1):i}/google${t?`?returnUrl=${encodeURIComponent(t)}`:""}`;window.location.href=a};return L(P.Provider,{value:{accessToken:n,setAccessToken:t=>c(t,s),user:s,setUser:r,isLoading:d,checkAuth:x,logout:p,fetch:b,loginWithGoogle:g,loginPath:o},children:h})},C=()=>E(P);import{useEffect as F}from"react";import{Fragment as A,jsx as w}from"react/jsx-runtime";var I=({children:h,loadingComponent:i,onUnauthenticated:l,isPublic:o=!1,allowedRoles:n,forbiddenComponent:f})=>{let{user:s,isLoading:r,loginPath:d}=C();return F(()=>{!r&&!s&&!o&&(l?l():d&&(window.location.href=d))},[s,r,o,l,d]),r?w(A,{children:i||null}):!s&&!o?w(A,{children:i||null}):s&&n&&n.length>0&&!n.includes(s.role)?w(A,{children:f||null}):w(A,{children:h})};import{jsx as m,jsxs as S}from"react/jsx-runtime";var q=({returnUrl:h,text:i="Sign in with Google",className:l,style:o})=>{let{loginWithGoogle:n}=C(),f=()=>{n(h)},s={display:"flex",alignItems:"center",justifyContent:"center",backgroundColor:"#ffffff",color:"#3c4043",border:"1px solid #dadce0",borderRadius:"24px",padding:"0 16px",height:"40px",fontSize:"14px",fontWeight:500,cursor:"pointer",transition:"background-color 0.2s, box-shadow 0.2s, border-color 0.2s",boxShadow:"0 1px 2px 0 rgba(60,64,67,0.30), 0 1px 3px 1px rgba(60,64,67,0.15)",gap:"10px",outline:"none",...o};return S("button",{onClick:f,style:s,className:l,onMouseOver:r=>{r.currentTarget.style.backgroundColor="#f8f9fa",r.currentTarget.style.boxShadow="0 1px 3px 0 rgba(60,64,67,0.30), 0 4px 8px 3px rgba(60,64,67,0.15)"},onMouseOut:r=>{r.currentTarget.style.backgroundColor="#ffffff",r.currentTarget.style.boxShadow="0 1px 2px 0 rgba(60,64,67,0.30), 0 1px 3px 1px rgba(60,64,67,0.15)"},children:[S("svg",{width:"18",height:"18",viewBox:"0 0 18 18",xmlns:"http://www.w3.org/2000/svg",children:[m("path",{d:"M17.64 9.20455C17.64 8.56636 17.5827 7.95273 17.4764 7.36364H9V10.845H13.8436C13.635 11.97 13.0009 12.9232 12.0477 13.5614V15.8195H14.9564C16.6582 14.2527 17.64 11.9455 17.64 9.20455Z",fill:"#4285F4"}),m("path",{d:"M9 18C11.43 18 13.4673 17.1941 14.9564 15.8195L12.0477 13.5614C11.2418 14.1014 10.2109 14.4205 9 14.4205C6.65591 14.4205 4.67182 12.8373 3.96409 10.71H0.957273V13.0418C2.43818 15.9832 5.48182 18 9 18Z",fill:"#34A853"}),m("path",{d:"M5.03591 10.71C4.85591 10.17 4.75364 9.59318 4.75364 9C4.75364 8.40682 4.85591 7.83 5.03591 7.29V4.95818H0.957273C0.347727 6.17318 0 7.54773 0 9C0 10.4523 0.347727 11.8268 0.957273 13.0418L5.03591 10.71z",fill:"#FBBC05"}),m("path",{d:"M9 3.57955C10.3214 3.57955 11.5077 4.03364 12.4405 4.92545L15.0218 2.34409C13.4632 0.891818 11.4259 0 9 0C5.48182 0 2.43818 2.01682 0.957273 4.95818L5.03591 7.29C5.74364 5.16273 7.72773 3.57955 9 3.57955Z",fill:"#EA4335"})]}),m("span",{children:i})]})};export{P as AuthContext,I as AuthGuard,W as AuthProvider,q as GoogleLoginButton,k as apiFetch,C as useAuth};
package/package.json CHANGED
@@ -1,34 +1,37 @@
1
- {
2
- "name": "@zhmdff/auth-react",
3
- "version": "1.0.7",
4
- "description": "Plug and play authentication library for React/Next.js",
5
- "main": "./dist/index.js",
6
- "private": false,
7
- "module": "./dist/index.mjs",
8
- "exports": {
9
- ".": {
10
- "types": "./dist/index.d.ts",
11
- "import": "./dist/index.mjs",
12
- "require": "./dist/index.js"
13
- }
14
- },
15
- "files": [
16
- "dist"
17
- ],
18
- "scripts": {
19
- "build": "tsup src/index.ts --format cjs,esm --dts --minify --clean",
20
- "dev": "tsup src/index.ts --format cjs,esm --watch --dts",
21
- "lint": "eslint src"
22
- },
23
- "peerDependencies": {
24
- "react": ">=18",
25
- "react-dom": ">=18",
26
- "next": ">=13"
27
- },
28
- "devDependencies": {
29
- "tsup": "^8.0.2",
30
- "typescript": "^5.3.3",
31
- "@types/react": "^18.2.48",
32
- "@types/react-dom": "^18.2.18"
33
- }
34
- }
1
+ {
2
+ "name": "@zhmdff/auth-react",
3
+ "version": "1.0.9",
4
+ "description": "Plug and play authentication library for React/Next.js",
5
+ "main": "./dist/index.js",
6
+ "private": false,
7
+ "module": "./dist/index.mjs",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsup src/index.ts --format cjs,esm --dts --minify --clean",
20
+ "dev": "tsup src/index.ts --format cjs,esm --watch --dts",
21
+ "lint": "eslint src"
22
+ },
23
+ "peerDependencies": {
24
+ "next": ">=13",
25
+ "react": ">=18",
26
+ "react-dom": ">=18"
27
+ },
28
+ "devDependencies": {
29
+ "@types/react": "^18.2.48",
30
+ "@types/react-dom": "^18.2.18",
31
+ "tsup": "^8.0.2",
32
+ "typescript": "^5.3.3"
33
+ },
34
+ "dependencies": {
35
+ "framer-motion": "^12.34.4"
36
+ }
37
+ }