@geekapps/auth-nextjs 0.1.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,73 @@
1
+ # @geekapps/auth-nextjs
2
+
3
+ SDK Next.js para consumir o Geekapps Auth/IDP.
4
+
5
+ ## Instalação
6
+
7
+ ```bash
8
+ bun add @geekapps/auth-nextjs
9
+ ```
10
+
11
+ ## Server
12
+
13
+ ```ts
14
+ // src/lib/auth.ts
15
+ import { configureGeekappsAuth } from "@geekapps/auth-nextjs/server";
16
+
17
+ export const auth = configureGeekappsAuth({
18
+ issuer: process.env.GEEKAPPS_AUTH_ISSUER!,
19
+ clientId: process.env.GEEKAPPS_AUTH_CLIENT_ID!,
20
+ clientSecret: process.env.GEEKAPPS_AUTH_CLIENT_SECRET,
21
+ redirectUri: process.env.GEEKAPPS_AUTH_REDIRECT_URI!,
22
+ scopes: ["openid", "profile", "email"],
23
+ });
24
+ ```
25
+
26
+ ```ts
27
+ // app/api/auth/[...routes]/route.ts
28
+ import { auth } from "@/lib/auth";
29
+
30
+ export const GET = auth.handlers.GET;
31
+ export const POST = auth.handlers.POST;
32
+ export const PATCH = auth.handlers.PATCH;
33
+ export const DELETE = auth.handlers.DELETE;
34
+ ```
35
+
36
+ ## Client
37
+
38
+ ```tsx
39
+ import { GeekappsAuthProvider } from "@geekapps/auth-nextjs/client";
40
+
41
+ export function Providers({ children }: { children: React.ReactNode }) {
42
+ return <GeekappsAuthProvider>{children}</GeekappsAuthProvider>;
43
+ }
44
+ ```
45
+
46
+ Hooks principais:
47
+
48
+ - `useAuth()`
49
+ - `useOrg()`
50
+ - `useUsers()`
51
+ - `useRoles()`
52
+ - `usePermissions()`
53
+ - `useSessions()`
54
+
55
+
56
+ ## Standalone client auth actions
57
+
58
+ `@geekapps/auth-nextjs/client` exporta funções standalone usadas pela `auth-ui`:
59
+
60
+ ```ts
61
+ import {
62
+ loginWithPassword,
63
+ registerWithPassword,
64
+ requestPasswordlessLink,
65
+ startGoogleLogin,
66
+ } from "@geekapps/auth-nextjs/client";
67
+ ```
68
+
69
+ Depois de aplicar esse source, rode o build do package para gerar o `dist`:
70
+
71
+ ```bash
72
+ bun --filter @geekapps/auth-nextjs build
73
+ ```
@@ -0,0 +1,379 @@
1
+ import { MouseEvent, ReactNode, ReactElement } from 'react';
2
+
3
+ type UserStatus = "active" | "inactive" | "suspended";
4
+ type OrganizationStatus = "active" | "inactive" | "suspended";
5
+ type OAuthClientType = "public" | "confidential";
6
+ type AuthResult<T = unknown> = {
7
+ success: true;
8
+ data: T;
9
+ } | {
10
+ success: false;
11
+ error: string;
12
+ status?: number;
13
+ };
14
+ type User = {
15
+ id: string;
16
+ email: string;
17
+ name: string | null;
18
+ phone: string | null;
19
+ imageUrl: string | null;
20
+ status?: UserStatus;
21
+ createdAt?: string | Date;
22
+ updatedAt?: string | Date;
23
+ };
24
+ type ConnectedAccount = {
25
+ id: string;
26
+ sessionId?: string;
27
+ isActive: boolean;
28
+ user: User;
29
+ };
30
+ type Permission = {
31
+ id: string;
32
+ applicationId?: string | null;
33
+ key: string;
34
+ name: string;
35
+ description: string | null;
36
+ category: string | null;
37
+ isSystem: boolean;
38
+ isActive: boolean;
39
+ createdAt?: string | Date;
40
+ updatedAt?: string | Date;
41
+ };
42
+ type RolePermission = {
43
+ permissionDefinition: Permission;
44
+ };
45
+ type Role = {
46
+ id: string;
47
+ applicationId?: string | null;
48
+ /** Present only on custom roles */
49
+ organizationId?: string | null;
50
+ /** "system" | "custom" */
51
+ type?: string;
52
+ key: string;
53
+ name: string;
54
+ description: string | null;
55
+ isSystem?: boolean;
56
+ isDefault?: boolean;
57
+ isActive: boolean;
58
+ permissions: RolePermission[];
59
+ createdAt?: string | Date;
60
+ updatedAt?: string | Date;
61
+ };
62
+ type RoleAssignment = {
63
+ id: string;
64
+ roleDefinitionId?: string | null;
65
+ customRoleId?: string | null;
66
+ roleDefinition?: Role | null;
67
+ customRole?: Role | null;
68
+ };
69
+ type OrganizationMember = {
70
+ id: string;
71
+ organizationId?: string;
72
+ userId?: string;
73
+ createdAt?: string | Date;
74
+ user: User;
75
+ roleAssignments?: RoleAssignment[];
76
+ };
77
+ type OrganizationOwnedApplication = {
78
+ id: string;
79
+ name: string;
80
+ description: string | null;
81
+ imageUrl: string | null;
82
+ type?: OAuthClientType;
83
+ organizationId: string;
84
+ isFirstParty: boolean;
85
+ ssoEnabled: boolean;
86
+ status?: "active" | "inactive" | "suspended";
87
+ createdAt?: string | Date;
88
+ updatedAt?: string | Date;
89
+ };
90
+ type Organization = {
91
+ id: string;
92
+ name: string;
93
+ description: string | null;
94
+ slug: string;
95
+ domain: string | null;
96
+ imageUrl: string | null;
97
+ faviconUrl: string | null;
98
+ status: OrganizationStatus;
99
+ members?: OrganizationMember[];
100
+ ownedApplications?: OrganizationOwnedApplication[];
101
+ applicationLinks?: Array<{
102
+ id: string;
103
+ status: OrganizationStatus;
104
+ createdAt?: string | Date;
105
+ application: OrganizationOwnedApplication;
106
+ }>;
107
+ createdAt: string | Date;
108
+ updatedAt: string | Date;
109
+ };
110
+ type SessionApplication = {
111
+ id: string;
112
+ createdAt: string | Date;
113
+ expiresAt: string | Date;
114
+ application: {
115
+ id: string;
116
+ name: string;
117
+ organizationId: string;
118
+ };
119
+ };
120
+ type UserSession = {
121
+ id: string;
122
+ userId: string;
123
+ organizationId: string | null;
124
+ deviceAgent: string | null;
125
+ deviceId: string | null;
126
+ deviceName: string | null;
127
+ ip: string | null;
128
+ authState?: unknown;
129
+ authTime?: string | Date;
130
+ expiresAt: string | Date;
131
+ createdAt: string | Date;
132
+ updatedAt: string | Date;
133
+ user?: User;
134
+ applicationSessions?: SessionApplication[];
135
+ };
136
+ type UserInput = {
137
+ name?: string | null;
138
+ email?: string;
139
+ phone?: string | null;
140
+ imageUrl?: string | null;
141
+ password?: string;
142
+ organizationId?: string | null;
143
+ status?: UserStatus;
144
+ };
145
+ type OrganizationInput = {
146
+ name: string;
147
+ description?: string | null;
148
+ slug: string;
149
+ domain?: string | null;
150
+ imageUrl?: string | null;
151
+ faviconUrl?: string | null;
152
+ };
153
+ type OrganizationUpdateInput = Partial<OrganizationInput> & {
154
+ status?: OrganizationStatus;
155
+ };
156
+ type RoleInput = {
157
+ organizationId: string;
158
+ applicationId?: string | null;
159
+ key: string;
160
+ name: string;
161
+ description?: string | null;
162
+ permissionDefinitionIds?: string[];
163
+ };
164
+ type RoleUpdateInput = {
165
+ name?: string;
166
+ description?: string | null;
167
+ isActive?: boolean;
168
+ permissionDefinitionIds?: string[];
169
+ };
170
+ type PermissionInput = {
171
+ applicationId?: string | null;
172
+ key: string;
173
+ name: string;
174
+ description?: string | null;
175
+ category?: string | null;
176
+ };
177
+ type PermissionUpdateInput = {
178
+ name?: string;
179
+ description?: string | null;
180
+ category?: string | null;
181
+ isActive?: boolean;
182
+ };
183
+ type SignInOptions = {
184
+ redirectTo?: string;
185
+ authorizationFlowId?: string;
186
+ };
187
+ type SignInInput = SignInOptions | MouseEvent<HTMLElement>;
188
+ type CallbackUrlOptions = SignInOptions & {
189
+ callbackUrl?: string;
190
+ };
191
+ type PermissionCheckOptions = {
192
+ permission: string;
193
+ organizationId?: string | null;
194
+ applicationId?: string | null;
195
+ resourceId?: string | null;
196
+ };
197
+ type LoginResponse = {
198
+ user: User;
199
+ redirectTo?: string | null;
200
+ };
201
+ type RegisterResponse = User;
202
+ type GeekappsAuthProviderProps = {
203
+ children: ReactNode;
204
+ basePath?: string;
205
+ initialUser?: User | null;
206
+ initialOrganizations?: Organization[];
207
+ initialCurrentOrgId?: string | null;
208
+ };
209
+ type RequestOptions = {
210
+ body?: unknown;
211
+ method?: "GET" | "POST" | "PATCH" | "DELETE";
212
+ };
213
+ type ClientActionOptions = {
214
+ basePath?: string;
215
+ };
216
+ declare function loginWithPassword(params: {
217
+ identifier?: string;
218
+ email?: string;
219
+ phone?: string;
220
+ password: string;
221
+ authorizationFlowId?: string;
222
+ redirectTo?: string;
223
+ }, options?: ClientActionOptions): Promise<AuthResult<LoginResponse>>;
224
+ declare function registerWithPassword(params: {
225
+ name: string;
226
+ email: string;
227
+ phone?: string;
228
+ imageUrl?: string;
229
+ address?: Record<string, unknown>;
230
+ password: string;
231
+ redirectTo?: string;
232
+ }, options?: ClientActionOptions): Promise<AuthResult<RegisterResponse>>;
233
+ declare function startGoogleLogin(params?: CallbackUrlOptions, options?: ClientActionOptions): void;
234
+ declare function requestPasswordlessLink(params: {
235
+ email: string;
236
+ authorizationFlowId?: string;
237
+ redirectTo?: string;
238
+ callbackUrl?: string;
239
+ }, options?: ClientActionOptions): Promise<AuthResult>;
240
+ type GeekappsAuthContextValue = {
241
+ basePath: string;
242
+ user: User | null;
243
+ isLoading: boolean;
244
+ accounts: ConnectedAccount[];
245
+ isLoadingAccounts: boolean;
246
+ organizations: Organization[];
247
+ currentOrg: Organization | null;
248
+ currentOrgId: string | null;
249
+ isLoadingOrganizations: boolean;
250
+ request: <T>(path: string, options?: RequestOptions) => Promise<T>;
251
+ signIn: (options?: SignInInput) => Promise<void>;
252
+ signUp: (options?: SignInInput) => Promise<void>;
253
+ addAccount: (options?: SignInInput) => Promise<void>;
254
+ signOut: () => Promise<void>;
255
+ signOutAll: () => Promise<void>;
256
+ refreshUser: () => Promise<void>;
257
+ refreshAccounts: () => Promise<void>;
258
+ switchAccount: (sessionId: string, options?: SignInOptions) => Promise<void>;
259
+ loginWithPassword: (params: {
260
+ identifier?: string;
261
+ email?: string;
262
+ phone?: string;
263
+ password: string;
264
+ authorizationFlowId?: string;
265
+ redirectTo?: string;
266
+ }) => Promise<AuthResult<LoginResponse>>;
267
+ registerWithPassword: (params: {
268
+ name: string;
269
+ email: string;
270
+ phone?: string;
271
+ imageUrl?: string;
272
+ address?: Record<string, unknown>;
273
+ password: string;
274
+ redirectTo?: string;
275
+ }) => Promise<AuthResult<RegisterResponse>>;
276
+ startGoogleLogin: (params?: CallbackUrlOptions) => void;
277
+ requestPasswordlessLink: (params: {
278
+ email: string;
279
+ authorizationFlowId?: string;
280
+ redirectTo?: string;
281
+ callbackUrl?: string;
282
+ }) => Promise<AuthResult>;
283
+ refreshOrganizations: () => Promise<void>;
284
+ setCurrentOrg: (organizationId: string | null) => Promise<void>;
285
+ createOrganization: (input: OrganizationInput) => Promise<AuthResult<Organization>>;
286
+ updateOrganization: (id: string, input: OrganizationUpdateInput) => Promise<AuthResult<Organization>>;
287
+ deleteOrganization: (id: string) => Promise<AuthResult<null>>;
288
+ hasPermission: (options: PermissionCheckOptions) => Promise<boolean>;
289
+ listUsers: (params?: {
290
+ organizationId?: string | null;
291
+ }) => Promise<User[]>;
292
+ getUser: (id: string) => Promise<User>;
293
+ createUser: (input: UserInput) => Promise<AuthResult<User>>;
294
+ updateUser: (id: string, input: UserInput) => Promise<AuthResult<User>>;
295
+ deleteUser: (id: string) => Promise<AuthResult<null>>;
296
+ listRoles: (params: {
297
+ organizationId?: string;
298
+ applicationId?: string;
299
+ type?: "system" | "custom" | "all";
300
+ }) => Promise<{
301
+ systemRoles: Role[];
302
+ customRoles: Role[];
303
+ }>;
304
+ getRole: (id: string, type: "system" | "custom") => Promise<Role>;
305
+ createRole: (input: RoleInput) => Promise<AuthResult<Role>>;
306
+ updateRole: (id: string, input: RoleUpdateInput) => Promise<AuthResult<Role>>;
307
+ deleteRole: (id: string) => Promise<AuthResult<null>>;
308
+ listPermissions: (params: {
309
+ applicationId?: string;
310
+ roleId?: string | null;
311
+ }) => Promise<Permission[]>;
312
+ createPermission: (input: PermissionInput) => Promise<AuthResult<Permission>>;
313
+ updatePermission: (id: string, input: PermissionUpdateInput) => Promise<AuthResult<Permission>>;
314
+ deletePermission: (id: string) => Promise<AuthResult<null>>;
315
+ listUserSessions: (params?: {
316
+ userId?: string;
317
+ organizationId?: string | null;
318
+ }) => Promise<UserSession[]>;
319
+ revokeUserSession: (sessionId: string) => Promise<AuthResult<null>>;
320
+ };
321
+ type UseOrgResult = Pick<GeekappsAuthContextValue, "organizations" | "currentOrg" | "currentOrgId" | "isLoadingOrganizations" | "refreshOrganizations" | "setCurrentOrg" | "createOrganization" | "updateOrganization" | "deleteOrganization" | "hasPermission">;
322
+ type UseAccountsResult = Pick<GeekappsAuthContextValue, "accounts" | "addAccount" | "isLoadingAccounts" | "refreshAccounts" | "switchAccount">;
323
+ type UseUsersResult = Pick<GeekappsAuthContextValue, "listUsers" | "getUser" | "createUser" | "updateUser" | "deleteUser">;
324
+ type UseRolesResult = Pick<GeekappsAuthContextValue, "listRoles" | "getRole" | "createRole" | "updateRole" | "deleteRole">;
325
+ type UsePermissionsResult = Pick<GeekappsAuthContextValue, "listPermissions" | "createPermission" | "updatePermission" | "deletePermission">;
326
+ type UseSessionsResult = Pick<GeekappsAuthContextValue, "listUserSessions" | "revokeUserSession">;
327
+ declare function GeekappsAuthProvider({ children, basePath, initialUser, initialOrganizations, initialCurrentOrgId, }: GeekappsAuthProviderProps): ReactElement;
328
+ declare function useAuth(): GeekappsAuthContextValue;
329
+ declare function useOrg(): UseOrgResult;
330
+ declare function useAccounts(): UseAccountsResult;
331
+ declare function useUsers(): UseUsersResult;
332
+ declare function useRoles(): UseRolesResult;
333
+ declare function usePermissions(): UsePermissionsResult;
334
+ declare function useSessions(): UseSessionsResult;
335
+ type ServiceAccount = {
336
+ id: string;
337
+ membershipId: string;
338
+ organizationId: string;
339
+ applicationId: string | null;
340
+ name: string;
341
+ status: "active" | "inactive" | "suspended";
342
+ serviceId: string | null;
343
+ roleAssignment: {
344
+ role: {
345
+ id: string;
346
+ key: string;
347
+ name: string;
348
+ };
349
+ } | null;
350
+ createdAt: string;
351
+ updatedAt: string;
352
+ };
353
+ type ServiceAccountWithSecret = ServiceAccount & {
354
+ serviceSecret: string;
355
+ };
356
+ type CreateServiceAccountInput = {
357
+ organizationId: string;
358
+ applicationId?: string;
359
+ name: string;
360
+ description?: string;
361
+ roleId?: string;
362
+ };
363
+ type UseServiceAccountResult = {
364
+ listServiceAccounts: (params: {
365
+ organizationId: string;
366
+ applicationId?: string;
367
+ }) => Promise<ServiceAccount[]>;
368
+ createServiceAccount: (input: CreateServiceAccountInput) => Promise<ServiceAccountWithSecret>;
369
+ updateServiceAccountRole: (id: string, roleId: string | null) => Promise<void>;
370
+ revokeServiceAccount: (id: string) => Promise<void>;
371
+ rotateServiceAccountSecret: (id: string) => Promise<{
372
+ serviceId: string;
373
+ serviceSecret: string;
374
+ }>;
375
+ deleteServiceAccount: (id: string) => Promise<void>;
376
+ };
377
+ declare function useServiceAccount(): UseServiceAccountResult;
378
+
379
+ export { type AuthResult, type CallbackUrlOptions, type ConnectedAccount, type CreateServiceAccountInput, type GeekappsAuthContextValue, GeekappsAuthProvider, type GeekappsAuthProviderProps, type LoginResponse, type OAuthClientType, type Organization, type OrganizationInput, type OrganizationMember, type OrganizationOwnedApplication, type OrganizationStatus, type OrganizationUpdateInput, type Permission, type PermissionCheckOptions, type PermissionInput, type PermissionUpdateInput, type RegisterResponse, type Role, type RoleAssignment, type RoleInput, type RolePermission, type RoleUpdateInput, type ServiceAccount, type ServiceAccountWithSecret, type SessionApplication, type SignInInput, type SignInOptions, type UseAccountsResult, type UseOrgResult, type UsePermissionsResult, type UseRolesResult, type UseServiceAccountResult, type UseSessionsResult, type UseUsersResult, type User, type UserInput, type UserSession, type UserStatus, loginWithPassword, registerWithPassword, requestPasswordlessLink, startGoogleLogin, useAccounts, useAuth, useOrg, usePermissions, useRoles, useServiceAccount, useSessions, useUsers };