@object-ui/auth 0.1.0 → 3.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,115 @@
1
+ # @object-ui/auth
2
+
3
+ Authentication system for Object UI — AuthProvider, guards, login/register forms, and token management.
4
+
5
+ ## Features
6
+
7
+ - 🔐 **AuthProvider Context** - Wrap your app with authentication state and methods
8
+ - 🛡️ **AuthGuard** - Protect routes and components from unauthenticated access
9
+ - 📝 **Pre-built Forms** - LoginForm, RegisterForm, and ForgotPasswordForm ready to use
10
+ - 👤 **UserMenu** - Display authenticated user info with sign-out support
11
+ - 🔑 **Auth Client Factory** - `createAuthClient` for pluggable backend integration
12
+ - 🌐 **Authenticated Fetch** - `createAuthenticatedFetch` for automatic token injection
13
+ - 🎯 **Type-Safe** - Full TypeScript support with exported types
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @object-ui/auth
19
+ ```
20
+
21
+ **Peer Dependencies:**
22
+ - `react` ^18.0.0 || ^19.0.0
23
+ - `react-dom` ^18.0.0 || ^19.0.0
24
+
25
+ ## Quick Start
26
+
27
+ ```tsx
28
+ import { AuthProvider, useAuth, AuthGuard } from '@object-ui/auth';
29
+ import { createAuthClient } from '@object-ui/auth';
30
+
31
+ const authClient = createAuthClient({
32
+ provider: 'custom',
33
+ apiUrl: 'https://api.example.com/auth',
34
+ });
35
+
36
+ function App() {
37
+ return (
38
+ <AuthProvider client={authClient}>
39
+ <AuthGuard fallback={<LoginPage />}>
40
+ <Dashboard />
41
+ </AuthGuard>
42
+ </AuthProvider>
43
+ );
44
+ }
45
+
46
+ function Dashboard() {
47
+ const { user, signOut } = useAuth();
48
+ return (
49
+ <div>
50
+ <p>Welcome, {user?.name}</p>
51
+ <button onClick={signOut}>Sign Out</button>
52
+ </div>
53
+ );
54
+ }
55
+ ```
56
+
57
+ ## API
58
+
59
+ ### AuthProvider
60
+
61
+ Wraps your application with authentication context:
62
+
63
+ ```tsx
64
+ <AuthProvider client={authClient}>
65
+ <App />
66
+ </AuthProvider>
67
+ ```
68
+
69
+ ### useAuth
70
+
71
+ Hook for accessing auth state and methods:
72
+
73
+ ```tsx
74
+ const { user, session, signIn, signOut, signUp, isAuthenticated, isLoading } = useAuth();
75
+ ```
76
+
77
+ ### AuthGuard
78
+
79
+ Protects children from unauthenticated access:
80
+
81
+ ```tsx
82
+ <AuthGuard fallback={<LoginForm />}>
83
+ <ProtectedContent />
84
+ </AuthGuard>
85
+ ```
86
+
87
+ ### LoginForm / RegisterForm / ForgotPasswordForm
88
+
89
+ Pre-built authentication form components:
90
+
91
+ ```tsx
92
+ <LoginForm onSuccess={() => navigate('/dashboard')} />
93
+ <RegisterForm onSuccess={() => navigate('/welcome')} />
94
+ <ForgotPasswordForm onSuccess={() => navigate('/check-email')} />
95
+ ```
96
+
97
+ ### UserMenu
98
+
99
+ Displays current user info with avatar and sign-out:
100
+
101
+ ```tsx
102
+ <UserMenu />
103
+ ```
104
+
105
+ ### createAuthenticatedFetch
106
+
107
+ Creates a fetch wrapper that injects auth tokens into DataSource requests:
108
+
109
+ ```tsx
110
+ const authedFetch = createAuthenticatedFetch({ getToken: () => session.token });
111
+ ```
112
+
113
+ ## License
114
+
115
+ MIT
@@ -9,6 +9,13 @@ import React from 'react';
9
9
  import type { AuthProviderConfig } from './types';
10
10
  export interface AuthProviderProps extends AuthProviderConfig {
11
11
  children: React.ReactNode;
12
+ /**
13
+ * Whether authentication is enabled.
14
+ * When false, the provider will skip authentication checks and treat all users as authenticated.
15
+ * Useful for development or demo environments where the server doesn't have authentication enabled.
16
+ * @default true
17
+ */
18
+ enabled?: boolean;
12
19
  }
13
20
  /**
14
21
  * Authentication context provider.
@@ -22,6 +29,13 @@ export interface AuthProviderProps extends AuthProviderConfig {
22
29
  * <App />
23
30
  * </AuthProvider>
24
31
  * ```
32
+ *
33
+ * @example With disabled auth (development mode)
34
+ * ```tsx
35
+ * <AuthProvider authUrl="/api/auth" enabled={false}>
36
+ * <App />
37
+ * </AuthProvider>
38
+ * ```
25
39
  */
26
- export declare function AuthProvider({ authUrl, client: externalClient, onAuthStateChange, children, }: AuthProviderProps): import("react/jsx-runtime").JSX.Element;
40
+ export declare function AuthProvider({ authUrl, client: externalClient, onAuthStateChange, enabled, children, }: AuthProviderProps): import("react/jsx-runtime").JSX.Element;
27
41
  //# sourceMappingURL=AuthProvider.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"AuthProvider.d.ts","sourceRoot":"","sources":["../src/AuthProvider.tsx"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAoD,MAAM,OAAO,CAAC;AACzE,OAAO,KAAK,EAAwB,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAIxE,MAAM,WAAW,iBAAkB,SAAQ,kBAAkB;IAC3D,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,EAC3B,OAAO,EACP,MAAM,EAAE,cAAc,EACtB,iBAAiB,EACjB,QAAQ,GACT,EAAE,iBAAiB,2CAmKnB"}
1
+ {"version":3,"file":"AuthProvider.d.ts","sourceRoot":"","sources":["../src/AuthProvider.tsx"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAoD,MAAM,OAAO,CAAC;AACzE,OAAO,KAAK,EAAwB,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAIxE,MAAM,WAAW,iBAAkB,SAAQ,kBAAkB;IAC3D,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B;;;;;OAKG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,YAAY,CAAC,EAC3B,OAAO,EACP,MAAM,EAAE,cAAc,EACtB,iBAAiB,EACjB,OAAc,EACd,QAAQ,GACT,EAAE,iBAAiB,2CAqLnB"}
@@ -21,16 +21,40 @@ import { createAuthClient } from './createAuthClient';
21
21
  * <App />
22
22
  * </AuthProvider>
23
23
  * ```
24
+ *
25
+ * @example With disabled auth (development mode)
26
+ * ```tsx
27
+ * <AuthProvider authUrl="/api/auth" enabled={false}>
28
+ * <App />
29
+ * </AuthProvider>
30
+ * ```
24
31
  */
25
- export function AuthProvider({ authUrl, client: externalClient, onAuthStateChange, children, }) {
32
+ export function AuthProvider({ authUrl, client: externalClient, onAuthStateChange, enabled = true, children, }) {
26
33
  const client = useMemo(() => externalClient ?? createAuthClient({ baseURL: authUrl }), [externalClient, authUrl]);
27
34
  const [user, setUser] = useState(null);
28
35
  const [session, setSession] = useState(null);
29
36
  const [isLoading, setIsLoading] = useState(true);
30
37
  const [error, setError] = useState(null);
31
- const isAuthenticated = user !== null && session !== null;
32
- // Load session on mount
38
+ // If auth is disabled, automatically set as authenticated with a guest user
39
+ const isAuthenticated = enabled
40
+ ? user !== null && session !== null
41
+ : true;
42
+ // Load session on mount (only if auth is enabled)
33
43
  useEffect(() => {
44
+ if (!enabled) {
45
+ // When auth is disabled, set a guest user and mark as loaded
46
+ setUser({
47
+ id: 'guest',
48
+ email: 'guest@local',
49
+ name: 'Guest User',
50
+ });
51
+ setSession({
52
+ token: 'guest-token',
53
+ expiresAt: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000), // 1 year
54
+ });
55
+ setIsLoading(false);
56
+ return;
57
+ }
34
58
  let cancelled = false;
35
59
  async function loadSession() {
36
60
  try {
@@ -55,7 +79,7 @@ export function AuthProvider({ authUrl, client: externalClient, onAuthStateChang
55
79
  }
56
80
  loadSession();
57
81
  return () => { cancelled = true; };
58
- }, [client]);
82
+ }, [client, enabled]);
59
83
  // Notify on auth state changes
60
84
  useEffect(() => {
61
85
  onAuthStateChange?.({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@object-ui/auth",
3
- "version": "0.1.0",
3
+ "version": "3.0.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "Authentication system for Object UI with AuthProvider, useAuth hook, AuthGuard, and form components.",
@@ -26,11 +26,11 @@
26
26
  "react": "^18.0.0 || ^19.0.0"
27
27
  },
28
28
  "dependencies": {
29
- "@object-ui/types": "0.5.0"
29
+ "@object-ui/types": "3.0.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@types/react": "^19.2.13",
33
- "react": "^19.1.0",
32
+ "@types/react": "19.2.13",
33
+ "react": "19.2.4",
34
34
  "typescript": "^5.9.3",
35
35
  "vitest": "^4.0.18"
36
36
  },