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