@drmhse/authos-react 0.1.0 → 0.1.1
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 +277 -0
- package/package.json +1 -4
package/README.md
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
# @drmhse/authos-react
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@drmhse/authos-react)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
React adapter for [AuthOS](https://authos.dev) - the multi-tenant authentication platform. Provides React hooks, components, and Next.js integration.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @drmhse/authos-react
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Peer dependencies:
|
|
15
|
+
```bash
|
|
16
|
+
npm install react react-dom
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
Wrap your app with `AuthOSProvider`, then use the hooks and components throughout your app.
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
import { AuthOSProvider } from '@drmhse/authos-react';
|
|
25
|
+
|
|
26
|
+
function App() {
|
|
27
|
+
return (
|
|
28
|
+
<AuthOSProvider
|
|
29
|
+
config={{
|
|
30
|
+
baseURL: 'https://sso.example.com'
|
|
31
|
+
}}
|
|
32
|
+
>
|
|
33
|
+
<YourApp />
|
|
34
|
+
</AuthOSProvider>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Components
|
|
40
|
+
|
|
41
|
+
### SignIn
|
|
42
|
+
|
|
43
|
+
Pre-built sign-in form with email/password and OAuth provider buttons.
|
|
44
|
+
|
|
45
|
+
```tsx
|
|
46
|
+
import { SignIn } from '@drmhse/authos-react';
|
|
47
|
+
|
|
48
|
+
function LoginPage() {
|
|
49
|
+
return (
|
|
50
|
+
<SignIn
|
|
51
|
+
onSuccess={() => console.log('Logged in!')}
|
|
52
|
+
onError={(err) => console.error(err)}
|
|
53
|
+
/>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**Props:**
|
|
59
|
+
| Prop | Type | Description |
|
|
60
|
+
|------|------|-------------|
|
|
61
|
+
| `onSuccess` | `() => void` | Callback after successful login |
|
|
62
|
+
| `onError` | `(error: Error) => void` | Callback on login error |
|
|
63
|
+
|
|
64
|
+
### SignUp
|
|
65
|
+
|
|
66
|
+
Registration form for new users.
|
|
67
|
+
|
|
68
|
+
```tsx
|
|
69
|
+
import { SignUp } from '@drmhse/authos-react';
|
|
70
|
+
|
|
71
|
+
function RegisterPage() {
|
|
72
|
+
return (
|
|
73
|
+
<SignUp
|
|
74
|
+
onSuccess={() => console.log('Registered!')}
|
|
75
|
+
onError={(err) => console.error(err)}
|
|
76
|
+
/>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Props:** Same as `SignIn`
|
|
82
|
+
|
|
83
|
+
### UserButton
|
|
84
|
+
|
|
85
|
+
User menu button that shows avatar/name and dropdown with profile/signout.
|
|
86
|
+
|
|
87
|
+
```tsx
|
|
88
|
+
import { UserButton } from '@drmhse/authos-react';
|
|
89
|
+
|
|
90
|
+
function Header() {
|
|
91
|
+
return <UserButton />;
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### OrganizationSwitcher
|
|
96
|
+
|
|
97
|
+
Dropdown to switch between organizations (for multi-tenant users).
|
|
98
|
+
|
|
99
|
+
```tsx
|
|
100
|
+
import { OrganizationSwitcher } from '@drmhse/authos-react';
|
|
101
|
+
|
|
102
|
+
function Sidebar() {
|
|
103
|
+
return <OrganizationSwitcher />;
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Protect
|
|
108
|
+
|
|
109
|
+
Conditional rendering based on user permissions.
|
|
110
|
+
|
|
111
|
+
```tsx
|
|
112
|
+
import { Protect } from '@drmhse/authos-react';
|
|
113
|
+
|
|
114
|
+
function AdminPanel() {
|
|
115
|
+
return (
|
|
116
|
+
<Protect
|
|
117
|
+
permission="admin:access"
|
|
118
|
+
fallback={<p>Access denied. Admins only.</p>}
|
|
119
|
+
>
|
|
120
|
+
<AdminDashboard />
|
|
121
|
+
</Protect>
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
**Props:**
|
|
127
|
+
| Prop | Type | Description |
|
|
128
|
+
|------|------|-------------|
|
|
129
|
+
| `permission` | `string` | Required permission to access content |
|
|
130
|
+
| `fallback` | `ReactNode` | Shown when user lacks permission |
|
|
131
|
+
| `children` | `ReactNode` | Protected content |
|
|
132
|
+
|
|
133
|
+
## Hooks
|
|
134
|
+
|
|
135
|
+
### useAuthOS
|
|
136
|
+
|
|
137
|
+
Access the AuthOS client directly.
|
|
138
|
+
|
|
139
|
+
```tsx
|
|
140
|
+
import { useAuthOS } from '@drmhse/authos-react';
|
|
141
|
+
|
|
142
|
+
function Profile() {
|
|
143
|
+
const { client, isAuthenticated, isLoading } = useAuthOS();
|
|
144
|
+
|
|
145
|
+
if (isLoading) return <div>Loading...</div>;
|
|
146
|
+
if (!isAuthenticated) return <div>Please log in</div>;
|
|
147
|
+
|
|
148
|
+
const handleLogout = async () => {
|
|
149
|
+
await client.auth.logout();
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
return (
|
|
153
|
+
<div>
|
|
154
|
+
<button onClick={handleLogout}>Logout</button>
|
|
155
|
+
</div>
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### useUser
|
|
161
|
+
|
|
162
|
+
Get the current user's profile.
|
|
163
|
+
|
|
164
|
+
```tsx
|
|
165
|
+
import { useUser } from '@drmhse/authos-react';
|
|
166
|
+
|
|
167
|
+
function UserProfile() {
|
|
168
|
+
const { user, isLoading } = useUser();
|
|
169
|
+
|
|
170
|
+
if (isLoading) return <div>Loading...</div>;
|
|
171
|
+
return <div>Welcome, {user?.email}</div>;
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### useOrganization
|
|
176
|
+
|
|
177
|
+
Get the current organization and list of organizations.
|
|
178
|
+
|
|
179
|
+
```tsx
|
|
180
|
+
import { useOrganization } from '@drmhse/authos-react';
|
|
181
|
+
|
|
182
|
+
function OrgInfo() {
|
|
183
|
+
const { currentOrganization, organizations } = useOrganization();
|
|
184
|
+
|
|
185
|
+
return (
|
|
186
|
+
<div>
|
|
187
|
+
<h3>{currentOrganization?.name}</h3>
|
|
188
|
+
<ul>
|
|
189
|
+
{organizations.map((org) => (
|
|
190
|
+
<li key={org.id}>{org.name}</li>
|
|
191
|
+
))}
|
|
192
|
+
</ul>
|
|
193
|
+
</div>
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### usePermission, useAnyPermission, useAllPermissions
|
|
199
|
+
|
|
200
|
+
Check user permissions.
|
|
201
|
+
|
|
202
|
+
```tsx
|
|
203
|
+
import { usePermission } from '@drmhse/authos-react';
|
|
204
|
+
|
|
205
|
+
function AdminButton() {
|
|
206
|
+
const canAccessAdmin = usePermission('admin:access');
|
|
207
|
+
|
|
208
|
+
if (!canAccessAdmin) return null;
|
|
209
|
+
|
|
210
|
+
return <button>Admin Panel</button>;
|
|
211
|
+
}
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Next.js Integration
|
|
215
|
+
|
|
216
|
+
For Next.js App Router, use the middleware and server utilities:
|
|
217
|
+
|
|
218
|
+
```tsx
|
|
219
|
+
// middleware.ts
|
|
220
|
+
import { authMiddleware } from '@drmhse/authos-react/nextjs';
|
|
221
|
+
|
|
222
|
+
export default authMiddleware();
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
```tsx
|
|
226
|
+
// app/dashboard/page.tsx
|
|
227
|
+
import { currentUser } from '@drmhse/authos-react/nextjs';
|
|
228
|
+
|
|
229
|
+
export default async function Dashboard() {
|
|
230
|
+
const user = await currentUser();
|
|
231
|
+
|
|
232
|
+
if (!user) {
|
|
233
|
+
return <div>Please log in</div>;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return <div>Welcome, {user.email}</div>;
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## API Reference
|
|
241
|
+
|
|
242
|
+
### AuthOSProvider Props
|
|
243
|
+
|
|
244
|
+
| Prop | Type | Default | Description |
|
|
245
|
+
|------|------|---------|-------------|
|
|
246
|
+
| `config.baseURL` | `string` | **required** | AuthOS API URL |
|
|
247
|
+
| `config.storage` | `TokenStorage` | `localStorage` | Custom token storage |
|
|
248
|
+
| `config.autoRefresh` | `boolean` | `true` | Auto-refresh expired tokens |
|
|
249
|
+
| `config.onAuthStateChange` | `(user) => void` | - | Callback when auth state changes |
|
|
250
|
+
|
|
251
|
+
### SsoClient
|
|
252
|
+
|
|
253
|
+
The underlying client from `@drmhse/sso-sdk`. See [SDK docs](https://www.npmjs.com/package/@drmhse/sso-sdk) for full API.
|
|
254
|
+
|
|
255
|
+
```tsx
|
|
256
|
+
const { client } = useAuthOS();
|
|
257
|
+
|
|
258
|
+
// Authentication
|
|
259
|
+
await client.auth.login({ email, password });
|
|
260
|
+
await client.auth.logout();
|
|
261
|
+
await client.auth.register({ email, password, org_slug });
|
|
262
|
+
|
|
263
|
+
// User
|
|
264
|
+
await client.user.getProfile();
|
|
265
|
+
await client.user.updateProfile({ name });
|
|
266
|
+
await client.user.changePassword({ old, new });
|
|
267
|
+
|
|
268
|
+
// Organizations
|
|
269
|
+
await client.organizations.list();
|
|
270
|
+
await client.organizations.get(slug);
|
|
271
|
+
|
|
272
|
+
// And more...
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## License
|
|
276
|
+
|
|
277
|
+
MIT © DRM HSE
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drmhse/authos-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "React and Next.js adapter for AuthOS authentication",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -35,7 +35,6 @@
|
|
|
35
35
|
"dev": "tsup --watch",
|
|
36
36
|
"typecheck": "tsc --noEmit",
|
|
37
37
|
"lint": "eslint src --ext .ts,.tsx",
|
|
38
|
-
"test:ct": "playwright test -c playwright-ct.config.ts --reporter=list",
|
|
39
38
|
"prepublishOnly": "npm run build"
|
|
40
39
|
},
|
|
41
40
|
"keywords": [
|
|
@@ -53,8 +52,6 @@
|
|
|
53
52
|
"react": ">=18.0.0"
|
|
54
53
|
},
|
|
55
54
|
"devDependencies": {
|
|
56
|
-
"@playwright/experimental-ct-react": "^1.48.0",
|
|
57
|
-
"@playwright/test": "^1.48.0",
|
|
58
55
|
"@types/react": "^19.0.0",
|
|
59
56
|
"next": "^15.1.0",
|
|
60
57
|
"react": "^19.0.0",
|