@stackwright-pro/auth 0.2.0-alpha.13 → 0.2.0-alpha.15

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.
@@ -0,0 +1,197 @@
1
+ import * as React from 'react';
2
+ import React__default, { ReactNode, ReactElement } from 'react';
3
+ import { AuthUser, AuthSession, RBACConfig, ComponentAuthConfig } from '@stackwright-pro/types';
4
+
5
+ /**
6
+ * Auth context value provided to component tree
7
+ */
8
+ interface AuthContextValue {
9
+ /**
10
+ * Currently authenticated user (null if not authenticated)
11
+ */
12
+ user: AuthUser | null;
13
+ /**
14
+ * Current session (null if not authenticated)
15
+ */
16
+ session: AuthSession | null;
17
+ /**
18
+ * Whether user is authenticated
19
+ */
20
+ isAuthenticated: boolean;
21
+ /**
22
+ * Whether auth is still loading
23
+ */
24
+ isLoading: boolean;
25
+ /**
26
+ * Check if user has a specific role
27
+ */
28
+ hasRole: (role: string) => boolean;
29
+ /**
30
+ * Check if user has a specific permission
31
+ */
32
+ hasPermission: (permission: string) => boolean;
33
+ /**
34
+ * Check if user has any of the specified roles
35
+ */
36
+ hasAnyRole: (roles: string[]) => boolean;
37
+ /**
38
+ * Check if user has all of the specified permissions
39
+ */
40
+ hasAllPermissions: (permissions: string[]) => boolean;
41
+ }
42
+ /**
43
+ * Auth context - provides authentication state to components
44
+ */
45
+ declare const AuthContext: React.Context<AuthContextValue | null>;
46
+ /**
47
+ * Hook to access auth context
48
+ * Throws error if used outside AuthProvider
49
+ */
50
+ declare function useAuth(): AuthContextValue;
51
+ /**
52
+ * Hook to require authentication
53
+ * Returns null and logs warning if not authenticated
54
+ */
55
+ declare function useRequireAuth(): AuthContextValue | null;
56
+
57
+ interface AuthProviderProps {
58
+ /**
59
+ * Current authenticated user (null if not authenticated)
60
+ */
61
+ user: AuthUser | null;
62
+ /**
63
+ * Current session (null if not authenticated)
64
+ */
65
+ session: AuthSession | null;
66
+ /**
67
+ * RBAC configuration for role/permission checking
68
+ */
69
+ rbacConfig: RBACConfig;
70
+ /**
71
+ * Whether auth is still loading
72
+ */
73
+ isLoading?: boolean;
74
+ /**
75
+ * Child components
76
+ */
77
+ children: ReactNode;
78
+ }
79
+ /**
80
+ * AuthProvider - Provides authentication state to component tree
81
+ *
82
+ * @example
83
+ * ```tsx
84
+ * <AuthProvider user={user} session={session} rbacConfig={config}>
85
+ * <App />
86
+ * </AuthProvider>
87
+ * ```
88
+ */
89
+ declare function AuthProvider({ user, session, rbacConfig, isLoading, children, }: AuthProviderProps): ReactElement;
90
+
91
+ /**
92
+ * Props that any component wrapped with withAuth will receive
93
+ */
94
+ interface ComponentProps {
95
+ id?: string;
96
+ [key: string]: any;
97
+ }
98
+ /**
99
+ * Higher-order component that wraps a component with authentication checks
100
+ *
101
+ * @example
102
+ * ```tsx
103
+ * const ProtectedButton = withAuth(Button, {
104
+ * required_roles: ['ADMIN'],
105
+ * fallback: 'message',
106
+ * fallback_message: 'Only admins can see this button'
107
+ * });
108
+ * ```
109
+ *
110
+ * @param Component - The component to wrap
111
+ * @param authConfig - Authentication requirements from YAML
112
+ * @returns Wrapped component with auth enforcement
113
+ */
114
+ declare function withAuth<P extends ComponentProps>(Component: React__default.ComponentType<P>, authConfig?: ComponentAuthConfig): React__default.ComponentType<P>;
115
+ /**
116
+ * Custom fallback component (for advanced use cases)
117
+ */
118
+ declare function withAuthFallback<P extends ComponentProps>(Component: React__default.ComponentType<P>, authConfig: ComponentAuthConfig, FallbackComponent: React__default.ComponentType<any>): React__default.ComponentType<P>;
119
+
120
+ /**
121
+ * Register the auth decorator for use by content renderers
122
+ *
123
+ * This should be called once in your app's initialization (e.g., _app.tsx)
124
+ * to enable auth-aware component rendering.
125
+ *
126
+ * @example
127
+ * ```tsx
128
+ * // In pages/_app.tsx
129
+ * import { registerAuthDecorator } from '@stackwright-pro/auth';
130
+ *
131
+ * registerAuthDecorator();
132
+ *
133
+ * function MyApp({ Component, pageProps }: AppProps) {
134
+ * return (
135
+ * <AuthProvider {...authProps}>
136
+ * <Component {...pageProps} />
137
+ * </AuthProvider>
138
+ * );
139
+ * }
140
+ * ```
141
+ */
142
+ declare function registerAuthDecorator(): void;
143
+ /**
144
+ * Get the registered auth decorator (for use by content renderers)
145
+ *
146
+ * Returns null if auth is not registered, allowing graceful degradation.
147
+ * This is the function that OSS core would call to check if auth is available.
148
+ *
149
+ * @returns The withAuth decorator function, or null if not registered
150
+ */
151
+ declare function getAuthDecorator(): typeof withAuth | null;
152
+ /**
153
+ * Wrap a component with auth if decorator is registered and config exists
154
+ *
155
+ * This is a safe wrapper that OSS packages can use without depending on auth.
156
+ * If auth is not registered, returns the original component unchanged.
157
+ * If auth config is missing/undefined, returns the original component unchanged.
158
+ *
159
+ * @example
160
+ * ```tsx
161
+ * // In content renderer (can live in OSS core!)\n * function renderContentItem(item: ContentItem) {
162
+ * const Component = getComponentFromRegistry(item.type);
163
+ *
164
+ * // Apply auth if available
165
+ * const WrappedComponent = maybeWrapWithAuth(Component, item.auth);
166
+ *
167
+ * return <WrappedComponent {...item} />;
168
+ * }
169
+ * ```
170
+ *
171
+ * @param Component - Component to wrap
172
+ * @param authConfig - Auth configuration from YAML (optional)
173
+ * @returns Wrapped component if auth is registered, original component otherwise
174
+ */
175
+ declare function maybeWrapWithAuth<P extends {
176
+ id?: string;
177
+ [key: string]: any;
178
+ }>(Component: React__default.ComponentType<P>, authConfig?: ComponentAuthConfig): React__default.ComponentType<P>;
179
+ /**
180
+ * Type guard to check if content item has auth config
181
+ *
182
+ * Useful for conditionally applying auth in renderers without
183
+ * needing to import auth types.
184
+ *
185
+ * @example
186
+ * ```tsx
187
+ * if (hasAuthConfig(item)) {
188
+ * // TypeScript knows item.auth exists
189
+ * WrappedComponent = maybeWrapWithAuth(Component, item.auth);
190
+ * }
191
+ * ```
192
+ */
193
+ declare function hasAuthConfig(item: any): item is {
194
+ auth: ComponentAuthConfig;
195
+ };
196
+
197
+ export { AuthContext, type AuthContextValue, AuthProvider, type AuthProviderProps, type ComponentProps, getAuthDecorator, hasAuthConfig, maybeWrapWithAuth, registerAuthDecorator, useAuth, useRequireAuth, withAuth, withAuthFallback };
@@ -0,0 +1,197 @@
1
+ import * as React from 'react';
2
+ import React__default, { ReactNode, ReactElement } from 'react';
3
+ import { AuthUser, AuthSession, RBACConfig, ComponentAuthConfig } from '@stackwright-pro/types';
4
+
5
+ /**
6
+ * Auth context value provided to component tree
7
+ */
8
+ interface AuthContextValue {
9
+ /**
10
+ * Currently authenticated user (null if not authenticated)
11
+ */
12
+ user: AuthUser | null;
13
+ /**
14
+ * Current session (null if not authenticated)
15
+ */
16
+ session: AuthSession | null;
17
+ /**
18
+ * Whether user is authenticated
19
+ */
20
+ isAuthenticated: boolean;
21
+ /**
22
+ * Whether auth is still loading
23
+ */
24
+ isLoading: boolean;
25
+ /**
26
+ * Check if user has a specific role
27
+ */
28
+ hasRole: (role: string) => boolean;
29
+ /**
30
+ * Check if user has a specific permission
31
+ */
32
+ hasPermission: (permission: string) => boolean;
33
+ /**
34
+ * Check if user has any of the specified roles
35
+ */
36
+ hasAnyRole: (roles: string[]) => boolean;
37
+ /**
38
+ * Check if user has all of the specified permissions
39
+ */
40
+ hasAllPermissions: (permissions: string[]) => boolean;
41
+ }
42
+ /**
43
+ * Auth context - provides authentication state to components
44
+ */
45
+ declare const AuthContext: React.Context<AuthContextValue | null>;
46
+ /**
47
+ * Hook to access auth context
48
+ * Throws error if used outside AuthProvider
49
+ */
50
+ declare function useAuth(): AuthContextValue;
51
+ /**
52
+ * Hook to require authentication
53
+ * Returns null and logs warning if not authenticated
54
+ */
55
+ declare function useRequireAuth(): AuthContextValue | null;
56
+
57
+ interface AuthProviderProps {
58
+ /**
59
+ * Current authenticated user (null if not authenticated)
60
+ */
61
+ user: AuthUser | null;
62
+ /**
63
+ * Current session (null if not authenticated)
64
+ */
65
+ session: AuthSession | null;
66
+ /**
67
+ * RBAC configuration for role/permission checking
68
+ */
69
+ rbacConfig: RBACConfig;
70
+ /**
71
+ * Whether auth is still loading
72
+ */
73
+ isLoading?: boolean;
74
+ /**
75
+ * Child components
76
+ */
77
+ children: ReactNode;
78
+ }
79
+ /**
80
+ * AuthProvider - Provides authentication state to component tree
81
+ *
82
+ * @example
83
+ * ```tsx
84
+ * <AuthProvider user={user} session={session} rbacConfig={config}>
85
+ * <App />
86
+ * </AuthProvider>
87
+ * ```
88
+ */
89
+ declare function AuthProvider({ user, session, rbacConfig, isLoading, children, }: AuthProviderProps): ReactElement;
90
+
91
+ /**
92
+ * Props that any component wrapped with withAuth will receive
93
+ */
94
+ interface ComponentProps {
95
+ id?: string;
96
+ [key: string]: any;
97
+ }
98
+ /**
99
+ * Higher-order component that wraps a component with authentication checks
100
+ *
101
+ * @example
102
+ * ```tsx
103
+ * const ProtectedButton = withAuth(Button, {
104
+ * required_roles: ['ADMIN'],
105
+ * fallback: 'message',
106
+ * fallback_message: 'Only admins can see this button'
107
+ * });
108
+ * ```
109
+ *
110
+ * @param Component - The component to wrap
111
+ * @param authConfig - Authentication requirements from YAML
112
+ * @returns Wrapped component with auth enforcement
113
+ */
114
+ declare function withAuth<P extends ComponentProps>(Component: React__default.ComponentType<P>, authConfig?: ComponentAuthConfig): React__default.ComponentType<P>;
115
+ /**
116
+ * Custom fallback component (for advanced use cases)
117
+ */
118
+ declare function withAuthFallback<P extends ComponentProps>(Component: React__default.ComponentType<P>, authConfig: ComponentAuthConfig, FallbackComponent: React__default.ComponentType<any>): React__default.ComponentType<P>;
119
+
120
+ /**
121
+ * Register the auth decorator for use by content renderers
122
+ *
123
+ * This should be called once in your app's initialization (e.g., _app.tsx)
124
+ * to enable auth-aware component rendering.
125
+ *
126
+ * @example
127
+ * ```tsx
128
+ * // In pages/_app.tsx
129
+ * import { registerAuthDecorator } from '@stackwright-pro/auth';
130
+ *
131
+ * registerAuthDecorator();
132
+ *
133
+ * function MyApp({ Component, pageProps }: AppProps) {
134
+ * return (
135
+ * <AuthProvider {...authProps}>
136
+ * <Component {...pageProps} />
137
+ * </AuthProvider>
138
+ * );
139
+ * }
140
+ * ```
141
+ */
142
+ declare function registerAuthDecorator(): void;
143
+ /**
144
+ * Get the registered auth decorator (for use by content renderers)
145
+ *
146
+ * Returns null if auth is not registered, allowing graceful degradation.
147
+ * This is the function that OSS core would call to check if auth is available.
148
+ *
149
+ * @returns The withAuth decorator function, or null if not registered
150
+ */
151
+ declare function getAuthDecorator(): typeof withAuth | null;
152
+ /**
153
+ * Wrap a component with auth if decorator is registered and config exists
154
+ *
155
+ * This is a safe wrapper that OSS packages can use without depending on auth.
156
+ * If auth is not registered, returns the original component unchanged.
157
+ * If auth config is missing/undefined, returns the original component unchanged.
158
+ *
159
+ * @example
160
+ * ```tsx
161
+ * // In content renderer (can live in OSS core!)\n * function renderContentItem(item: ContentItem) {
162
+ * const Component = getComponentFromRegistry(item.type);
163
+ *
164
+ * // Apply auth if available
165
+ * const WrappedComponent = maybeWrapWithAuth(Component, item.auth);
166
+ *
167
+ * return <WrappedComponent {...item} />;
168
+ * }
169
+ * ```
170
+ *
171
+ * @param Component - Component to wrap
172
+ * @param authConfig - Auth configuration from YAML (optional)
173
+ * @returns Wrapped component if auth is registered, original component otherwise
174
+ */
175
+ declare function maybeWrapWithAuth<P extends {
176
+ id?: string;
177
+ [key: string]: any;
178
+ }>(Component: React__default.ComponentType<P>, authConfig?: ComponentAuthConfig): React__default.ComponentType<P>;
179
+ /**
180
+ * Type guard to check if content item has auth config
181
+ *
182
+ * Useful for conditionally applying auth in renderers without
183
+ * needing to import auth types.
184
+ *
185
+ * @example
186
+ * ```tsx
187
+ * if (hasAuthConfig(item)) {
188
+ * // TypeScript knows item.auth exists
189
+ * WrappedComponent = maybeWrapWithAuth(Component, item.auth);
190
+ * }
191
+ * ```
192
+ */
193
+ declare function hasAuthConfig(item: any): item is {
194
+ auth: ComponentAuthConfig;
195
+ };
196
+
197
+ export { AuthContext, type AuthContextValue, AuthProvider, type AuthProviderProps, type ComponentProps, getAuthDecorator, hasAuthConfig, maybeWrapWithAuth, registerAuthDecorator, useAuth, useRequireAuth, withAuth, withAuthFallback };