@econneq/auth-nextjs 1.0.2 → 1.0.5
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 +91 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# @econneq/auth-nextjs
|
|
2
|
+
|
|
3
|
+
Next.js App Router integration — middleware that gates routes and server utilities that read tokens inside Server Components, Server Actions, and Route Handlers.
|
|
4
|
+
|
|
5
|
+
## Position in the install order
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
1. auth-core ◄── prerequisite
|
|
9
|
+
2. auth-nextjs ◄── you are here (parallel with auth-react)
|
|
10
|
+
auth-react ◄── needed in your app for client-side hooks/guards
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
**Install `@econneq/auth-core` first.** This package only depends on `auth-core` directly, but in practice your app will also pull in `@econneq/auth-react` (for `<AuthProvider>` + hooks) and optionally `@econneq/auth-ui` (for the login/tenant pages).
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @econneq/auth-core @econneq/auth-nextjs
|
|
19
|
+
# and, in the app:
|
|
20
|
+
npm install @econneq/auth-react # client side
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Peer deps: `next >=14`, `react >=18`, `react-dom >=18`.
|
|
24
|
+
|
|
25
|
+
## Three entry points
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { defineAuthConfig } from '@econneq/auth-nextjs' // root
|
|
29
|
+
import { createAuthMiddleware } from '@econneq/auth-nextjs/middleware' // edge
|
|
30
|
+
import { getServerUser, serverCan, getServerAuthHeader }
|
|
31
|
+
from '@econneq/auth-nextjs/server' // RSC / actions
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Keep the imports separated — the middleware bundle runs at the edge and must not pull in Node-only code, which is why `/server` is a different subpath.
|
|
35
|
+
|
|
36
|
+
## Wire the middleware
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
// proxy.ts (or middleware.ts)
|
|
40
|
+
import { createAuthMiddleware } from '@econneq/auth-nextjs/middleware'
|
|
41
|
+
import { authConfig } from './src/auth/auth.config'
|
|
42
|
+
|
|
43
|
+
export default createAuthMiddleware(authConfig, {
|
|
44
|
+
protectedRoutes: ['/dashboard', '/app'],
|
|
45
|
+
publicRoutes: ['/auth/login', '/auth/register', '/auth/mfa'],
|
|
46
|
+
loginUrl: '/auth/login',
|
|
47
|
+
tenantSelectUrl: '/auth/select-tenant',
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
export const config = { matcher: ['/((?!_next|favicon).*)'] }
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
What it does:
|
|
54
|
+
|
|
55
|
+
- No global token → redirect to `loginUrl` (with `?next=`).
|
|
56
|
+
- Token expired → same redirect.
|
|
57
|
+
- Global token present but no tenant token (when `tenantMode`) → redirect to `tenantSelectUrl`.
|
|
58
|
+
- Authenticated → forwards the request and injects `x-auth-user-id`, `x-auth-tenant-key`, `x-auth-roles` headers for downstream Server Components.
|
|
59
|
+
|
|
60
|
+
## Read auth in Server Components / Actions
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { getServerUser, serverCan, getServerAuthHeader }
|
|
64
|
+
from '@econneq/auth-nextjs/server'
|
|
65
|
+
|
|
66
|
+
export default async function DashboardPage() {
|
|
67
|
+
const { token, userId } = await getServerUser() // redirects if missing
|
|
68
|
+
const canExport = await serverCan('reports.export')
|
|
69
|
+
return <Header name={token.fullName} canExport={canExport} />
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// In a Server Action calling your GraphQL/REST API:
|
|
73
|
+
const auth = await getServerAuthHeader()
|
|
74
|
+
fetch(API, { headers: { Authorization: auth } })
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Also available: `getGlobalToken()`, `getTenantToken()`, `getServerRoles()`.
|
|
78
|
+
|
|
79
|
+
## Build
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
npm run build # tsup → dist with /middleware and /server subpaths
|
|
83
|
+
npm run typecheck
|
|
84
|
+
npm run dev
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Notes
|
|
88
|
+
|
|
89
|
+
- Cookie names are fixed: `ea_global_token` and `ea_tenant_token`. They must be set `HttpOnly` by your auth API.
|
|
90
|
+
- The `/server` module imports `next/headers` and `next/navigation` — only call it from server contexts.
|
|
91
|
+
- For client-side hooks and guards, use `@econneq/auth-react` — this package deliberately doesn't re-export them.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@econneq/auth-nextjs",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Next.js App Router integration — middleware, server actions, SSR auth",
|
|
5
5
|
"author": "Econneq",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"README.md"
|
|
39
39
|
],
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@econneq/auth-core": "1.0.
|
|
41
|
+
"@econneq/auth-core": "1.0.4"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"next": ">=14.0.0",
|