@demokit-ai/auth 0.0.2 → 0.2.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 +140 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# @demokit-ai/auth
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
Authentication abstraction for DemoKit with Supabase Auth integration and extensible provider interface.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @demokit-ai/auth @supabase/supabase-js
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- `AuthProvider` - React context for authentication state
|
|
17
|
+
- `DemoAuthProvider` - Demo mode authentication with mock users
|
|
18
|
+
- `useAuth` - Hook for accessing auth state and methods
|
|
19
|
+
- `useUser` - Hook for accessing current user
|
|
20
|
+
- `useSession` - Hook for accessing session data
|
|
21
|
+
- Supabase Auth integration out of the box
|
|
22
|
+
- Full TypeScript support
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### Basic Setup with Supabase
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
import { AuthProvider } from '@demokit-ai/auth'
|
|
30
|
+
import { createClient } from '@supabase/supabase-js'
|
|
31
|
+
|
|
32
|
+
const supabase = createClient(
|
|
33
|
+
process.env.SUPABASE_URL!,
|
|
34
|
+
process.env.SUPABASE_ANON_KEY!
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
function App() {
|
|
38
|
+
return (
|
|
39
|
+
<AuthProvider client={supabase}>
|
|
40
|
+
<YourApp />
|
|
41
|
+
</AuthProvider>
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Demo Mode Authentication
|
|
47
|
+
|
|
48
|
+
Use mock authentication for demos:
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
import { DemoAuthProvider } from '@demokit-ai/auth'
|
|
52
|
+
|
|
53
|
+
const demoUsers = [
|
|
54
|
+
{ id: '1', email: 'demo@example.com', name: 'Demo User' },
|
|
55
|
+
{ id: '2', email: 'admin@example.com', name: 'Admin User', role: 'admin' },
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
function App() {
|
|
59
|
+
return (
|
|
60
|
+
<DemoAuthProvider users={demoUsers} defaultUser="1">
|
|
61
|
+
<YourApp />
|
|
62
|
+
</DemoAuthProvider>
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Using Auth Hooks
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
import { useAuth, useUser } from '@demokit-ai/auth'
|
|
71
|
+
|
|
72
|
+
function Profile() {
|
|
73
|
+
const { signOut, isLoading } = useAuth()
|
|
74
|
+
const user = useUser()
|
|
75
|
+
|
|
76
|
+
if (isLoading) return <div>Loading...</div>
|
|
77
|
+
if (!user) return <div>Not authenticated</div>
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<div>
|
|
81
|
+
<p>Welcome, {user.email}</p>
|
|
82
|
+
<button onClick={signOut}>Sign Out</button>
|
|
83
|
+
</div>
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Protected Routes
|
|
89
|
+
|
|
90
|
+
```tsx
|
|
91
|
+
import { useAuth } from '@demokit-ai/auth'
|
|
92
|
+
import { Navigate } from 'react-router-dom'
|
|
93
|
+
|
|
94
|
+
function ProtectedRoute({ children }) {
|
|
95
|
+
const { user, isLoading } = useAuth()
|
|
96
|
+
|
|
97
|
+
if (isLoading) return <div>Loading...</div>
|
|
98
|
+
if (!user) return <Navigate to="/login" />
|
|
99
|
+
|
|
100
|
+
return children
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## API Reference
|
|
105
|
+
|
|
106
|
+
### `AuthProvider`
|
|
107
|
+
|
|
108
|
+
Props:
|
|
109
|
+
- `client` - Supabase client instance
|
|
110
|
+
- `onAuthStateChange` - Callback for auth state changes
|
|
111
|
+
|
|
112
|
+
### `DemoAuthProvider`
|
|
113
|
+
|
|
114
|
+
Props:
|
|
115
|
+
- `users` - Array of mock user objects
|
|
116
|
+
- `defaultUser` - ID of the default logged-in user
|
|
117
|
+
- `onSignIn` - Callback when a user signs in
|
|
118
|
+
- `onSignOut` - Callback when a user signs out
|
|
119
|
+
|
|
120
|
+
### `useAuth()`
|
|
121
|
+
|
|
122
|
+
Returns:
|
|
123
|
+
- `user` - Current user object
|
|
124
|
+
- `session` - Current session
|
|
125
|
+
- `isLoading` - Loading state
|
|
126
|
+
- `signIn(credentials)` - Sign in method
|
|
127
|
+
- `signOut()` - Sign out method
|
|
128
|
+
- `signUp(credentials)` - Sign up method
|
|
129
|
+
|
|
130
|
+
### `useUser()`
|
|
131
|
+
|
|
132
|
+
Returns the current user object or null.
|
|
133
|
+
|
|
134
|
+
### `useSession()`
|
|
135
|
+
|
|
136
|
+
Returns the current session object or null.
|
|
137
|
+
|
|
138
|
+
## License
|
|
139
|
+
|
|
140
|
+
MIT
|