@adonis-agora/authkit-react 0.13.1 → 0.14.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 +7 -19
- package/build/index.d.ts +1 -3
- package/build/index.js +1 -2
- package/build/src/authkit_provider.d.ts +0 -1
- package/build/src/provider.d.ts +0 -2
- package/build/src/roles.d.ts +0 -3
- package/build/src/roles.js +0 -15
- package/build/src/types.d.ts +0 -4
- package/build/src/use_auth.js +1 -5
- package/package.json +1 -1
- package/build/src/components/can.d.ts +0 -10
- package/build/src/components/can.js +0 -19
package/README.md
CHANGED
|
@@ -33,7 +33,6 @@ export default defineConfig({
|
|
|
33
33
|
return {
|
|
34
34
|
user: user ?? null,
|
|
35
35
|
globalRoles: auth.identity?.globalRoles ?? [],
|
|
36
|
-
// appRoles opcional, se o host resolver papéis de app
|
|
37
36
|
}
|
|
38
37
|
},
|
|
39
38
|
},
|
|
@@ -41,7 +40,7 @@ export default defineConfig({
|
|
|
41
40
|
```
|
|
42
41
|
|
|
43
42
|
O objeto `user` deve corresponder ao tipo `AuthUser`:
|
|
44
|
-
`{ id, email, name?, avatarUrl?, globalRoles
|
|
43
|
+
`{ id, email, name?, avatarUrl?, globalRoles }` — exatamente a saída de
|
|
45
44
|
`identityToUser`/`resolveUser` do client.
|
|
46
45
|
|
|
47
46
|
## 2. No frontend: `useAuth()`
|
|
@@ -69,7 +68,7 @@ não-autenticado (`user: null`, listas vazias).
|
|
|
69
68
|
## 3. Componentes de gating
|
|
70
69
|
|
|
71
70
|
```tsx
|
|
72
|
-
import { Authenticated, Guest
|
|
71
|
+
import { Authenticated, Guest } from '@adonis-agora/authkit-react'
|
|
73
72
|
|
|
74
73
|
<Authenticated fallback={<LoginButton />}>
|
|
75
74
|
<Dashboard />
|
|
@@ -78,23 +77,12 @@ import { Authenticated, Guest, Can } from '@adonis-agora/authkit-react'
|
|
|
78
77
|
<Guest>
|
|
79
78
|
<MarketingBanner />
|
|
80
79
|
</Guest>
|
|
81
|
-
|
|
82
|
-
{/* papel global único */}
|
|
83
|
-
<Can role="ADMIN">
|
|
84
|
-
<AdminPanel />
|
|
85
|
-
</Can>
|
|
86
|
-
|
|
87
|
-
{/* exige todos os papéis */}
|
|
88
|
-
<Can roles={['ADMIN', 'BILLING']} mode="all" fallback={<NoAccess />}>
|
|
89
|
-
<BillingSettings />
|
|
90
|
-
</Can>
|
|
91
|
-
|
|
92
|
-
{/* papel específico do app */}
|
|
93
|
-
<Can role="EDITOR" appRole>
|
|
94
|
-
<EditButton />
|
|
95
|
-
</Can>
|
|
96
80
|
```
|
|
97
81
|
|
|
82
|
+
Gating por papel/permissão de app (`<Can>`) migrou para
|
|
83
|
+
`@adonis-agora/authz-react`, que consulta o serviço Authz em vez de depender
|
|
84
|
+
de papéis de app resolvidos no host — veja a doc desse pacote.
|
|
85
|
+
|
|
98
86
|
## 4. `AuthProvider` (opcional)
|
|
99
87
|
|
|
100
88
|
Fora do Inertia (testes, Storybook), injete o valor manualmente. O contexto tem
|
|
@@ -139,7 +127,7 @@ redirecionam para o IdP. Temável via CSS vars `--authkit-*`. Veja a
|
|
|
139
127
|
Para uso fora de componentes, as funções de papéis são exportadas e livres de React:
|
|
140
128
|
|
|
141
129
|
```ts
|
|
142
|
-
import { hasGlobalRole, hasAnyGlobalRole, hasAllGlobalRoles
|
|
130
|
+
import { hasGlobalRole, hasAnyGlobalRole, hasAllGlobalRoles } from '@adonis-agora/authkit-react'
|
|
143
131
|
|
|
144
132
|
hasGlobalRole(user, 'ADMIN')
|
|
145
133
|
hasAnyGlobalRole(user, ['ADMIN', 'TEACHER'])
|
package/build/index.d.ts
CHANGED
|
@@ -34,8 +34,6 @@ export type { PasswordStrengthScore, PasswordStrengthResult, PasswordScorer, Use
|
|
|
34
34
|
export { deriveInitials, currentUrl } from "./src/utils.js";
|
|
35
35
|
export { Authenticated, Guest } from "./src/components/authenticated.js";
|
|
36
36
|
export type { AuthenticatedProps, GuestProps, } from "./src/components/authenticated.js";
|
|
37
|
-
export { Can } from "./src/components/can.js";
|
|
38
|
-
export type { CanProps } from "./src/components/can.js";
|
|
39
37
|
export { CanPermission } from "./src/components/can_permission.js";
|
|
40
38
|
export type { CanPermissionProps } from "./src/components/can_permission.js";
|
|
41
39
|
export { useCan, checkCan, invalidateCanCache } from "./src/hooks/use_can.js";
|
|
@@ -60,7 +58,7 @@ export { OrganizationProfile } from "./src/components/organization_profile.js";
|
|
|
60
58
|
export type { OrganizationProfileProps } from "./src/components/organization_profile.js";
|
|
61
59
|
export { KeyRotation } from "./src/components/key_rotation.js";
|
|
62
60
|
export type { KeyRotationProps } from "./src/components/key_rotation.js";
|
|
63
|
-
export { hasGlobalRole, hasAnyGlobalRole, hasAllGlobalRoles,
|
|
61
|
+
export { hasGlobalRole, hasAnyGlobalRole, hasAllGlobalRoles, } from "./src/roles.js";
|
|
64
62
|
export type { AuthUser, AuthSharedProps, AuthState } from "./src/types.js";
|
|
65
63
|
export { createAuthkitClient, AuthkitClientError, } from "./src/client/client.js";
|
|
66
64
|
export type { AuthkitClient, AuthkitClientOptions, } from "./src/client/client.js";
|
package/build/index.js
CHANGED
|
@@ -17,7 +17,6 @@ export { usePasskeyAutofill } from "./src/hooks/use_passkey_autofill.js";
|
|
|
17
17
|
export { usePasswordStrength, heuristicScorer, } from "./src/hooks/use_password_strength.js";
|
|
18
18
|
export { deriveInitials, currentUrl } from "./src/utils.js";
|
|
19
19
|
export { Authenticated, Guest } from "./src/components/authenticated.js";
|
|
20
|
-
export { Can } from "./src/components/can.js";
|
|
21
20
|
export { CanPermission } from "./src/components/can_permission.js";
|
|
22
21
|
export { useCan, checkCan, invalidateCanCache } from "./src/hooks/use_can.js";
|
|
23
22
|
export { SignInButton } from "./src/components/sign_in_button.js";
|
|
@@ -30,7 +29,7 @@ export { PasswordStrengthMeter } from "./src/components/password_strength_meter.
|
|
|
30
29
|
export { OrganizationSwitcher } from "./src/components/organization_switcher.js";
|
|
31
30
|
export { OrganizationProfile } from "./src/components/organization_profile.js";
|
|
32
31
|
export { KeyRotation } from "./src/components/key_rotation.js";
|
|
33
|
-
export { hasGlobalRole, hasAnyGlobalRole, hasAllGlobalRoles,
|
|
32
|
+
export { hasGlobalRole, hasAnyGlobalRole, hasAllGlobalRoles, } from "./src/roles.js";
|
|
34
33
|
export { createAuthkitClient, AuthkitClientError, } from "./src/client/client.js";
|
|
35
34
|
export { AuthkitClientProvider, useAuthkitClient, createAuthkitQueryClient, } from "./src/client/context.js";
|
|
36
35
|
export { authkitKeys } from "./src/queries/keys.js";
|
|
@@ -9,5 +9,4 @@ export interface AuthkitProviderProps {
|
|
|
9
9
|
export declare function AuthkitProvider({ config, value, children }: AuthkitProviderProps): import("react").FunctionComponentElement<import("react").ProviderProps<{
|
|
10
10
|
user: import("./types.js").AuthUser | null;
|
|
11
11
|
globalRoles: string[];
|
|
12
|
-
appRoles?: string[];
|
|
13
12
|
} | undefined>> | import("react").FunctionComponentElement<import("react").ProviderProps<import("./config.js").ResolvedAuthkitConfig>>;
|
package/build/src/provider.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ import type { AuthSharedProps } from './types.js';
|
|
|
3
3
|
export declare const AuthContext: import("react").Context<{
|
|
4
4
|
user: import("./types.js").AuthUser | null;
|
|
5
5
|
globalRoles: string[];
|
|
6
|
-
appRoles?: string[];
|
|
7
6
|
} | undefined>;
|
|
8
7
|
export interface AuthProviderProps {
|
|
9
8
|
value: AuthSharedProps['authkit'];
|
|
@@ -12,5 +11,4 @@ export interface AuthProviderProps {
|
|
|
12
11
|
export declare function AuthProvider({ value, children }: AuthProviderProps): import("react").FunctionComponentElement<import("react").ProviderProps<{
|
|
13
12
|
user: import("./types.js").AuthUser | null;
|
|
14
13
|
globalRoles: string[];
|
|
15
|
-
appRoles?: string[];
|
|
16
14
|
} | undefined>>;
|
package/build/src/roles.d.ts
CHANGED
|
@@ -2,6 +2,3 @@ import type { AuthUser } from './types.js';
|
|
|
2
2
|
export declare function hasGlobalRole(user: AuthUser | null | undefined, role: string): boolean;
|
|
3
3
|
export declare function hasAnyGlobalRole(user: AuthUser | null | undefined, roles: string[]): boolean;
|
|
4
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;
|
package/build/src/roles.js
CHANGED
|
@@ -22,18 +22,3 @@ export function hasAllGlobalRoles(user, roles) {
|
|
|
22
22
|
return false;
|
|
23
23
|
return hasAllRoles(user.globalRoles, roles);
|
|
24
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
|
-
}
|
package/build/src/types.d.ts
CHANGED
|
@@ -4,14 +4,12 @@ export interface AuthUser {
|
|
|
4
4
|
name?: string;
|
|
5
5
|
avatarUrl?: string;
|
|
6
6
|
globalRoles: string[];
|
|
7
|
-
appRoles?: string[];
|
|
8
7
|
[key: string]: unknown;
|
|
9
8
|
}
|
|
10
9
|
export interface AuthSharedProps {
|
|
11
10
|
authkit: {
|
|
12
11
|
user: AuthUser | null;
|
|
13
12
|
globalRoles: string[];
|
|
14
|
-
appRoles?: string[];
|
|
15
13
|
};
|
|
16
14
|
[key: string]: unknown;
|
|
17
15
|
}
|
|
@@ -19,9 +17,7 @@ export interface AuthState {
|
|
|
19
17
|
user: AuthUser | null;
|
|
20
18
|
isAuthenticated: boolean;
|
|
21
19
|
globalRoles: string[];
|
|
22
|
-
appRoles: string[];
|
|
23
20
|
hasGlobalRole(role: string): boolean;
|
|
24
21
|
hasAnyGlobalRole(roles: string[]): boolean;
|
|
25
22
|
hasAllGlobalRoles(roles: string[]): boolean;
|
|
26
|
-
hasAppRole(role: string): boolean;
|
|
27
23
|
}
|
package/build/src/use_auth.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { useContext, useMemo } from 'react';
|
|
2
2
|
import { usePage } from '@inertiajs/react';
|
|
3
3
|
import { AuthContext } from './provider.js';
|
|
4
|
-
import { hasAllGlobalRoles as hasAllGlobalRolesPure, hasAnyGlobalRole as hasAnyGlobalRolePure,
|
|
4
|
+
import { hasAllGlobalRoles as hasAllGlobalRolesPure, hasAnyGlobalRole as hasAnyGlobalRolePure, hasGlobalRole as hasGlobalRolePure, } from './roles.js';
|
|
5
5
|
const UNAUTHENTICATED = {
|
|
6
6
|
user: null,
|
|
7
7
|
globalRoles: [],
|
|
8
|
-
appRoles: undefined,
|
|
9
8
|
};
|
|
10
9
|
export function useAuth() {
|
|
11
10
|
const contextValue = useContext(AuthContext);
|
|
@@ -18,16 +17,13 @@ export function useAuth() {
|
|
|
18
17
|
return useMemo(() => {
|
|
19
18
|
const user = authkit.user ?? null;
|
|
20
19
|
const globalRoles = authkit.globalRoles ?? user?.globalRoles ?? [];
|
|
21
|
-
const appRoles = authkit.appRoles ?? user?.appRoles ?? [];
|
|
22
20
|
return {
|
|
23
21
|
user,
|
|
24
22
|
isAuthenticated: user !== null,
|
|
25
23
|
globalRoles,
|
|
26
|
-
appRoles,
|
|
27
24
|
hasGlobalRole: (role) => hasGlobalRolePure(user, role),
|
|
28
25
|
hasAnyGlobalRole: (roles) => hasAnyGlobalRolePure(user, roles),
|
|
29
26
|
hasAllGlobalRoles: (roles) => hasAllGlobalRolesPure(user, roles),
|
|
30
|
-
hasAppRole: (role) => hasAppRolePure(user, role),
|
|
31
27
|
};
|
|
32
28
|
}, [authkit]);
|
|
33
29
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonis-agora/authkit-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "Frontend ergonomics over AuthKit for AdonisJS + Inertia + React apps: a typed useAuth() hook, role-gating hooks and gating components.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "dudousxd",
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { type ReactNode } from 'react';
|
|
2
|
-
export interface CanProps {
|
|
3
|
-
children: ReactNode;
|
|
4
|
-
role?: string;
|
|
5
|
-
roles?: string[];
|
|
6
|
-
mode?: 'any' | 'all';
|
|
7
|
-
appRole?: boolean;
|
|
8
|
-
fallback?: ReactNode;
|
|
9
|
-
}
|
|
10
|
-
export declare function Can({ children, role, roles, mode, appRole, fallback, }: CanProps): import("react").FunctionComponentElement<import("react").FragmentProps>;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { createElement, Fragment } from 'react';
|
|
2
|
-
import { useAuth } from '../use_auth.js';
|
|
3
|
-
import { hasAllAppRoles, hasAllGlobalRoles, hasAnyAppRole, hasAnyGlobalRole, hasAppRole, hasGlobalRole, } from '../roles.js';
|
|
4
|
-
export function Can({ children, role, roles, mode = 'any', appRole = false, fallback = null, }) {
|
|
5
|
-
const { user } = useAuth();
|
|
6
|
-
let allowed = false;
|
|
7
|
-
if (role !== undefined) {
|
|
8
|
-
allowed = appRole ? hasAppRole(user, role) : hasGlobalRole(user, role);
|
|
9
|
-
}
|
|
10
|
-
else if (roles !== undefined) {
|
|
11
|
-
if (appRole) {
|
|
12
|
-
allowed = mode === 'all' ? hasAllAppRoles(user, roles) : hasAnyAppRole(user, roles);
|
|
13
|
-
}
|
|
14
|
-
else {
|
|
15
|
-
allowed = mode === 'all' ? hasAllGlobalRoles(user, roles) : hasAnyGlobalRole(user, roles);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
return createElement(Fragment, null, allowed ? children : fallback);
|
|
19
|
-
}
|