@zhmdff/auth-react 1.0.0 → 1.0.2
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 +2 -3
- package/dist/index.d.ts +2 -3
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import React from 'react';
|
|
2
|
+
import React, { PropsWithChildren } from 'react';
|
|
3
3
|
|
|
4
4
|
interface User {
|
|
5
5
|
id: number;
|
|
@@ -42,8 +42,7 @@ interface RegisterRequest {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
declare const AuthContext: React.Context<AuthContextType>;
|
|
45
|
-
interface AuthProviderProps {
|
|
46
|
-
children: React.ReactNode;
|
|
45
|
+
interface AuthProviderProps extends PropsWithChildren {
|
|
47
46
|
authUrl: string;
|
|
48
47
|
apiUrl?: string;
|
|
49
48
|
storageKey?: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import React from 'react';
|
|
2
|
+
import React, { PropsWithChildren } from 'react';
|
|
3
3
|
|
|
4
4
|
interface User {
|
|
5
5
|
id: number;
|
|
@@ -42,8 +42,7 @@ interface RegisterRequest {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
declare const AuthContext: React.Context<AuthContextType>;
|
|
45
|
-
interface AuthProviderProps {
|
|
46
|
-
children: React.ReactNode;
|
|
45
|
+
interface AuthProviderProps extends PropsWithChildren {
|
|
47
46
|
authUrl: string;
|
|
48
47
|
apiUrl?: string;
|
|
49
48
|
storageKey?: string;
|
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})},
|
|
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});
|
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")}})
|
|
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};
|