@korajs/auth 0.5.0 → 0.6.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/dist/react.d.ts CHANGED
@@ -1,64 +1,21 @@
1
1
  import * as react from 'react';
2
2
  import { ReactNode, ReactElement } from 'react';
3
- import { d as AuthClient, e as AuthState$1, j as AuthUser, O as OAuthAuthorizationOptions, o as OAuthAuthorizationResult, p as OAuthCallbackParams, L as LinkedOAuthAccount, q as OrgClient, k as ClientMembership, C as ClientInvitation, l as ClientOrganization } from './org-client-q2u55qod.js';
3
+ import { d as AuthClient, e as AuthState$1, l as AuthUser, O as OAuthAuthorizationOptions, q as OAuthAuthorizationResult, r as OAuthCallbackParams, L as LinkedOAuthAccount, i as AuthSession, s as OrgClient, v as OrgSession, m as ClientMembership, C as ClientInvitation, n as ClientOrganization } from './create-org-session-RsDj9cl4.js';
4
+ export { x as checkOrgPermission } from './create-org-session-RsDj9cl4.js';
5
+ import * as react_jsx_runtime from 'react/jsx-runtime';
4
6
  import '@korajs/core';
5
7
 
6
- /**
7
- * Props for the AuthProvider component.
8
- */
9
8
  interface AuthProviderProps {
10
- /** The AuthClient instance to provide to child components */
11
9
  client: AuthClient;
12
- /** Child components that will have access to auth context */
13
10
  children: ReactNode;
14
- /**
15
- * Optional fallback content to render while the auth client is initializing.
16
- * If not provided, children are rendered with `isLoading: true` in the context.
17
- */
18
11
  fallback?: ReactNode;
19
12
  }
20
- /**
21
- * React context provider that wraps the AuthClient for use with auth hooks.
22
- *
23
- * Calls `client.initialize()` on mount to restore any existing session from
24
- * stored tokens. Subscribes to auth state changes and re-renders children
25
- * when the state transitions.
26
- *
27
- * Must be placed above any component that uses {@link useAuth},
28
- * {@link useCurrentUser}, or {@link useAuthStatus}.
29
- *
30
- * @param props - Provider props including the AuthClient instance and children
31
- * @returns A React element wrapping children in the AuthContext
32
- *
33
- * @example
34
- * ```typescript
35
- * import { AuthClient } from '@korajs/auth'
36
- * import { AuthProvider } from '@korajs/auth/react'
37
- *
38
- * const authClient = new AuthClient({ serverUrl: 'http://localhost:3001' })
39
- *
40
- * function App() {
41
- * return (
42
- * <AuthProvider client={authClient} fallback={<div>Loading...</div>}>
43
- * <MyApp />
44
- * </AuthProvider>
45
- * )
46
- * }
47
- * ```
48
- */
49
13
  declare function AuthProvider({ client, children, fallback }: AuthProviderProps): ReactElement;
50
14
 
51
- /**
52
- * Return value of the {@link useAuth} hook.
53
- */
54
15
  interface UseAuthResult {
55
- /** Current authenticated user, or null if not signed in */
56
16
  user: AuthUser | null;
57
- /** Whether the user is currently authenticated */
58
17
  isAuthenticated: boolean;
59
- /** Whether the auth client is still initializing (restoring session) */
60
18
  isLoading: boolean;
61
- /** Sign up a new user account */
62
19
  signUp: (params: {
63
20
  email: string;
64
21
  password: string;
@@ -66,245 +23,88 @@ interface UseAuthResult {
66
23
  deviceId?: string;
67
24
  devicePublicKey?: string;
68
25
  }) => Promise<void>;
69
- /** Sign in with email and password */
70
26
  signIn: (params: {
71
27
  email: string;
72
28
  password: string;
73
29
  deviceId?: string;
74
30
  devicePublicKey?: string;
75
31
  }) => Promise<void>;
76
- /** Start OAuth sign-in. Web apps can redirect; desktop/mobile can open the returned URL. */
77
32
  signInWithOAuth: (provider: string, options?: OAuthAuthorizationOptions) => Promise<OAuthAuthorizationResult>;
78
- /** Complete an OAuth sign-in callback with code and state. */
79
33
  completeOAuthSignIn: (provider: string, params: OAuthCallbackParams) => Promise<void>;
80
- /** Create an OAuth authorization URL without redirecting. */
81
34
  getOAuthAuthorizationUrl: (provider: string, options?: OAuthAuthorizationOptions) => Promise<OAuthAuthorizationResult>;
82
- /** Link an OAuth provider to the current user. */
83
35
  linkOAuth: (provider: string, params: OAuthCallbackParams) => Promise<LinkedOAuthAccount | null>;
84
- /** List OAuth accounts linked to the current user. */
85
36
  listLinkedAccounts: () => Promise<LinkedOAuthAccount[]>;
86
- /** Unlink an OAuth provider from the current user. */
87
37
  unlinkOAuth: (provider: string) => Promise<void>;
88
- /** Sign out the current user */
89
38
  signOut: () => Promise<void>;
90
- /** Last error message from a sign-up, sign-in, or sign-out attempt, or null */
91
39
  error: string | null;
40
+ initError: Error | null;
92
41
  }
93
- /**
94
- * Auth status information returned by {@link useAuthStatus}.
95
- */
96
42
  interface AuthStatus {
97
- /** Current authentication state */
98
43
  state: AuthState$1;
99
- /** Whether the user is currently authenticated */
100
44
  isAuthenticated: boolean;
101
- /** Whether the auth client is still initializing */
102
45
  isLoading: boolean;
103
46
  }
104
- /**
105
- * React hook providing full authentication functionality.
106
- *
107
- * Returns the current user, loading state, error state, and methods for
108
- * sign-up, sign-in, and sign-out. Re-renders when auth state changes.
109
- *
110
- * Must be used within an {@link AuthProvider}.
111
- *
112
- * @returns An object with user info, auth methods, and status flags
113
- *
114
- * @example
115
- * ```typescript
116
- * function LoginPage() {
117
- * const { user, isAuthenticated, isLoading, signIn, error } = useAuth()
118
- *
119
- * if (isLoading) return <div>Loading...</div>
120
- * if (isAuthenticated) return <div>Welcome, {user?.name}</div>
121
- *
122
- * return (
123
- * <form onSubmit={async (e) => {
124
- * e.preventDefault()
125
- * await signIn({ email: 'user@example.com', password: 'secret' })
126
- * }}>
127
- * {error && <p>{error}</p>}
128
- * <button type="submit">Sign In</button>
129
- * </form>
130
- * )
131
- * }
132
- * ```
133
- */
134
47
  declare function useAuth(): UseAuthResult;
135
- /**
136
- * React hook that returns the currently authenticated user, or null.
137
- *
138
- * A lightweight alternative to {@link useAuth} when you only need the user
139
- * object and do not need auth methods or error state.
140
- *
141
- * Must be used within an {@link AuthProvider}.
142
- *
143
- * @returns The current AuthUser or null if not authenticated
144
- *
145
- * @example
146
- * ```typescript
147
- * function UserAvatar() {
148
- * const user = useCurrentUser()
149
- * if (!user) return null
150
- * return <span>{user.name ?? user.email}</span>
151
- * }
152
- * ```
153
- */
154
48
  declare function useCurrentUser(): AuthUser | null;
155
- /**
156
- * React hook that returns the current authentication status.
157
- *
158
- * Re-renders only when the auth state changes, not on every auth event.
159
- * Use this for status indicators, route guards, and conditional rendering.
160
- *
161
- * Must be used within an {@link AuthProvider}.
162
- *
163
- * @returns An AuthStatus object with state, isAuthenticated, and isLoading flags
164
- *
165
- * @example
166
- * ```typescript
167
- * function AuthGuard({ children }: { children: React.ReactNode }) {
168
- * const { isAuthenticated, isLoading } = useAuthStatus()
169
- * if (isLoading) return <Spinner />
170
- * if (!isAuthenticated) return <Navigate to="/login" />
171
- * return <>{children}</>
172
- * }
173
- * ```
174
- */
175
49
  declare function useAuthStatus(): AuthStatus;
176
50
 
177
51
  /**
178
52
  * Possible authentication states for the client.
179
- * - 'loading': Initial state while restoring tokens from storage
180
- * - 'authenticated': User is signed in with a valid session
181
- * - 'unauthenticated': No valid session exists
182
53
  */
183
54
  type AuthState = 'loading' | 'authenticated' | 'unauthenticated';
184
55
  /**
185
56
  * Shape of the value provided by the AuthContext.
186
- * Includes the AuthClient instance, reactive state, and a loading flag.
187
57
  */
188
58
  interface AuthContextValue {
189
- /** The underlying AuthClient instance for direct access */
190
59
  client: AuthClient;
191
- /** Current authentication state */
60
+ session: AuthSession;
192
61
  state: AuthState;
193
- /** Whether the client is still initializing (restoring session from storage) */
194
62
  isLoading: boolean;
195
63
  }
196
64
  /**
197
65
  * React context for Kora authentication.
198
- *
199
- * Provides the AuthClient and reactive auth state to child components.
200
- * Must be provided by an AuthProvider higher in the component tree.
201
- * Defaults to null — hooks that consume this context throw if it is missing.
202
66
  */
203
67
  declare const AuthContext: react.Context<AuthContextValue | null>;
204
68
 
205
- /**
206
- * Shape of the OrgContext value.
207
- */
208
69
  interface OrgContextValue {
209
- /** The OrgClient instance */
210
70
  client: OrgClient;
71
+ session: OrgSession;
211
72
  }
212
- /**
213
- * React context for organization state.
214
- */
215
73
  declare const OrgContext: react.Context<OrgContextValue | null>;
216
- /**
217
- * Return value of the {@link useOrg} hook.
218
- */
219
74
  interface UseOrgResult {
220
- /** Currently active organization, or null */
221
75
  org: ClientOrganization | null;
222
- /** Current user's role in the active organization, or null */
223
76
  role: string | null;
224
- /** Active organization ID, or null */
225
77
  orgId: string | null;
226
- /** Switch to a different organization */
227
78
  switchOrg: (orgId: string) => Promise<void>;
228
- /** Create a new organization */
229
79
  createOrg: (params: {
230
80
  name: string;
231
81
  slug?: string;
232
82
  }) => Promise<ClientOrganization>;
233
- /** Leave the active organization */
234
83
  leaveOrg: () => Promise<void>;
235
- /** Clear the active organization */
236
84
  clearOrg: () => void;
237
- /** List all organizations the user belongs to */
238
85
  listOrgs: () => Promise<ClientOrganization[]>;
239
- /** Last error, or null */
240
86
  error: string | null;
241
87
  }
242
- /**
243
- * React hook for organization management and context switching.
244
- *
245
- * Re-renders when the active organization changes.
246
- *
247
- * @example
248
- * ```typescript
249
- * function OrgSwitcher() {
250
- * const { org, switchOrg, listOrgs, error } = useOrg()
251
- * const [orgs, setOrgs] = useState<ClientOrganization[]>([])
252
- *
253
- * useEffect(() => { listOrgs().then(setOrgs) }, [listOrgs])
254
- *
255
- * return (
256
- * <select value={org?.id ?? ''} onChange={(e) => switchOrg(e.target.value)}>
257
- * <option value="">Select org...</option>
258
- * {orgs.map(o => <option key={o.id} value={o.id}>{o.name}</option>)}
259
- * </select>
260
- * )
261
- * }
262
- * ```
263
- */
264
88
  declare function useOrg(): UseOrgResult;
265
- /**
266
- * Return value of the {@link useOrgMembers} hook.
267
- */
268
89
  interface UseOrgMembersResult {
269
- /** Members of the organization (empty until loaded) */
270
90
  members: ClientMembership[];
271
- /** Whether members are being loaded */
272
91
  isLoading: boolean;
273
- /** Reload the members list */
274
92
  refresh: () => Promise<void>;
275
- /** Invite a user by email */
276
93
  invite: (email: string, role: string) => Promise<ClientInvitation>;
277
- /** Remove a member */
278
94
  removeMember: (userId: string) => Promise<void>;
279
- /** Update a member's role */
280
95
  updateRole: (userId: string, role: string) => Promise<void>;
281
- /** Last error, or null */
282
96
  error: string | null;
283
97
  }
284
- /**
285
- * React hook for managing organization members.
286
- *
287
- * Automatically loads members when the orgId changes.
288
- *
289
- * @param orgId - Organization ID to manage members for
290
- */
291
98
  declare function useOrgMembers(orgId: string): UseOrgMembersResult;
99
+ declare function usePermission(requiredRole: string): boolean;
100
+
101
+ interface OrgProviderProps {
102
+ client: OrgClient;
103
+ children?: ReactNode;
104
+ }
292
105
  /**
293
- * React hook that checks if the current user has a specific role level
294
- * in the active organization.
295
- *
296
- * @param requiredRole - Minimum role required (uses ROLE_HIERARCHY from org-types)
297
- * @returns true if the user's role is at least requiredRole
298
- *
299
- * @example
300
- * ```typescript
301
- * function AdminPanel() {
302
- * const canManage = usePermission('admin')
303
- * if (!canManage) return <p>Access denied</p>
304
- * return <AdminSettings />
305
- * }
306
- * ```
106
+ * Provides organization context for {@link useOrg}, {@link useOrgMembers}, and {@link usePermission}.
307
107
  */
308
- declare function usePermission(requiredRole: string): boolean;
108
+ declare function OrgProvider({ client, children }: OrgProviderProps): react_jsx_runtime.JSX.Element;
309
109
 
310
- export { AuthContext, type AuthContextValue, AuthProvider, type AuthProviderProps, type AuthStatus, OrgContext, type OrgContextValue, type UseAuthResult, type UseOrgMembersResult, type UseOrgResult, useAuth, useAuthStatus, useCurrentUser, useOrg, useOrgMembers, usePermission };
110
+ export { AuthContext, type AuthContextValue, AuthProvider, type AuthProviderProps, type AuthStatus, OrgContext, type OrgContextValue, OrgProvider, type OrgProviderProps, type UseAuthResult, type UseOrgMembersResult, type UseOrgResult, useAuth, useAuthStatus, useCurrentUser, useOrg, useOrgMembers, usePermission };