@jengolabs/auth-react 0.1.0 → 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 +119 -0
- package/package.json +3 -2
package/README.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# @jengolabs/auth-react
|
|
2
|
+
|
|
3
|
+
React SDK for [Jengo Auth](https://github.com/jengolabs/jen-auth) — hooks, components, and Next.js middleware for client-side authentication.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @jengolabs/auth-react
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Setup
|
|
12
|
+
|
|
13
|
+
Create an auth client once and reuse it across your app:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
// lib/auth.ts
|
|
17
|
+
import { createJengoAuthClient } from '@jengolabs/auth-react';
|
|
18
|
+
|
|
19
|
+
export const authClient = createJengoAuthClient({
|
|
20
|
+
authUrl: process.env.NEXT_PUBLIC_AUTH_URL, // e.g. https://auth.yourdomain.com
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Hooks
|
|
25
|
+
|
|
26
|
+
### `useSession`
|
|
27
|
+
|
|
28
|
+
Returns the current session state for the authenticated user.
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
import { useSession } from '@jengolabs/auth-react';
|
|
32
|
+
|
|
33
|
+
export function ProfileButton() {
|
|
34
|
+
const { data: session, isPending } = useSession(process.env.NEXT_PUBLIC_AUTH_URL);
|
|
35
|
+
|
|
36
|
+
if (isPending) return <span>Loading...</span>;
|
|
37
|
+
if (!session) return <a href="/sign-in">Sign in</a>;
|
|
38
|
+
|
|
39
|
+
return <span>Hello, {session.user.name}</span>;
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### `useAuthFetch`
|
|
44
|
+
|
|
45
|
+
Authenticated `fetch` wrapper that forwards session credentials automatically.
|
|
46
|
+
|
|
47
|
+
```tsx
|
|
48
|
+
import { useAuthFetch } from '@jengolabs/auth-react';
|
|
49
|
+
|
|
50
|
+
export function DataList() {
|
|
51
|
+
const authFetch = useAuthFetch(authClient);
|
|
52
|
+
|
|
53
|
+
async function load() {
|
|
54
|
+
const res = await authFetch('https://api.yourdomain.com/data');
|
|
55
|
+
return res.json();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ...
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Components
|
|
63
|
+
|
|
64
|
+
### `ProtectedRoute`
|
|
65
|
+
|
|
66
|
+
Wraps a route and redirects unauthenticated users to the sign-in page.
|
|
67
|
+
|
|
68
|
+
```tsx
|
|
69
|
+
import { ProtectedRoute } from '@jengolabs/auth-react';
|
|
70
|
+
|
|
71
|
+
export default function DashboardPage() {
|
|
72
|
+
return (
|
|
73
|
+
<ProtectedRoute authUrl={process.env.NEXT_PUBLIC_AUTH_URL}>
|
|
74
|
+
<Dashboard />
|
|
75
|
+
</ProtectedRoute>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Next.js middleware
|
|
81
|
+
|
|
82
|
+
Protect routes at the edge — unauthenticated requests are redirected to the sign-in page before the page renders.
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
// middleware.ts
|
|
86
|
+
import { createNextAuthMiddleware } from '@jengolabs/auth-react/middleware/nextjs';
|
|
87
|
+
|
|
88
|
+
export const middleware = createNextAuthMiddleware({
|
|
89
|
+
authServerUrl: process.env.AUTH_SERVER_URL,
|
|
90
|
+
publicPaths: ['/sign-in', '/api/public'],
|
|
91
|
+
signInPath: '/sign-in', // default
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
export const config = {
|
|
95
|
+
matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
|
|
96
|
+
};
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Auth client methods
|
|
100
|
+
|
|
101
|
+
The `authClient` returned by `createJengoAuthClient` exposes the full [Better Auth](https://www.better-auth.com) client API:
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
// Sign in
|
|
105
|
+
await authClient.signIn.email({ email, password });
|
|
106
|
+
|
|
107
|
+
// Sign out
|
|
108
|
+
await authClient.signOut();
|
|
109
|
+
|
|
110
|
+
// Get current session
|
|
111
|
+
const { data } = await authClient.getSession();
|
|
112
|
+
|
|
113
|
+
// OAuth
|
|
114
|
+
await authClient.signIn.social({ provider: 'google' });
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## License
|
|
118
|
+
|
|
119
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jengolabs/auth-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "React SDK for Jengo Auth — hooks, components, and Next.js middleware",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
|
-
"dist/**/*"
|
|
8
|
+
"dist/**/*",
|
|
9
|
+
"README.md"
|
|
9
10
|
],
|
|
10
11
|
"exports": {
|
|
11
12
|
".": {
|