@dudousxd/adonis-authkit-react 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/LICENSE +21 -0
- package/README.md +120 -0
- package/build/index.d.ts +9 -0
- package/build/index.js +5 -0
- package/build/src/components/authenticated.d.ts +11 -0
- package/build/src/components/authenticated.js +10 -0
- package/build/src/components/can.d.ts +10 -0
- package/build/src/components/can.js +19 -0
- package/build/src/provider.d.ts +16 -0
- package/build/src/provider.js +5 -0
- package/build/src/roles.d.ts +7 -0
- package/build/src/roles.js +32 -0
- package/build/src/types.d.ts +27 -0
- package/build/src/types.js +1 -0
- package/build/src/use_auth.d.ts +2 -0
- package/build/src/use_auth.js +29 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Davi de Carvalho
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# @dudousxd/adonis-authkit-react
|
|
2
|
+
|
|
3
|
+
Ergonomia de frontend sobre o AuthKit para apps **AdonisJS + Inertia + React**:
|
|
4
|
+
um `useAuth()` tipado, helpers de papéis e componentes de gating.
|
|
5
|
+
|
|
6
|
+
Este pacote **não** faz autenticação — ele consome o estado de auth que o host
|
|
7
|
+
AdonisJS já resolveu (via `@dudousxd/adonis-authkit-client`) e compartilhou como
|
|
8
|
+
uma shared-prop do Inertia.
|
|
9
|
+
|
|
10
|
+
## Instalação
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pnpm add @dudousxd/adonis-authkit-react
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
`react`, `react-dom` e `@inertiajs/react` são **peer dependencies** (o app os fornece).
|
|
17
|
+
|
|
18
|
+
## 1. No host AdonisJS: compartilhar a prop `authkit`
|
|
19
|
+
|
|
20
|
+
A shared-prop tem o formato `AuthSharedProps`. Use `auth.getUser()` /
|
|
21
|
+
`auth.identity` do `@dudousxd/adonis-authkit-client` para preenchê-la — tipicamente
|
|
22
|
+
num middleware ou no `config/inertia.ts` (`sharedData`):
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
// config/inertia.ts
|
|
26
|
+
import { defineConfig } from '@adonisjs/inertia'
|
|
27
|
+
|
|
28
|
+
export default defineConfig({
|
|
29
|
+
sharedData: {
|
|
30
|
+
authkit: async (ctx) => {
|
|
31
|
+
const auth = ctx.authkit // sua instância do Authenticator
|
|
32
|
+
const user = await auth.getUser() // mesmo shape de identityToUser/resolveUser
|
|
33
|
+
return {
|
|
34
|
+
user: user ?? null,
|
|
35
|
+
globalRoles: auth.identity?.globalRoles ?? [],
|
|
36
|
+
// appRoles opcional, se o host resolver papéis de app
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
O objeto `user` deve corresponder ao tipo `AuthUser`:
|
|
44
|
+
`{ id, email, name?, avatarUrl?, globalRoles, appRoles? }` — exatamente a saída de
|
|
45
|
+
`identityToUser`/`resolveUser` do client.
|
|
46
|
+
|
|
47
|
+
## 2. No frontend: `useAuth()`
|
|
48
|
+
|
|
49
|
+
```tsx
|
|
50
|
+
import { useAuth } from '@dudousxd/adonis-authkit-react'
|
|
51
|
+
|
|
52
|
+
function Header() {
|
|
53
|
+
const { user, isAuthenticated, hasGlobalRole } = useAuth()
|
|
54
|
+
|
|
55
|
+
if (!isAuthenticated) return <a href="/login">Entrar</a>
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<div>
|
|
59
|
+
Olá, {user!.name ?? user!.email}
|
|
60
|
+
{hasGlobalRole('ADMIN') && <a href="/admin">Admin</a>}
|
|
61
|
+
</div>
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`useAuth()` nunca lança quando a prop está ausente: retorna estado
|
|
67
|
+
não-autenticado (`user: null`, listas vazias).
|
|
68
|
+
|
|
69
|
+
## 3. Componentes de gating
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
import { Authenticated, Guest, Can } from '@dudousxd/adonis-authkit-react'
|
|
73
|
+
|
|
74
|
+
<Authenticated fallback={<LoginButton />}>
|
|
75
|
+
<Dashboard />
|
|
76
|
+
</Authenticated>
|
|
77
|
+
|
|
78
|
+
<Guest>
|
|
79
|
+
<MarketingBanner />
|
|
80
|
+
</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
|
+
```
|
|
97
|
+
|
|
98
|
+
## 4. `AuthProvider` (opcional)
|
|
99
|
+
|
|
100
|
+
Fora do Inertia (testes, Storybook), injete o valor manualmente. O contexto tem
|
|
101
|
+
precedência sobre as page props quando presente:
|
|
102
|
+
|
|
103
|
+
```tsx
|
|
104
|
+
import { AuthProvider } from '@dudousxd/adonis-authkit-react'
|
|
105
|
+
|
|
106
|
+
<AuthProvider value={{ user, globalRoles: user.globalRoles }}>
|
|
107
|
+
<App />
|
|
108
|
+
</AuthProvider>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Helpers puros
|
|
112
|
+
|
|
113
|
+
Para uso fora de componentes, as funções de papéis são exportadas e livres de React:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
import { hasGlobalRole, hasAnyGlobalRole, hasAllGlobalRoles, hasAppRole } from '@dudousxd/adonis-authkit-react'
|
|
117
|
+
|
|
118
|
+
hasGlobalRole(user, 'ADMIN')
|
|
119
|
+
hasAnyGlobalRole(user, ['ADMIN', 'TEACHER'])
|
|
120
|
+
```
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { useAuth } from './src/use_auth.js';
|
|
2
|
+
export { AuthProvider, AuthContext } from './src/provider.js';
|
|
3
|
+
export type { AuthProviderProps } from './src/provider.js';
|
|
4
|
+
export { Authenticated, Guest } from './src/components/authenticated.js';
|
|
5
|
+
export type { AuthenticatedProps, GuestProps } from './src/components/authenticated.js';
|
|
6
|
+
export { Can } from './src/components/can.js';
|
|
7
|
+
export type { CanProps } from './src/components/can.js';
|
|
8
|
+
export { hasGlobalRole, hasAnyGlobalRole, hasAllGlobalRoles, hasAppRole, hasAnyAppRole, hasAllAppRoles, } from './src/roles.js';
|
|
9
|
+
export type { AuthUser, AuthSharedProps, AuthState } from './src/types.js';
|
package/build/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { useAuth } from './src/use_auth.js';
|
|
2
|
+
export { AuthProvider, AuthContext } from './src/provider.js';
|
|
3
|
+
export { Authenticated, Guest } from './src/components/authenticated.js';
|
|
4
|
+
export { Can } from './src/components/can.js';
|
|
5
|
+
export { hasGlobalRole, hasAnyGlobalRole, hasAllGlobalRoles, hasAppRole, hasAnyAppRole, hasAllAppRoles, } from './src/roles.js';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
export interface AuthenticatedProps {
|
|
3
|
+
children: ReactNode;
|
|
4
|
+
fallback?: ReactNode;
|
|
5
|
+
}
|
|
6
|
+
export declare function Authenticated({ children, fallback }: AuthenticatedProps): import("react").FunctionComponentElement<import("react").FragmentProps>;
|
|
7
|
+
export interface GuestProps {
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
fallback?: ReactNode;
|
|
10
|
+
}
|
|
11
|
+
export declare function Guest({ children, fallback }: GuestProps): import("react").FunctionComponentElement<import("react").FragmentProps>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createElement, Fragment } from 'react';
|
|
2
|
+
import { useAuth } from '../use_auth.js';
|
|
3
|
+
export function Authenticated({ children, fallback = null }) {
|
|
4
|
+
const { isAuthenticated } = useAuth();
|
|
5
|
+
return createElement(Fragment, null, isAuthenticated ? children : fallback);
|
|
6
|
+
}
|
|
7
|
+
export function Guest({ children, fallback = null }) {
|
|
8
|
+
const { isAuthenticated } = useAuth();
|
|
9
|
+
return createElement(Fragment, null, isAuthenticated ? fallback : children);
|
|
10
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
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>;
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import type { AuthSharedProps } from './types.js';
|
|
3
|
+
export declare const AuthContext: import("react").Context<{
|
|
4
|
+
user: import("./types.js").AuthUser | null;
|
|
5
|
+
globalRoles: string[];
|
|
6
|
+
appRoles?: string[];
|
|
7
|
+
} | undefined>;
|
|
8
|
+
export interface AuthProviderProps {
|
|
9
|
+
value: AuthSharedProps['authkit'];
|
|
10
|
+
children: ReactNode;
|
|
11
|
+
}
|
|
12
|
+
export declare function AuthProvider({ value, children }: AuthProviderProps): import("react").FunctionComponentElement<import("react").ProviderProps<{
|
|
13
|
+
user: import("./types.js").AuthUser | null;
|
|
14
|
+
globalRoles: string[];
|
|
15
|
+
appRoles?: string[];
|
|
16
|
+
} | undefined>>;
|
|
@@ -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,32 @@
|
|
|
1
|
+
export function hasGlobalRole(user, role) {
|
|
2
|
+
if (!user)
|
|
3
|
+
return false;
|
|
4
|
+
return user.globalRoles.includes(role);
|
|
5
|
+
}
|
|
6
|
+
export function hasAnyGlobalRole(user, roles) {
|
|
7
|
+
if (!user)
|
|
8
|
+
return false;
|
|
9
|
+
return roles.some((role) => user.globalRoles.includes(role));
|
|
10
|
+
}
|
|
11
|
+
export function hasAllGlobalRoles(user, roles) {
|
|
12
|
+
if (!user)
|
|
13
|
+
return false;
|
|
14
|
+
return roles.every((role) => user.globalRoles.includes(role));
|
|
15
|
+
}
|
|
16
|
+
export function hasAppRole(user, role) {
|
|
17
|
+
if (!user || !user.appRoles)
|
|
18
|
+
return false;
|
|
19
|
+
return user.appRoles.includes(role);
|
|
20
|
+
}
|
|
21
|
+
export function hasAnyAppRole(user, roles) {
|
|
22
|
+
if (!user || !user.appRoles)
|
|
23
|
+
return false;
|
|
24
|
+
const appRoles = user.appRoles;
|
|
25
|
+
return roles.some((role) => appRoles.includes(role));
|
|
26
|
+
}
|
|
27
|
+
export function hasAllAppRoles(user, roles) {
|
|
28
|
+
if (!user || !user.appRoles)
|
|
29
|
+
return false;
|
|
30
|
+
const appRoles = user.appRoles;
|
|
31
|
+
return roles.every((role) => appRoles.includes(role));
|
|
32
|
+
}
|
|
@@ -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,29 @@
|
|
|
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 authkit = contextValue ?? pageProps?.authkit ?? UNAUTHENTICATED;
|
|
14
|
+
return useMemo(() => {
|
|
15
|
+
const user = authkit.user ?? null;
|
|
16
|
+
const globalRoles = authkit.globalRoles ?? user?.globalRoles ?? [];
|
|
17
|
+
const appRoles = authkit.appRoles ?? user?.appRoles ?? [];
|
|
18
|
+
return {
|
|
19
|
+
user,
|
|
20
|
+
isAuthenticated: user !== null,
|
|
21
|
+
globalRoles,
|
|
22
|
+
appRoles,
|
|
23
|
+
hasGlobalRole: (role) => hasGlobalRolePure(user, role),
|
|
24
|
+
hasAnyGlobalRole: (roles) => hasAnyGlobalRolePure(user, roles),
|
|
25
|
+
hasAllGlobalRoles: (roles) => hasAllGlobalRolesPure(user, roles),
|
|
26
|
+
hasAppRole: (role) => hasAppRolePure(user, role),
|
|
27
|
+
};
|
|
28
|
+
}, [authkit]);
|
|
29
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dudousxd/adonis-authkit-react",
|
|
3
|
+
"version": "0.1.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/streaming-educacao/tree/main/packages/authkit-react#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/DavideCarvalho/streaming-educacao.git",
|
|
11
|
+
"directory": "packages/authkit-react"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/DavideCarvalho/streaming-educacao/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
|
+
],
|
|
34
|
+
"exports": {
|
|
35
|
+
".": "./build/index.js",
|
|
36
|
+
"./types": "./build/index.js"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@inertiajs/react": "3.3.0",
|
|
40
|
+
"react": "19.2.6",
|
|
41
|
+
"react-dom": "19.2.6"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@dudousxd/adonis-authkit-core": "0.1.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@adonisjs/tsconfig": "2.0.0",
|
|
48
|
+
"@inertiajs/react": "3.3.0",
|
|
49
|
+
"@japa/assert": "4.2.0",
|
|
50
|
+
"@japa/runner": "5.3.0",
|
|
51
|
+
"@poppinss/ts-exec": "1.4.4",
|
|
52
|
+
"@types/node": "25.9.1",
|
|
53
|
+
"@types/react": "19.2.15",
|
|
54
|
+
"@types/react-dom": "19.2.3",
|
|
55
|
+
"react": "19.2.6",
|
|
56
|
+
"react-dom": "19.2.6",
|
|
57
|
+
"typescript": "6.0.3"
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsc",
|
|
61
|
+
"typecheck": "tsc --noEmit",
|
|
62
|
+
"test": "node --import=@poppinss/ts-exec bin/test.ts"
|
|
63
|
+
}
|
|
64
|
+
}
|