@zhmdff/auth-react 1.0.0 → 1.0.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.
- package/dist/index.d.mts +23 -4
- package/dist/index.d.ts +23 -4
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +2 -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;
|
|
@@ -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;
|
|
@@ -42,8 +48,7 @@ interface RegisterRequest {
|
|
|
42
48
|
}
|
|
43
49
|
|
|
44
50
|
declare const AuthContext: React.Context<AuthContextType>;
|
|
45
|
-
interface AuthProviderProps {
|
|
46
|
-
children: React.ReactNode;
|
|
51
|
+
interface AuthProviderProps extends PropsWithChildren {
|
|
47
52
|
authUrl: string;
|
|
48
53
|
apiUrl?: string;
|
|
49
54
|
storageKey?: string;
|
|
@@ -52,13 +57,27 @@ declare const AuthProvider: ({ children, authUrl, apiUrl }: AuthProviderProps) =
|
|
|
52
57
|
declare const useAuth: () => AuthContextType;
|
|
53
58
|
|
|
54
59
|
type FetchOptions = {
|
|
55
|
-
|
|
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";
|
|
56
65
|
body?: any;
|
|
57
66
|
token?: string | null;
|
|
58
67
|
onTokenRefresh?: (token: string) => void;
|
|
59
68
|
onAuthFail?: () => void;
|
|
60
69
|
apiUrl?: string;
|
|
61
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;
|
|
62
81
|
};
|
|
63
82
|
declare function apiFetch(endpoint: string, options?: FetchOptions): Promise<any>;
|
|
64
83
|
|
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;
|
|
@@ -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;
|
|
@@ -42,8 +48,7 @@ interface RegisterRequest {
|
|
|
42
48
|
}
|
|
43
49
|
|
|
44
50
|
declare const AuthContext: React.Context<AuthContextType>;
|
|
45
|
-
interface AuthProviderProps {
|
|
46
|
-
children: React.ReactNode;
|
|
51
|
+
interface AuthProviderProps extends PropsWithChildren {
|
|
47
52
|
authUrl: string;
|
|
48
53
|
apiUrl?: string;
|
|
49
54
|
storageKey?: string;
|
|
@@ -52,13 +57,27 @@ declare const AuthProvider: ({ children, authUrl, apiUrl }: AuthProviderProps) =
|
|
|
52
57
|
declare const useAuth: () => AuthContextType;
|
|
53
58
|
|
|
54
59
|
type FetchOptions = {
|
|
55
|
-
|
|
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";
|
|
56
65
|
body?: any;
|
|
57
66
|
token?: string | null;
|
|
58
67
|
onTokenRefresh?: (token: string) => void;
|
|
59
68
|
onAuthFail?: () => void;
|
|
60
69
|
apiUrl?: string;
|
|
61
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;
|
|
62
81
|
};
|
|
63
82
|
declare function apiFetch(endpoint: string, options?: FetchOptions): Promise<any>;
|
|
64
83
|
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
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
|
|
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.
|
|
3
|
+
"version": "1.0.3",
|
|
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": [
|