@vergeinfosoft/react 1.0.0

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/README.md ADDED
@@ -0,0 +1,277 @@
1
+ # @verge-auth/react
2
+
3
+ React SDK for Verge Auth - Single-line authentication integration for your React applications.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @verge-auth/react
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ### One-Line Integration
14
+
15
+ Wrap your entire app with the `VergeAuth` component:
16
+
17
+ ```jsx
18
+ import { VergeAuth } from '@verge-auth/react';
19
+
20
+ function App() {
21
+ return (
22
+ <VergeAuth>
23
+ <YourApp />
24
+ </VergeAuth>
25
+ );
26
+ }
27
+ ```
28
+
29
+ That's it! Your app is now protected with Verge Auth.
30
+
31
+ ### With Custom Configuration
32
+
33
+ ```jsx
34
+ import { VergeAuth } from '@verge-auth/react';
35
+
36
+ function App() {
37
+ return (
38
+ <VergeAuth
39
+ config={{
40
+ apiBaseUrl: '/api',
41
+ authEndpoint: '/auth/me',
42
+ loginUrl: 'https://auth.vergeinfosoft.com/login',
43
+ logoutUrl: '/auth/logout',
44
+ redirectUrl: window.location.origin
45
+ }}
46
+ callbackPath="/auth/callback"
47
+ >
48
+ <YourApp />
49
+ </VergeAuth>
50
+ );
51
+ }
52
+ ```
53
+
54
+ ## Environment Variables
55
+
56
+ Set these in your `.env` file:
57
+
58
+ ```env
59
+ VITE_VERGEAUTH_LOGIN_URL=https://auth.vergeinfosoft.com/login
60
+ ```
61
+
62
+ ## Advanced Usage
63
+
64
+ ### Using Individual Components
65
+
66
+ If you need more control, you can use the individual components:
67
+
68
+ ```jsx
69
+ import { AuthProvider, useAuth, ProtectedRoute } from '@verge-auth/react';
70
+ import { BrowserRouter, Routes, Route } from 'react-router-dom';
71
+
72
+ function App() {
73
+ return (
74
+ <AuthProvider>
75
+ <BrowserRouter>
76
+ <Routes>
77
+ <Route path="/auth/callback" element={<AuthCallback />} />
78
+ <Route path="/*" element={
79
+ <ProtectedRoute>
80
+ <YourApp />
81
+ </ProtectedRoute>
82
+ } />
83
+ </Routes>
84
+ </BrowserRouter>
85
+ </AuthProvider>
86
+ );
87
+ }
88
+ ```
89
+
90
+ ### Permission-Based Route Protection
91
+
92
+ ```jsx
93
+ import { ProtectedRoute } from '@verge-auth/react';
94
+
95
+ function Dashboard() {
96
+ return (
97
+ <ProtectedRoute requiredPermissions={['hrms-service:/api/dashboard/stats:get']}>
98
+ <DashboardContent />
99
+ </ProtectedRoute>
100
+ );
101
+ }
102
+ ```
103
+
104
+ ### Using the Auth Hook
105
+
106
+ ```jsx
107
+ import { useAuth } from '@verge-auth/react';
108
+
109
+ function UserProfile() {
110
+ const { isAuthenticated, loading, permissions, hasPermission, login, logout } = useAuth();
111
+
112
+ if (loading) return <div>Loading...</div>;
113
+
114
+ if (!isAuthenticated) {
115
+ return <button onClick={login}>Login</button>;
116
+ }
117
+
118
+ return (
119
+ <div>
120
+ <h1>Welcome!</h1>
121
+ <p>Permissions: {permissions.join(', ')}</p>
122
+ {hasPermission('admin') && <button>Admin Panel</button>}
123
+ <button onClick={logout}>Logout</button>
124
+ </div>
125
+ );
126
+ }
127
+ ```
128
+
129
+ ### Require All Permissions
130
+
131
+ By default, `ProtectedRoute` requires ANY of the specified permissions. To require ALL:
132
+
133
+ ```jsx
134
+ <ProtectedRoute
135
+ requiredPermissions={['perm1', 'perm2']}
136
+ requireAll={true}
137
+ >
138
+ <AdminPanel />
139
+ </ProtectedRoute>
140
+ ```
141
+
142
+ ### Custom Fallback Path
143
+
144
+ ```jsx
145
+ <ProtectedRoute
146
+ requiredPermissions={['admin']}
147
+ fallbackPath="/unauthorized"
148
+ >
149
+ <AdminPanel />
150
+ </ProtectedRoute>
151
+ ```
152
+
153
+ ## API Reference
154
+
155
+ ### VergeAuth
156
+
157
+ Main wrapper component for one-line integration.
158
+
159
+ **Props:**
160
+ - `children` (ReactNode): Your application
161
+ - `config?` (VergeAuthConfig): Configuration object
162
+ - `callbackPath?` (string): Path for auth callback (default: `/auth/callback`)
163
+
164
+ ### AuthProvider
165
+
166
+ Provides auth context to your app.
167
+
168
+ **Props:**
169
+ - `children` (ReactNode): Child components
170
+ - `config?` (VergeAuthConfig): Configuration object
171
+
172
+ ### ProtectedRoute
173
+
174
+ Protects routes based on authentication and permissions.
175
+
176
+ **Props:**
177
+ - `children` (ReactNode): Protected content
178
+ - `requiredPermissions?` (string[]): Required permissions
179
+ - `requireAll?` (boolean): Require all permissions instead of any (default: false)
180
+ - `fallbackPath?` (string): Redirect path if unauthorized (default: `/`)
181
+
182
+ ### AuthCallback
183
+
184
+ Handles OAuth callback from Verge Auth.
185
+
186
+ **Props:**
187
+ - `redirectPath?` (string): Path to redirect after successful auth (default: `/`)
188
+
189
+ ### useAuth
190
+
191
+ Hook to access auth state and methods.
192
+
193
+ **Returns:**
194
+ - `isAuthenticated` (boolean | null): Authentication status
195
+ - `loading` (boolean): Loading state
196
+ - `permissions` (string[]): User permissions
197
+ - `appBrandName` (string | null): App brand name
198
+ - `user` (any): User data
199
+ - `hasPermission(permission: string)` (function): Check if user has permission
200
+ - `hasAnyPermission(permissions: string[])` (function): Check if user has any of the permissions
201
+ - `login()` (function): Redirect to login
202
+ - `logout()` (function): Redirect to logout
203
+
204
+ ## Configuration
205
+
206
+ ### VergeAuthConfig
207
+
208
+ ```typescript
209
+ interface VergeAuthConfig {
210
+ apiBaseUrl?: string; // Default: '/api'
211
+ authEndpoint?: string; // Default: '/auth/me'
212
+ loginUrl?: string; // Default: from VITE_VERGEAUTH_LOGIN_URL
213
+ logoutUrl?: string; // Default: '/auth/logout'
214
+ redirectUrl?: string; // Default: current URL
215
+ }
216
+ ```
217
+
218
+ ## How It Works
219
+
220
+ 1. **Auth Check**: On mount, the SDK calls `/api/auth/me` to check authentication status
221
+ 2. **403 Handling**: A 403 response means the user is authenticated but lacks route permission
222
+ 3. **Login Redirect**: Unauthenticated users are redirected to the Verge Auth login page
223
+ 4. **Callback Handling**: After login, the callback route handles the OAuth code exchange
224
+ 5. **Permission Checks**: Routes can be protected based on user permissions
225
+
226
+ ## Backend Requirements
227
+
228
+ Your backend must:
229
+ - Use the Verge Auth Python SDK with `add_central_auth(app)`
230
+ - Provide an `/api/auth/me` endpoint (handled automatically by the SDK)
231
+ - Handle OAuth code exchange at `/api/?code=` (handled automatically by the SDK)
232
+
233
+ ## Example: Full HRMS Integration
234
+
235
+ ```jsx
236
+ import { VergeAuth, ProtectedRoute, useAuth } from '@verge-auth/react';
237
+
238
+ const PERMISSIONS = {
239
+ DASHBOARD_GET: "hrms-service:/api/dashboard/stats:get",
240
+ EMPLOYEES_GET: "hrms-service:/api/employees:get",
241
+ ATTENDANCE_GET: "hrms-service:/api/attendance:get",
242
+ };
243
+
244
+ function App() {
245
+ return (
246
+ <VergeAuth>
247
+ <Dashboard />
248
+ <Employees />
249
+ <Attendance />
250
+ </VergeAuth>
251
+ );
252
+ }
253
+
254
+ function Dashboard() {
255
+ return (
256
+ <ProtectedRoute requiredPermissions={[PERMISSIONS.DASHBOARD_GET]}>
257
+ <DashboardContent />
258
+ </ProtectedRoute>
259
+ );
260
+ }
261
+
262
+ function Employees() {
263
+ return (
264
+ <ProtectedRoute requiredPermissions={[PERMISSIONS.EMPLOYEES_GET]}>
265
+ <EmployeesContent />
266
+ </ProtectedRoute>
267
+ );
268
+ }
269
+ ```
270
+
271
+ ## License
272
+
273
+ MIT
274
+
275
+ ## Support
276
+
277
+ For issues and questions, please contact Verge Infosoft support.
@@ -0,0 +1,5 @@
1
+ export interface AuthCallbackProps {
2
+ redirectPath?: string;
3
+ }
4
+ export declare function AuthCallback({ redirectPath }: AuthCallbackProps): import("react/jsx-runtime").JSX.Element;
5
+ //# sourceMappingURL=AuthCallback.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthCallback.d.ts","sourceRoot":"","sources":["../src/AuthCallback.tsx"],"names":[],"mappings":"AAGA,MAAM,WAAW,iBAAiB;IAChC,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,wBAAgB,YAAY,CAAC,EAAE,YAAkB,EAAE,EAAE,iBAAiB,2CA4BrE"}
@@ -0,0 +1,9 @@
1
+ import { ReactNode } from 'react';
2
+ import { AuthContextValue, VergeAuthConfig } from './types';
3
+ export interface AuthProviderProps {
4
+ children: ReactNode;
5
+ config?: VergeAuthConfig;
6
+ }
7
+ export declare function AuthProvider({ children, config }: AuthProviderProps): import("react/jsx-runtime").JSX.Element;
8
+ export declare const useAuth: () => AuthContextValue;
9
+ //# sourceMappingURL=AuthContext.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthContext.d.ts","sourceRoot":"","sources":["../src/AuthContext.tsx"],"names":[],"mappings":"AAAA,OAAc,EAAkD,SAAS,EAAE,MAAM,OAAO,CAAC;AAEzF,OAAO,EAAa,gBAAgB,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAIvE,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,SAAS,CAAC;IACpB,MAAM,CAAC,EAAE,eAAe,CAAC;CAC1B;AAUD,wBAAgB,YAAY,CAAC,EAAE,QAAQ,EAAE,MAAW,EAAE,EAAE,iBAAiB,2CA6FxE;AAED,eAAO,MAAM,OAAO,QAAO,gBAM1B,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { ReactNode } from 'react';
2
+ export interface ProtectedRouteProps {
3
+ children: ReactNode;
4
+ requiredPermissions?: string[];
5
+ requireAll?: boolean;
6
+ fallbackPath?: string;
7
+ }
8
+ export declare function ProtectedRoute({ children, requiredPermissions, requireAll, fallbackPath, }: ProtectedRouteProps): import("react/jsx-runtime").JSX.Element | null;
9
+ //# sourceMappingURL=ProtectedRoute.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ProtectedRoute.d.ts","sourceRoot":"","sources":["../src/ProtectedRoute.tsx"],"names":[],"mappings":"AAAA,OAAc,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAGzC,MAAM,WAAW,mBAAmB;IAClC,QAAQ,EAAE,SAAS,CAAC;IACpB,mBAAmB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC/B,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,wBAAgB,cAAc,CAAC,EAC7B,QAAQ,EACR,mBAAwB,EACxB,UAAkB,EAClB,YAAkB,GACnB,EAAE,mBAAmB,kDA8BrB"}
@@ -0,0 +1,25 @@
1
+ import { ReactNode } from 'react';
2
+ import { VergeAuthConfig } from './types';
3
+ export interface VergeAuthProps {
4
+ children: ReactNode;
5
+ config?: VergeAuthConfig;
6
+ callbackPath?: string;
7
+ }
8
+ /**
9
+ * VergeAuth - One-line integration wrapper for Verge Auth
10
+ *
11
+ * Usage:
12
+ * ```jsx
13
+ * import { VergeAuth } from '@verge-auth/react';
14
+ *
15
+ * function App() {
16
+ * return (
17
+ * <VergeAuth>
18
+ * <YourApp />
19
+ * </VergeAuth>
20
+ * );
21
+ * }
22
+ * ```
23
+ */
24
+ export declare function VergeAuth({ children, config, callbackPath }: VergeAuthProps): import("react/jsx-runtime").JSX.Element;
25
+ //# sourceMappingURL=VergeAuth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VergeAuth.d.ts","sourceRoot":"","sources":["../src/VergeAuth.tsx"],"names":[],"mappings":"AAAA,OAAc,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAIzC,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1C,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,SAAS,CAAC;IACpB,MAAM,CAAC,EAAE,eAAe,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,SAAS,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,YAA+B,EAAE,EAAE,cAAc,2CAW9F"}
@@ -0,0 +1,87 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ReactNode } from 'react';
3
+
4
+ interface AuthState {
5
+ isAuthenticated: boolean | null;
6
+ loading: boolean;
7
+ permissions: string[];
8
+ appBrandName: string | null;
9
+ user: any;
10
+ }
11
+ interface VergeAuthConfig {
12
+ apiBaseUrl?: string;
13
+ authEndpoint?: string;
14
+ loginUrl?: string;
15
+ logoutUrl?: string;
16
+ appBaseUrl?: string;
17
+ }
18
+ interface AuthContextValue extends AuthState {
19
+ hasPermission: (permission: string) => boolean;
20
+ hasAnyPermission: (permissions: string[]) => boolean;
21
+ login: () => void;
22
+ logout: () => void;
23
+ }
24
+ interface AuthProviderProps$1 {
25
+ children: ReactNode;
26
+ config?: VergeAuthConfig;
27
+ }
28
+ interface ProtectedRouteProps$1 {
29
+ children: ReactNode;
30
+ requiredPermissions?: string[];
31
+ requireAll?: boolean;
32
+ fallbackPath?: string;
33
+ }
34
+ interface AuthCallbackProps$1 {
35
+ redirectPath?: string;
36
+ }
37
+ interface VergeAuthProps$1 {
38
+ children: ReactNode;
39
+ config?: VergeAuthConfig;
40
+ callbackPath?: string;
41
+ }
42
+
43
+ interface VergeAuthProps {
44
+ children: ReactNode;
45
+ config?: VergeAuthConfig;
46
+ callbackPath?: string;
47
+ }
48
+ /**
49
+ * VergeAuth - One-line integration wrapper for Verge Auth
50
+ *
51
+ * Usage:
52
+ * ```jsx
53
+ * import { VergeAuth } from '@verge-auth/react';
54
+ *
55
+ * function App() {
56
+ * return (
57
+ * <VergeAuth>
58
+ * <YourApp />
59
+ * </VergeAuth>
60
+ * );
61
+ * }
62
+ * ```
63
+ */
64
+ declare function VergeAuth({ children, config, callbackPath }: VergeAuthProps): react_jsx_runtime.JSX.Element;
65
+
66
+ interface AuthProviderProps {
67
+ children: ReactNode;
68
+ config?: VergeAuthConfig;
69
+ }
70
+ declare function AuthProvider({ children, config }: AuthProviderProps): react_jsx_runtime.JSX.Element;
71
+ declare const useAuth: () => AuthContextValue;
72
+
73
+ interface ProtectedRouteProps {
74
+ children: ReactNode;
75
+ requiredPermissions?: string[];
76
+ requireAll?: boolean;
77
+ fallbackPath?: string;
78
+ }
79
+ declare function ProtectedRoute({ children, requiredPermissions, requireAll, fallbackPath, }: ProtectedRouteProps): react_jsx_runtime.JSX.Element | null;
80
+
81
+ interface AuthCallbackProps {
82
+ redirectPath?: string;
83
+ }
84
+ declare function AuthCallback({ redirectPath }: AuthCallbackProps): react_jsx_runtime.JSX.Element;
85
+
86
+ export { AuthCallback, AuthProvider, ProtectedRoute, VergeAuth, useAuth };
87
+ export type { AuthCallbackProps$1 as AuthCallbackProps, AuthContextValue, AuthProviderProps$1 as AuthProviderProps, AuthState, ProtectedRouteProps$1 as ProtectedRouteProps, VergeAuthConfig, VergeAuthProps$1 as VergeAuthProps };
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,YAAY,EAAE,SAAS,EAAE,eAAe,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC"}