@adonis-agora/authkit-react 0.12.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.
Files changed (82) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +146 -0
  3. package/build/index.d.ts +68 -0
  4. package/build/index.js +36 -0
  5. package/build/src/authkit_provider.d.ts +13 -0
  6. package/build/src/authkit_provider.js +11 -0
  7. package/build/src/client/client.d.ts +172 -0
  8. package/build/src/client/client.js +218 -0
  9. package/build/src/client/context.d.ts +11 -0
  10. package/build/src/client/context.js +28 -0
  11. package/build/src/client/types.d.ts +425 -0
  12. package/build/src/client/types.js +1 -0
  13. package/build/src/components/authenticated.d.ts +11 -0
  14. package/build/src/components/authenticated.js +10 -0
  15. package/build/src/components/authorized_apps.d.ts +10 -0
  16. package/build/src/components/authorized_apps.js +34 -0
  17. package/build/src/components/avatar.d.ts +22 -0
  18. package/build/src/components/avatar.js +15 -0
  19. package/build/src/components/can.d.ts +10 -0
  20. package/build/src/components/can.js +19 -0
  21. package/build/src/components/key_rotation.d.ts +8 -0
  22. package/build/src/components/key_rotation.js +36 -0
  23. package/build/src/components/organization_profile.d.ts +10 -0
  24. package/build/src/components/organization_profile.js +94 -0
  25. package/build/src/components/organization_switcher.d.ts +9 -0
  26. package/build/src/components/organization_switcher.js +65 -0
  27. package/build/src/components/password_strength_meter.d.ts +11 -0
  28. package/build/src/components/password_strength_meter.js +39 -0
  29. package/build/src/components/sign_in_button.d.ts +297 -0
  30. package/build/src/components/sign_in_button.js +16 -0
  31. package/build/src/components/sign_out_button.d.ts +296 -0
  32. package/build/src/components/sign_out_button.js +16 -0
  33. package/build/src/components/user_button.d.ts +8 -0
  34. package/build/src/components/user_button.js +31 -0
  35. package/build/src/components/user_profile.d.ts +8 -0
  36. package/build/src/components/user_profile.js +35 -0
  37. package/build/src/config.d.ts +30 -0
  38. package/build/src/config.js +42 -0
  39. package/build/src/hooks/use_authorized_apps.d.ts +16 -0
  40. package/build/src/hooks/use_authorized_apps.js +15 -0
  41. package/build/src/hooks/use_org_invitations.d.ts +19 -0
  42. package/build/src/hooks/use_org_invitations.js +20 -0
  43. package/build/src/hooks/use_organization.d.ts +23 -0
  44. package/build/src/hooks/use_organization.js +8 -0
  45. package/build/src/hooks/use_organizations.d.ts +18 -0
  46. package/build/src/hooks/use_organizations.js +14 -0
  47. package/build/src/hooks/use_passkey_autofill.d.ts +8 -0
  48. package/build/src/hooks/use_passkey_autofill.js +76 -0
  49. package/build/src/hooks/use_password_strength.d.ts +11 -0
  50. package/build/src/hooks/use_password_strength.js +34 -0
  51. package/build/src/hooks/use_profile.d.ts +13 -0
  52. package/build/src/hooks/use_profile.js +28 -0
  53. package/build/src/hooks/use_resource.d.ts +11 -0
  54. package/build/src/hooks/use_resource.js +48 -0
  55. package/build/src/hooks/use_sessions.d.ts +16 -0
  56. package/build/src/hooks/use_sessions.js +15 -0
  57. package/build/src/hooks/use_sign_in.d.ts +6 -0
  58. package/build/src/hooks/use_sign_in.js +13 -0
  59. package/build/src/hooks/use_sign_out.d.ts +6 -0
  60. package/build/src/hooks/use_sign_out.js +11 -0
  61. package/build/src/hooks/use_switch_organization.d.ts +7 -0
  62. package/build/src/hooks/use_switch_organization.js +42 -0
  63. package/build/src/hooks/use_user.d.ts +6 -0
  64. package/build/src/hooks/use_user.js +5 -0
  65. package/build/src/provider.d.ts +16 -0
  66. package/build/src/provider.js +5 -0
  67. package/build/src/queries/account/index.d.ts +86 -0
  68. package/build/src/queries/account/index.js +150 -0
  69. package/build/src/queries/admin/index.d.ts +226 -0
  70. package/build/src/queries/admin/index.js +287 -0
  71. package/build/src/queries/keys.d.ts +50 -0
  72. package/build/src/queries/keys.js +34 -0
  73. package/build/src/roles.d.ts +7 -0
  74. package/build/src/roles.js +39 -0
  75. package/build/src/types.d.ts +27 -0
  76. package/build/src/types.js +1 -0
  77. package/build/src/use_auth.d.ts +2 -0
  78. package/build/src/use_auth.js +33 -0
  79. package/build/src/utils.d.ts +2 -0
  80. package/build/src/utils.js +18 -0
  81. package/package.json +68 -0
  82. package/styles.css +303 -0
@@ -0,0 +1,287 @@
1
+ import { useAuthkitClient } from '../../client/context.js';
2
+ import { authkitKeys } from '../keys.js';
3
+ export function useOverviewQueryOptions() {
4
+ const client = useAuthkitClient();
5
+ return {
6
+ queryKey: authkitKeys.admin.overview(),
7
+ queryFn: () => client.admin.overview(),
8
+ };
9
+ }
10
+ export function useUsersQueryOptions(params) {
11
+ const client = useAuthkitClient();
12
+ return {
13
+ queryKey: authkitKeys.admin.users(params),
14
+ queryFn: () => client.admin.users.list(params),
15
+ };
16
+ }
17
+ export function useUserQueryOptions(id) {
18
+ const client = useAuthkitClient();
19
+ return {
20
+ queryKey: authkitKeys.admin.user(id),
21
+ queryFn: () => client.admin.users.get(id),
22
+ enabled: !!id,
23
+ };
24
+ }
25
+ export function useUserSessionsQueryOptions(id) {
26
+ const client = useAuthkitClient();
27
+ return {
28
+ queryKey: authkitKeys.admin.userSessions(id),
29
+ queryFn: () => client.admin.users.getSessions(id),
30
+ enabled: !!id,
31
+ };
32
+ }
33
+ export function useCreateUserMutationOptions() {
34
+ const client = useAuthkitClient();
35
+ return {
36
+ mutationKey: ['authkit', 'admin', 'users', 'create'],
37
+ mutationFn: (data) => client.admin.users.create(data),
38
+ };
39
+ }
40
+ export function useUpdateUserMutationOptions(id) {
41
+ const client = useAuthkitClient();
42
+ return {
43
+ mutationKey: ['authkit', 'admin', 'users', id, 'update'],
44
+ mutationFn: (data) => client.admin.users.update(id, data),
45
+ };
46
+ }
47
+ export function useDisableUserMutationOptions(id) {
48
+ const client = useAuthkitClient();
49
+ return {
50
+ mutationKey: ['authkit', 'admin', 'users', id, 'disable'],
51
+ mutationFn: () => client.admin.users.disable(id),
52
+ };
53
+ }
54
+ export function useEnableUserMutationOptions(id) {
55
+ const client = useAuthkitClient();
56
+ return {
57
+ mutationKey: ['authkit', 'admin', 'users', id, 'enable'],
58
+ mutationFn: () => client.admin.users.enable(id),
59
+ };
60
+ }
61
+ export function useResetPasswordMutationOptions(id) {
62
+ const client = useAuthkitClient();
63
+ return {
64
+ mutationKey: ['authkit', 'admin', 'users', id, 'reset-password'],
65
+ mutationFn: () => client.admin.users.resetPassword(id),
66
+ };
67
+ }
68
+ export function useDeleteUserMutationOptions(id) {
69
+ const client = useAuthkitClient();
70
+ return {
71
+ mutationKey: ['authkit', 'admin', 'users', id, 'delete'],
72
+ mutationFn: () => client.admin.users.remove(id),
73
+ };
74
+ }
75
+ export function useRevokeUserSessionsMutationOptions(id) {
76
+ const client = useAuthkitClient();
77
+ return {
78
+ mutationKey: ['authkit', 'admin', 'users', id, 'revoke-sessions'],
79
+ mutationFn: () => client.admin.users.revokeSessions(id),
80
+ };
81
+ }
82
+ export function useSessionsQueryOptions(accountId) {
83
+ const client = useAuthkitClient();
84
+ return {
85
+ queryKey: authkitKeys.admin.sessions(accountId),
86
+ queryFn: () => client.admin.sessions.list(accountId),
87
+ };
88
+ }
89
+ export function useRevokeAllSessionsMutationOptions(accountId) {
90
+ const client = useAuthkitClient();
91
+ return {
92
+ mutationKey: ['authkit', 'admin', 'sessions', 'revoke-all', accountId ?? null],
93
+ mutationFn: () => client.admin.sessions.revokeAll(accountId),
94
+ };
95
+ }
96
+ export function useClientsQueryOptions() {
97
+ const client = useAuthkitClient();
98
+ return {
99
+ queryKey: authkitKeys.admin.clients(),
100
+ queryFn: () => client.admin.clients.list(),
101
+ };
102
+ }
103
+ export function useClientQueryOptions(id) {
104
+ const client = useAuthkitClient();
105
+ return {
106
+ queryKey: authkitKeys.admin.client(id),
107
+ queryFn: () => client.admin.clients.get(id),
108
+ enabled: !!id,
109
+ };
110
+ }
111
+ export function useCreateClientMutationOptions() {
112
+ const client = useAuthkitClient();
113
+ return {
114
+ mutationKey: ['authkit', 'admin', 'clients', 'create'],
115
+ mutationFn: (data) => client.admin.clients.create(data),
116
+ };
117
+ }
118
+ export function useUpdateClientMutationOptions(id) {
119
+ const client = useAuthkitClient();
120
+ return {
121
+ mutationKey: ['authkit', 'admin', 'clients', id, 'update'],
122
+ mutationFn: (data) => client.admin.clients.update(id, data),
123
+ };
124
+ }
125
+ export function useDeleteClientMutationOptions(id) {
126
+ const client = useAuthkitClient();
127
+ return {
128
+ mutationKey: ['authkit', 'admin', 'clients', id, 'delete'],
129
+ mutationFn: () => client.admin.clients.remove(id),
130
+ };
131
+ }
132
+ export function useRegenerateClientSecretMutationOptions(id) {
133
+ const client = useAuthkitClient();
134
+ return {
135
+ mutationKey: ['authkit', 'admin', 'clients', id, 'regenerate-secret'],
136
+ mutationFn: () => client.admin.clients.regenerateSecret(id),
137
+ };
138
+ }
139
+ export function useRolesQueryOptions() {
140
+ const client = useAuthkitClient();
141
+ return {
142
+ queryKey: authkitKeys.admin.roles(),
143
+ queryFn: () => client.admin.roles.list(),
144
+ };
145
+ }
146
+ export function useCreateRoleMutationOptions() {
147
+ const client = useAuthkitClient();
148
+ return {
149
+ mutationKey: ['authkit', 'admin', 'roles', 'create'],
150
+ mutationFn: (data) => client.admin.roles.create(data),
151
+ };
152
+ }
153
+ export function useUpdateRoleMutationOptions(name) {
154
+ const client = useAuthkitClient();
155
+ return {
156
+ mutationKey: ['authkit', 'admin', 'roles', name, 'update'],
157
+ mutationFn: (data) => client.admin.roles.update(name, data),
158
+ };
159
+ }
160
+ export function useDeleteRoleMutationOptions(name) {
161
+ const client = useAuthkitClient();
162
+ return {
163
+ mutationKey: ['authkit', 'admin', 'roles', name, 'delete'],
164
+ mutationFn: () => client.admin.roles.remove(name),
165
+ };
166
+ }
167
+ export function useOrgsQueryOptions() {
168
+ const client = useAuthkitClient();
169
+ return {
170
+ queryKey: authkitKeys.admin.orgs(),
171
+ queryFn: () => client.admin.orgs.list(),
172
+ };
173
+ }
174
+ export function useOrgQueryOptions(id) {
175
+ const client = useAuthkitClient();
176
+ return {
177
+ queryKey: authkitKeys.admin.org(id),
178
+ queryFn: () => client.admin.orgs.get(id),
179
+ enabled: !!id,
180
+ };
181
+ }
182
+ export function useCreateOrgMutationOptions() {
183
+ const client = useAuthkitClient();
184
+ return {
185
+ mutationKey: ['authkit', 'admin', 'orgs', 'create'],
186
+ mutationFn: (data) => client.admin.orgs.create(data),
187
+ };
188
+ }
189
+ export function useUpdateOrgMutationOptions(id) {
190
+ const client = useAuthkitClient();
191
+ return {
192
+ mutationKey: ['authkit', 'admin', 'orgs', id, 'update'],
193
+ mutationFn: (data) => client.admin.orgs.update(id, data),
194
+ };
195
+ }
196
+ export function useDeleteOrgMutationOptions(id) {
197
+ const client = useAuthkitClient();
198
+ return {
199
+ mutationKey: ['authkit', 'admin', 'orgs', id, 'delete'],
200
+ mutationFn: () => client.admin.orgs.remove(id),
201
+ };
202
+ }
203
+ export function useAddOrgMemberMutationOptions(orgId) {
204
+ const client = useAuthkitClient();
205
+ return {
206
+ mutationKey: ['authkit', 'admin', 'orgs', orgId, 'members', 'add'],
207
+ mutationFn: (data) => client.admin.orgs.addMember(orgId, data),
208
+ };
209
+ }
210
+ export function useRemoveOrgMemberMutationOptions(orgId, accountId) {
211
+ const client = useAuthkitClient();
212
+ return {
213
+ mutationKey: ['authkit', 'admin', 'orgs', orgId, 'members', accountId, 'remove'],
214
+ mutationFn: () => client.admin.orgs.removeMember(orgId, accountId),
215
+ };
216
+ }
217
+ export function useUpdateOrgMemberRoleMutationOptions(orgId, accountId) {
218
+ const client = useAuthkitClient();
219
+ return {
220
+ mutationKey: ['authkit', 'admin', 'orgs', orgId, 'members', accountId, 'role'],
221
+ mutationFn: (role) => client.admin.orgs.updateMemberRole(orgId, accountId, role),
222
+ };
223
+ }
224
+ export function useCreateOrgInvitationMutationOptions(orgId) {
225
+ const client = useAuthkitClient();
226
+ return {
227
+ mutationKey: ['authkit', 'admin', 'orgs', orgId, 'invitations', 'create'],
228
+ mutationFn: (data) => client.admin.orgs.createInvitation(orgId, data),
229
+ };
230
+ }
231
+ export function useRevokeOrgInvitationMutationOptions(orgId, invitationId) {
232
+ const client = useAuthkitClient();
233
+ return {
234
+ mutationKey: ['authkit', 'admin', 'orgs', orgId, 'invitations', invitationId, 'revoke'],
235
+ mutationFn: () => client.admin.orgs.revokeInvitation(orgId, invitationId),
236
+ };
237
+ }
238
+ export function useAuditQueryOptions(params) {
239
+ const client = useAuthkitClient();
240
+ return {
241
+ queryKey: authkitKeys.admin.audit(params),
242
+ queryFn: () => client.admin.audit.list(params),
243
+ };
244
+ }
245
+ export function useSettingsQueryOptions(orgId) {
246
+ const client = useAuthkitClient();
247
+ return {
248
+ queryKey: authkitKeys.admin.settings(orgId),
249
+ queryFn: () => client.admin.settings.list(orgId),
250
+ };
251
+ }
252
+ export function useSetSettingMutationOptions(orgId) {
253
+ const client = useAuthkitClient();
254
+ return {
255
+ mutationKey: ['authkit', 'admin', 'settings', 'set', orgId ?? null],
256
+ mutationFn: ({ key, value }) => client.admin.settings.set(key, value, orgId),
257
+ };
258
+ }
259
+ export function useRemoveSettingMutationOptions(orgId) {
260
+ const client = useAuthkitClient();
261
+ return {
262
+ mutationKey: ['authkit', 'admin', 'settings', 'remove', orgId ?? null],
263
+ mutationFn: (key) => client.admin.settings.remove(key, orgId),
264
+ };
265
+ }
266
+ export function useImpersonationQueryOptions(userId) {
267
+ const client = useAuthkitClient();
268
+ return {
269
+ queryKey: authkitKeys.admin.impersonation(userId),
270
+ queryFn: () => client.admin.impersonation.get(userId),
271
+ enabled: !!userId,
272
+ };
273
+ }
274
+ export function useKeysQueryOptions() {
275
+ const client = useAuthkitClient();
276
+ return {
277
+ queryKey: authkitKeys.admin.keys(),
278
+ queryFn: () => client.admin.keys.status(),
279
+ };
280
+ }
281
+ export function useRotateKeysMutationOptions() {
282
+ const client = useAuthkitClient();
283
+ return {
284
+ mutationKey: ['authkit', 'admin', 'keys', 'rotate'],
285
+ mutationFn: (input) => client.admin.keys.rotate(input),
286
+ };
287
+ }
@@ -0,0 +1,50 @@
1
+ export declare const authkitKeys: {
2
+ readonly admin: {
3
+ readonly all: readonly ["authkit", "admin"];
4
+ readonly overview: () => readonly ["authkit", "admin", "overview"];
5
+ readonly users: (params?: {
6
+ search?: string;
7
+ page?: number;
8
+ limit?: number;
9
+ }) => readonly ["authkit", "admin", "users", {
10
+ search?: string;
11
+ page?: number;
12
+ limit?: number;
13
+ }];
14
+ readonly user: (id: string) => readonly ["authkit", "admin", "users", string];
15
+ readonly userSessions: (id: string) => readonly ["authkit", "admin", "users", string, "sessions"];
16
+ readonly sessions: (accountId?: string) => readonly ["authkit", "admin", "sessions", string | null];
17
+ readonly clients: () => readonly ["authkit", "admin", "clients"];
18
+ readonly client: (id: string) => readonly ["authkit", "admin", "clients", string];
19
+ readonly roles: () => readonly ["authkit", "admin", "roles"];
20
+ readonly orgs: () => readonly ["authkit", "admin", "orgs"];
21
+ readonly org: (id: string) => readonly ["authkit", "admin", "orgs", string];
22
+ readonly audit: (params?: {
23
+ type?: string;
24
+ page?: number;
25
+ limit?: number;
26
+ subject?: string;
27
+ }) => readonly ["authkit", "admin", "audit", {
28
+ type?: string;
29
+ page?: number;
30
+ limit?: number;
31
+ subject?: string;
32
+ }];
33
+ readonly settings: (orgId?: string | null) => readonly ["authkit", "admin", "settings", string] | readonly ["authkit", "admin", "settings"];
34
+ readonly impersonation: (userId: string) => readonly ["authkit", "admin", "impersonation", string];
35
+ readonly keys: () => readonly ["authkit", "admin", "keys"];
36
+ };
37
+ readonly account: {
38
+ readonly all: readonly ["authkit", "account"];
39
+ readonly me: () => readonly ["authkit", "account", "me"];
40
+ readonly security: () => readonly ["authkit", "account", "security"];
41
+ readonly sessions: () => readonly ["authkit", "account", "sessions"];
42
+ readonly apps: () => readonly ["authkit", "account", "apps"];
43
+ readonly mfa: () => readonly ["authkit", "account", "mfa"];
44
+ readonly passkeys: () => readonly ["authkit", "account", "passkeys"];
45
+ readonly tokens: () => readonly ["authkit", "account", "tokens"];
46
+ readonly orgs: () => readonly ["authkit", "account", "orgs"];
47
+ readonly org: (id: string) => readonly ["authkit", "account", "orgs", string];
48
+ readonly orgInvitations: () => readonly ["authkit", "account", "orgs", "invitations"];
49
+ };
50
+ };
@@ -0,0 +1,34 @@
1
+ export const authkitKeys = {
2
+ admin: {
3
+ all: ['authkit', 'admin'],
4
+ overview: () => ['authkit', 'admin', 'overview'],
5
+ users: (params) => ['authkit', 'admin', 'users', params ?? {}],
6
+ user: (id) => ['authkit', 'admin', 'users', id],
7
+ userSessions: (id) => ['authkit', 'admin', 'users', id, 'sessions'],
8
+ sessions: (accountId) => ['authkit', 'admin', 'sessions', accountId ?? null],
9
+ clients: () => ['authkit', 'admin', 'clients'],
10
+ client: (id) => ['authkit', 'admin', 'clients', id],
11
+ roles: () => ['authkit', 'admin', 'roles'],
12
+ orgs: () => ['authkit', 'admin', 'orgs'],
13
+ org: (id) => ['authkit', 'admin', 'orgs', id],
14
+ audit: (params) => ['authkit', 'admin', 'audit', params ?? {}],
15
+ settings: (orgId) => orgId
16
+ ? ['authkit', 'admin', 'settings', orgId]
17
+ : ['authkit', 'admin', 'settings'],
18
+ impersonation: (userId) => ['authkit', 'admin', 'impersonation', userId],
19
+ keys: () => ['authkit', 'admin', 'keys'],
20
+ },
21
+ account: {
22
+ all: ['authkit', 'account'],
23
+ me: () => ['authkit', 'account', 'me'],
24
+ security: () => ['authkit', 'account', 'security'],
25
+ sessions: () => ['authkit', 'account', 'sessions'],
26
+ apps: () => ['authkit', 'account', 'apps'],
27
+ mfa: () => ['authkit', 'account', 'mfa'],
28
+ passkeys: () => ['authkit', 'account', 'passkeys'],
29
+ tokens: () => ['authkit', 'account', 'tokens'],
30
+ orgs: () => ['authkit', 'account', 'orgs'],
31
+ org: (id) => ['authkit', 'account', 'orgs', id],
32
+ orgInvitations: () => ['authkit', 'account', 'orgs', 'invitations'],
33
+ },
34
+ };
@@ -0,0 +1,7 @@
1
+ import type { AuthUser } from './types.js';
2
+ export declare function hasGlobalRole(user: AuthUser | null | undefined, role: string): boolean;
3
+ export declare function hasAnyGlobalRole(user: AuthUser | null | undefined, roles: string[]): boolean;
4
+ export declare function hasAllGlobalRoles(user: AuthUser | null | undefined, roles: string[]): boolean;
5
+ export declare function hasAppRole(user: AuthUser | null | undefined, role: string): boolean;
6
+ export declare function hasAnyAppRole(user: AuthUser | null | undefined, roles: string[]): boolean;
7
+ export declare function hasAllAppRoles(user: AuthUser | null | undefined, roles: string[]): boolean;
@@ -0,0 +1,39 @@
1
+ function hasRole(arr, role) {
2
+ return arr.includes(role);
3
+ }
4
+ function hasAnyRole(arr, roles) {
5
+ return roles.some((role) => arr.includes(role));
6
+ }
7
+ function hasAllRoles(arr, roles) {
8
+ return roles.every((role) => arr.includes(role));
9
+ }
10
+ export function hasGlobalRole(user, role) {
11
+ if (!user)
12
+ return false;
13
+ return hasRole(user.globalRoles, role);
14
+ }
15
+ export function hasAnyGlobalRole(user, roles) {
16
+ if (!user)
17
+ return false;
18
+ return hasAnyRole(user.globalRoles, roles);
19
+ }
20
+ export function hasAllGlobalRoles(user, roles) {
21
+ if (!user)
22
+ return false;
23
+ return hasAllRoles(user.globalRoles, roles);
24
+ }
25
+ export function hasAppRole(user, role) {
26
+ if (!user || !user.appRoles)
27
+ return false;
28
+ return hasRole(user.appRoles, role);
29
+ }
30
+ export function hasAnyAppRole(user, roles) {
31
+ if (!user || !user.appRoles)
32
+ return false;
33
+ return hasAnyRole(user.appRoles, roles);
34
+ }
35
+ export function hasAllAppRoles(user, roles) {
36
+ if (!user || !user.appRoles)
37
+ return false;
38
+ return hasAllRoles(user.appRoles, roles);
39
+ }
@@ -0,0 +1,27 @@
1
+ export interface AuthUser {
2
+ id: string;
3
+ email: string;
4
+ name?: string;
5
+ avatarUrl?: string;
6
+ globalRoles: string[];
7
+ appRoles?: string[];
8
+ [key: string]: unknown;
9
+ }
10
+ export interface AuthSharedProps {
11
+ authkit: {
12
+ user: AuthUser | null;
13
+ globalRoles: string[];
14
+ appRoles?: string[];
15
+ };
16
+ [key: string]: unknown;
17
+ }
18
+ export interface AuthState {
19
+ user: AuthUser | null;
20
+ isAuthenticated: boolean;
21
+ globalRoles: string[];
22
+ appRoles: string[];
23
+ hasGlobalRole(role: string): boolean;
24
+ hasAnyGlobalRole(roles: string[]): boolean;
25
+ hasAllGlobalRoles(roles: string[]): boolean;
26
+ hasAppRole(role: string): boolean;
27
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { AuthState } from './types.js';
2
+ export declare function useAuth(): AuthState;
@@ -0,0 +1,33 @@
1
+ import { useContext, useMemo } from 'react';
2
+ import { usePage } from '@inertiajs/react';
3
+ import { AuthContext } from './provider.js';
4
+ import { hasAllGlobalRoles as hasAllGlobalRolesPure, hasAnyGlobalRole as hasAnyGlobalRolePure, hasAppRole as hasAppRolePure, hasGlobalRole as hasGlobalRolePure, } from './roles.js';
5
+ const UNAUTHENTICATED = {
6
+ user: null,
7
+ globalRoles: [],
8
+ appRoles: undefined,
9
+ };
10
+ export function useAuth() {
11
+ const contextValue = useContext(AuthContext);
12
+ const pageProps = usePage().props;
13
+ const resolved = contextValue ?? pageProps?.authkit;
14
+ if (!resolved && typeof process !== 'undefined' && process.env.NODE_ENV !== 'production') {
15
+ console.warn('[authkit] useAuth(): no <AuthProvider> nor shared-prop `authkit` found — returning unauthenticated state.');
16
+ }
17
+ const authkit = resolved ?? UNAUTHENTICATED;
18
+ return useMemo(() => {
19
+ const user = authkit.user ?? null;
20
+ const globalRoles = authkit.globalRoles ?? user?.globalRoles ?? [];
21
+ const appRoles = authkit.appRoles ?? user?.appRoles ?? [];
22
+ return {
23
+ user,
24
+ isAuthenticated: user !== null,
25
+ globalRoles,
26
+ appRoles,
27
+ hasGlobalRole: (role) => hasGlobalRolePure(user, role),
28
+ hasAnyGlobalRole: (roles) => hasAnyGlobalRolePure(user, roles),
29
+ hasAllGlobalRoles: (roles) => hasAllGlobalRolesPure(user, roles),
30
+ hasAppRole: (role) => hasAppRolePure(user, role),
31
+ };
32
+ }, [authkit]);
33
+ }
@@ -0,0 +1,2 @@
1
+ export declare function deriveInitials(name?: string | null, email?: string | null): string;
2
+ export declare function currentUrl(): string;
@@ -0,0 +1,18 @@
1
+ export function deriveInitials(name, email) {
2
+ const source = (name ?? '').trim();
3
+ if (source) {
4
+ const parts = source.split(/\s+/).filter(Boolean);
5
+ if (parts.length === 1)
6
+ return parts[0].slice(0, 2).toUpperCase();
7
+ return (parts[0][0] + parts[parts.length - 1][0]).toUpperCase();
8
+ }
9
+ const e = (email ?? '').trim();
10
+ if (e)
11
+ return e[0].toUpperCase();
12
+ return '?';
13
+ }
14
+ export function currentUrl() {
15
+ if (typeof window === 'undefined')
16
+ return '';
17
+ return window.location.pathname + window.location.search;
18
+ }
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@adonis-agora/authkit-react",
3
+ "version": "0.12.0",
4
+ "description": "Frontend ergonomics over AuthKit for AdonisJS + Inertia + React apps: a typed useAuth() hook, role-gating hooks and gating components.",
5
+ "license": "MIT",
6
+ "author": "dudousxd",
7
+ "homepage": "https://github.com/DavideCarvalho/adonis-authkit/tree/main/packages/authkit-react#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/DavideCarvalho/adonis-authkit.git",
11
+ "directory": "packages/authkit-react"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/DavideCarvalho/adonis-authkit/issues"
15
+ },
16
+ "keywords": [
17
+ "adonisjs",
18
+ "inertia",
19
+ "react",
20
+ "authentication",
21
+ "authkit",
22
+ "rbac",
23
+ "authorization"
24
+ ],
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "type": "module",
29
+ "main": "./build/index.js",
30
+ "types": "./build/index.d.ts",
31
+ "files": [
32
+ "build",
33
+ "styles.css"
34
+ ],
35
+ "exports": {
36
+ ".": "./build/index.js",
37
+ "./types": "./build/index.js",
38
+ "./styles.css": "./styles.css"
39
+ },
40
+ "peerDependencies": {
41
+ "@inertiajs/react": "3.3.0",
42
+ "@tanstack/react-query": "5.100.14",
43
+ "react": "19.2.6",
44
+ "react-dom": "19.2.6"
45
+ },
46
+ "dependencies": {
47
+ "@adonis-agora/authkit-core": "0.7.0"
48
+ },
49
+ "devDependencies": {
50
+ "@adonisjs/tsconfig": "2.0.0",
51
+ "@inertiajs/react": "3.3.0",
52
+ "@japa/assert": "4.2.0",
53
+ "@japa/runner": "5.3.0",
54
+ "@poppinss/ts-exec": "1.4.4",
55
+ "@tanstack/react-query": "5.100.14",
56
+ "@types/node": "25.9.1",
57
+ "@types/react": "19.2.15",
58
+ "@types/react-dom": "19.2.3",
59
+ "react": "19.2.6",
60
+ "react-dom": "19.2.6",
61
+ "typescript": "6.0.3"
62
+ },
63
+ "scripts": {
64
+ "build": "tsc",
65
+ "typecheck": "tsc --noEmit",
66
+ "test": "node --import=@poppinss/ts-exec bin/test.ts"
67
+ }
68
+ }