@devcoffee/nuxt-core 1.1.0 β 1.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/CHANGELOG.md +82 -0
- package/GUIDELINE.md +351 -0
- package/README.md +0 -2
- package/dist/module.json +1 -1
- package/dist/module.mjs +1 -1
- package/dist/runtime/server/adapters/http.d.ts +1 -1
- package/dist/runtime/server/core/helpers.js +20 -25
- package/dist/runtime/server/core/mutex.js +1 -1
- package/dist/runtime/server/core/nuxtAuthtsHandler.js +1 -1
- package/dist/runtime/server/plugins/authts.d.ts +1 -1
- package/dist/runtime/server/plugins/authts.js +4 -4
- package/package.json +23 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## v1.1.1
|
|
4
|
+
|
|
5
|
+
[compare changes](https://github.com/coolkg1412/devcoffee-nuxt-core/compare/v1.0...v1.1.1)
|
|
6
|
+
|
|
7
|
+
### π
Refactors
|
|
8
|
+
|
|
9
|
+
- Sort storage adapter imports in helpers.ts; remove unused moduleText in forward handler test ([20f3496](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/20f3496))
|
|
10
|
+
|
|
11
|
+
### π‘ Chore
|
|
12
|
+
|
|
13
|
+
- Complete v1.0 milestone β archive roadmap, requirements, retrospective ([2cb5067](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/2cb5067))
|
|
14
|
+
- Delete REQUIREMENTS.md β archived to milestones/v1.0-REQUIREMENTS.md ([48ce751](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/48ce751))
|
|
15
|
+
- Archive phase directories from v1.0 milestone ([1f3ced8](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/1f3ced8))
|
|
16
|
+
|
|
17
|
+
### β€οΈ Contributors
|
|
18
|
+
|
|
19
|
+
- Hieu Nguyen <hieu.nguyen@devcoffee.tech>
|
|
20
|
+
|
|
21
|
+
## v1.1.0
|
|
22
|
+
|
|
23
|
+
[compare changes](https://github.com/coolkg1412/devcoffee-nuxt-core/compare/v1.0.2...v1.1.0)
|
|
24
|
+
|
|
25
|
+
### π Enhancements
|
|
26
|
+
|
|
27
|
+
- Add support for ignored authentication path regex patterns ([#5](https://github.com/coolkg1412/devcoffee-nuxt-core/pull/5))
|
|
28
|
+
- **04:** Adapter foundation β http/utils/storage/oidc barrel adapters (ADPT-01β04) ([3c44a78](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/3c44a78))
|
|
29
|
+
- **09:** Add E2E test coverage using Playwright ([#14](https://github.com/coolkg1412/devcoffee-nuxt-core/pull/14))
|
|
30
|
+
- **docs:** Phase 10 β README.md and GUIDELINE.md for consumers and contributors ([#15](https://github.com/coolkg1412/devcoffee-nuxt-core/pull/15))
|
|
31
|
+
- **11:** Distributed token refresh mutex β atomic Redis NX lock for multi-instance deployments ([f7e2d72](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/f7e2d72))
|
|
32
|
+
|
|
33
|
+
### π©Ή Fixes
|
|
34
|
+
|
|
35
|
+
- TypeScript ([#4](https://github.com/coolkg1412/devcoffee-nuxt-core/pull/4))
|
|
36
|
+
- Update index page test to check for non-null response ([aba39bf](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/aba39bf))
|
|
37
|
+
- **lint:** Auto-fix prettier formatting and replace dynamic delete in omit() ([89d987a](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/89d987a))
|
|
38
|
+
- **test:** Update SEC-03 source-text assertion to match prettier-formatted multiline call ([55f92d7](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/55f92d7))
|
|
39
|
+
- **types:** Resolve TypeScript strict-mode errors across app layer and server core ([b089812](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/b089812))
|
|
40
|
+
- **types:** Cast cookieOpts input before omit to resolve TS key narrowing error ([93dbb50](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/93dbb50))
|
|
41
|
+
- **types:** Include global.d.ts in Nitro server tsconfig to resolve DeepPartial on server layer ([24700e2](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/24700e2))
|
|
42
|
+
|
|
43
|
+
### π
Refactors
|
|
44
|
+
|
|
45
|
+
- Flatten utils paths (utils/utils.ts β utils.ts) ([45d8997](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/45d8997))
|
|
46
|
+
- **test:** Migrate test imports to @/ alias and fix tsconfig/vitest paths ([1f73e73](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/1f73e73))
|
|
47
|
+
- **utils:** Consolidate utility exports and fix module import paths ([d9e2fdc](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/d9e2fdc))
|
|
48
|
+
- **module:** Remove redundant typescript tsConfig includes covered by prepare:types hook ([9ced2c5](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/9ced2c5))
|
|
49
|
+
|
|
50
|
+
### π Documentation
|
|
51
|
+
|
|
52
|
+
- **10:** Research phase for README.md and GUIDELINE.md documentation ([31d5ab0](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/31d5ab0))
|
|
53
|
+
- Resolve debug eslint-ts-type-errors ([0653761](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/0653761))
|
|
54
|
+
- Update debug knowledge base with eslint-ts-type-errors ([2dc0cc1](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/2dc0cc1))
|
|
55
|
+
|
|
56
|
+
### π‘ Chore
|
|
57
|
+
|
|
58
|
+
- Authorize for devecoffee ([#2](https://github.com/coolkg1412/devcoffee-nuxt-core/pull/2))
|
|
59
|
+
- The base UI and common Components ([#3](https://github.com/coolkg1412/devcoffee-nuxt-core/pull/3))
|
|
60
|
+
- Update docs ([b708e93](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/b708e93))
|
|
61
|
+
- Suppress DEP0155 deprecation warning in test scripts ([6fdb696](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/6fdb696))
|
|
62
|
+
- **test:** Split test scripts and enable headed E2E mode ([207c3d5](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/207c3d5))
|
|
63
|
+
- Bump `compatibilityDate` ([2654eb2](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/2654eb2))
|
|
64
|
+
|
|
65
|
+
### β
Tests
|
|
66
|
+
|
|
67
|
+
- **api:** Mark BUG-03 deprecation tests as todo until implemented ([69cd623](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/69cd623))
|
|
68
|
+
- Update import aliases and trim stale todo tests for BUG-03 ([f7c5cff](https://github.com/coolkg1412/devcoffee-nuxt-core/commit/f7c5cff))
|
|
69
|
+
|
|
70
|
+
### β€οΈ Contributors
|
|
71
|
+
|
|
72
|
+
- Hieu Nguyen <hieu.nguyen@devcoffee.tech>
|
|
73
|
+
- HiαΊΏu Nguyα»
n ([@coolkg1412](https://github.com/coolkg1412))
|
|
74
|
+
|
|
75
|
+
## v1.0.2
|
|
76
|
+
|
|
77
|
+
[compare changes](https://github.com/coolkg1412/devcoffee-nuxt-core/compare/v1.0.1...v1.0.2)
|
|
78
|
+
|
|
79
|
+
## v1.0.1
|
|
80
|
+
|
|
81
|
+
[compare changes](https://github.com/coolkg1412/devcoffee-nuxt-core/compare/v1.0.0...v1.0.1)
|
|
82
|
+
|
package/GUIDELINE.md
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
# @devcoffee/nuxt-core β Contributor Guide
|
|
2
|
+
|
|
3
|
+
This guide is for developers working on the module itself. If you are integrating the module into your app, see [README.md](./README.md).
|
|
4
|
+
|
|
5
|
+
## Quick Reference
|
|
6
|
+
|
|
7
|
+
| Topic | Section |
|
|
8
|
+
|---|---|
|
|
9
|
+
| Module layers and responsibilities | [Architecture](#architecture) |
|
|
10
|
+
| Annotated file tree | [Directory Reference](#directory-reference) |
|
|
11
|
+
| First-time setup | [Development Setup](#development-setup) |
|
|
12
|
+
| Test commands | [Test Infrastructure](#test-infrastructure) |
|
|
13
|
+
| Adding external dependencies | [Adapter Pattern](#adapter-pattern) |
|
|
14
|
+
| Per-request auth flow | [Request Lifecycle](#request-lifecycle) |
|
|
15
|
+
| Guarded design choices | [Key Architectural Decisions](#key-architectural-decisions) |
|
|
16
|
+
| Publishing a new version | [Release Pipeline](#release-pipeline) |
|
|
17
|
+
| Planning workflow | [GSD Workflow](#gsd-workflow) |
|
|
18
|
+
| Style and naming rules | [Coding Conventions](#coding-conventions) |
|
|
19
|
+
|
|
20
|
+
## Architecture
|
|
21
|
+
|
|
22
|
+
The module is organized into five layers. Each layer has a single responsibility and strict import rules.
|
|
23
|
+
|
|
24
|
+
### Layer 1: Module Definition (`src/module.ts`)
|
|
25
|
+
|
|
26
|
+
Entry point. Configures and registers all runtime components into Nuxt and Nitro during build:
|
|
27
|
+
|
|
28
|
+
- Registers server plugins, server composables, and server handler imports via `@nuxt/kit`
|
|
29
|
+
- Registers app plugins, app composables, and route middleware
|
|
30
|
+
- Injects runtime configuration from `nuxtCore` options
|
|
31
|
+
- Sets up Nitro storage and DevTools routes
|
|
32
|
+
|
|
33
|
+
### Layer 2: Server / Nitro (`src/runtime/server/`)
|
|
34
|
+
|
|
35
|
+
All server-side auth logic. Runs in the Nitro runtime (not the browser):
|
|
36
|
+
|
|
37
|
+
- `plugins/authts.ts` β global Nitro plugin; validates and refreshes sessions on every HTTP request
|
|
38
|
+
- `core/helpers.ts` β PKCE, state, token exchange, session CRUD, token refresh mutex
|
|
39
|
+
- `core/nuxtAuthtsHandler.ts` β `NuxtAuthtsHandler` export; handles GET_SESSION, TOKEN, LOGOUT, AUTHORIZE_URL actions
|
|
40
|
+
- `core/nuxtForwardHandler.ts` β `NuxtForwardRequestHandler` export; authenticated API proxy
|
|
41
|
+
- `adapters/` β external dependency wrappers (see [Adapter Pattern](#adapter-pattern))
|
|
42
|
+
- `composables/useServerLogger.ts` β server-side logging composable
|
|
43
|
+
|
|
44
|
+
### Layer 3: Client / App (`src/runtime/app/`)
|
|
45
|
+
|
|
46
|
+
Client-side auth state, UI interactions, and route protection. Runs in Vue/Nuxt:
|
|
47
|
+
|
|
48
|
+
- `plugins/authts.ts` β app plugin; initializes session state, provides `$sessionContext`, `$sessionReady`, fires hooks
|
|
49
|
+
- `middleware/authts.ts` β global route middleware; enforces `definePageMeta` auth requirements
|
|
50
|
+
- `composables/useAuthContext.ts` β reactive auth state and actions
|
|
51
|
+
- `composables/useSessionContext.ts` β low-level session accessor
|
|
52
|
+
- `composables/useLogger.ts` β client-side logging composable
|
|
53
|
+
- `pages/authorize.vue` β OIDC callback page (auto-registered at `openid.redirectUri`)
|
|
54
|
+
|
|
55
|
+
### Layer 4: Types (`src/types/`)
|
|
56
|
+
|
|
57
|
+
All TypeScript interfaces and augmentations:
|
|
58
|
+
|
|
59
|
+
- `types/authts.d.ts` β auth types (`SessionContext`, `AuthorizedUser`, `ModuleOptions`, etc.)
|
|
60
|
+
- `types/logging.d.ts` β logging types
|
|
61
|
+
- `types/index.d.ts` β central re-export point; consumers import from `@devcoffee/nuxt-core`
|
|
62
|
+
|
|
63
|
+
### Layer 5: Module Helpers (`src/helpers.ts`)
|
|
64
|
+
|
|
65
|
+
Option normalization and public config sanitization:
|
|
66
|
+
|
|
67
|
+
- `normalizedModuleOptions()` β merges user config with defaults
|
|
68
|
+
- `normalizePublicRuntimeConfig()` β strips private fields before injecting into runtime config
|
|
69
|
+
|
|
70
|
+
## Directory Reference
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
src/
|
|
74
|
+
βββ module.ts # Layer 1: Module Definition
|
|
75
|
+
βββ helpers.ts # Layer 5: Option normalization + defaults
|
|
76
|
+
βββ utils.ts # Utility relay (re-exports from runtime/utils)
|
|
77
|
+
βββ types/
|
|
78
|
+
β βββ index.d.ts # Central type re-export for consumers
|
|
79
|
+
β βββ authts.d.ts # Auth types (SessionContext, AuthorizedUser, etc.)
|
|
80
|
+
β βββ logging.d.ts # Logging types
|
|
81
|
+
βββ runtime/
|
|
82
|
+
βββ server/ # Layer 2: Server / Nitro
|
|
83
|
+
β βββ adapters/ # External dependency wrappers
|
|
84
|
+
β β βββ http.ts # h3 cookie, request, response, error functions
|
|
85
|
+
β β βββ oidc.ts # openid-client wrappers with module-owned types
|
|
86
|
+
β β βββ storage.ts # Nitro useStorage session CRUD
|
|
87
|
+
β β βββ utils.ts # deepMerge, omit, pick (native, no lodash)
|
|
88
|
+
β βββ core/ # Business logic β imports ONLY from adapters/
|
|
89
|
+
β β βββ helpers.ts
|
|
90
|
+
β β βββ nuxtAuthtsHandler.ts
|
|
91
|
+
β β βββ nuxtForwardHandler.ts
|
|
92
|
+
β βββ composables/
|
|
93
|
+
β β βββ useServerLogger.ts
|
|
94
|
+
β βββ plugins/
|
|
95
|
+
β β βββ authts.ts # Per-request session validation entry point
|
|
96
|
+
β βββ dev/ # DevTools route handler
|
|
97
|
+
βββ app/ # Layer 3: Client / App
|
|
98
|
+
βββ composables/
|
|
99
|
+
β βββ useAuthContext.ts
|
|
100
|
+
β βββ useSessionContext.ts
|
|
101
|
+
β βββ useLogger.ts
|
|
102
|
+
βββ middleware/
|
|
103
|
+
β βββ authts.ts # Global route protection middleware
|
|
104
|
+
βββ pages/
|
|
105
|
+
β βββ authorize.vue # OIDC callback page (auto-registered)
|
|
106
|
+
βββ plugins/
|
|
107
|
+
β βββ authts.ts # Session init, hooks, $sessionReady
|
|
108
|
+
β βββ logging.ts
|
|
109
|
+
β βββ formatters.ts
|
|
110
|
+
β βββ locale.ts
|
|
111
|
+
βββ utils/
|
|
112
|
+
βββ utils.ts # Utility relay
|
|
113
|
+
|
|
114
|
+
test/
|
|
115
|
+
βββ unit/ # Vitest unit tests
|
|
116
|
+
βββ e2e/ # Playwright + Vitest E2E tests
|
|
117
|
+
βββ fixture/ # Nuxt test app with mock OIDC server
|
|
118
|
+
βββ *.test.ts
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Development Setup
|
|
122
|
+
|
|
123
|
+
### Prerequisites
|
|
124
|
+
|
|
125
|
+
- Node.js LTS (18+)
|
|
126
|
+
- npm 10+
|
|
127
|
+
- A `.certs/devcoffee.ca.pem` file β required for TLS in development (internal CA). Place the DevCoffee CA certificate at this path.
|
|
128
|
+
|
|
129
|
+
### First-time setup
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
# 1. Clone and install
|
|
133
|
+
git clone <repo-url>
|
|
134
|
+
cd devcoffee-nuxt-core
|
|
135
|
+
npm install
|
|
136
|
+
|
|
137
|
+
# 2. Generate type stubs (required before dev or test)
|
|
138
|
+
npm run dev:prepare
|
|
139
|
+
|
|
140
|
+
# 3. Start the playground dev server
|
|
141
|
+
npm run dev
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
The playground runs at `http://localhost:3000`. The custom CA certificate is injected via `NODE_EXTRA_CA_CERTS` in the `dev` script.
|
|
145
|
+
|
|
146
|
+
### Clean rebuild
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
npm run cleanup # Remove .nuxt and playground build artifacts
|
|
150
|
+
npm run dev:prepare
|
|
151
|
+
npm run dev
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Test Infrastructure
|
|
155
|
+
|
|
156
|
+
| Command | What it runs | When to use |
|
|
157
|
+
|---|---|---|
|
|
158
|
+
| `npm run test` | Vitest unit suite (once) | Before committing |
|
|
159
|
+
| `npm run test:watch` | Vitest unit suite (watch mode) | During development |
|
|
160
|
+
| `npm run test:types` | `vue-tsc --noEmit` type check | Before committing |
|
|
161
|
+
| `npm run test:e2e` | Playwright + Vitest E2E tests (headless) | After changing auth flow, middleware, or session handling |
|
|
162
|
+
| `npm run test:e2e:ui` | E2E tests in headed Chromium (via `PLAYWRIGHT_HEADLESS=false`) | Debugging browser-mode E2E failures |
|
|
163
|
+
| `npm run test:all` | All unit + E2E tests combined | Before opening a PR or releasing |
|
|
164
|
+
|
|
165
|
+
### Unit tests (`test/unit/`)
|
|
166
|
+
|
|
167
|
+
Written with Vitest. Use `@nuxt/test-utils` for composable and middleware tests. Mock Nitro virtual modules where needed (e.g., `vi.mock('nitropack/runtime')`).
|
|
168
|
+
|
|
169
|
+
Tests follow source-text assertions for structural code presence and behavior assertions for logic. TDD was used for SEC-*, PERF-*, MDLW-*, and SSR-* requirements β new security or behavior requirements should follow the same pattern.
|
|
170
|
+
|
|
171
|
+
### E2E tests (`test/e2e/`)
|
|
172
|
+
|
|
173
|
+
Run in Vitest but use `@playwright/test` for browser automation and `createNuxtDevServer` from `@nuxt/test-utils` to spin up the fixture Nuxt app. A mock OIDC server is embedded as a Nitro route handler in the fixture.
|
|
174
|
+
|
|
175
|
+
Key files:
|
|
176
|
+
|
|
177
|
+
- `test/e2e/fixture/` β test Nuxt app (standalone, not the playground)
|
|
178
|
+
- `test/e2e/fixture/server/routes/` β mock OIDC discovery, token, and userinfo endpoints
|
|
179
|
+
- `test/e2e/middleware.test.ts` β E2E-01 (required redirect) and E2E-02 (unauthenticatedOnly)
|
|
180
|
+
- `test/e2e/session.test.ts` β E2E-03 (session creation) and E2E-07 (token refresh)
|
|
181
|
+
- `test/e2e/auth-flow.test.ts` β E2E-04 (valid TOKEN flow), E2E-05 (invalid state), E2E-06 (LOGOUT)
|
|
182
|
+
- `test/e2e/auth-flow-browser.test.ts` β E2E-08 (browser-mode Chromium tests)
|
|
183
|
+
|
|
184
|
+
**Important:** Browser tests (`auth-flow-browser.test.ts`) are isolated in their own file to prevent `EADDRINUSE` port conflicts when two Nuxt dev servers start in the same process.
|
|
185
|
+
|
|
186
|
+
## Adapter Pattern
|
|
187
|
+
|
|
188
|
+
### Why adapters exist
|
|
189
|
+
|
|
190
|
+
Business logic in `src/runtime/server/core/` must not import directly from `h3`, `openid-client`, or any external npm package. Instead, all external calls go through adapter barrel files in `src/runtime/server/adapters/`.
|
|
191
|
+
|
|
192
|
+
**Rationale:** Adapters create a stable internal boundary. If `h3` or `openid-client` upgrades break their API, only the adapter needs to change β not every call site in the business logic. Adapters also own the type bridge between external library types and module-owned types (e.g., `OidcUserInfo`).
|
|
193
|
+
|
|
194
|
+
This rule is enforced by grep assertions in the test suite:
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
# These must return zero matches:
|
|
198
|
+
grep -r "from 'h3'" src/runtime/server/core/
|
|
199
|
+
grep -r "from 'openid-client'" src/runtime/server/core/
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### How to add a new external dependency
|
|
203
|
+
|
|
204
|
+
1. Install the package as a dependency
|
|
205
|
+
2. Create (or update) the appropriate adapter file in `src/runtime/server/adapters/`
|
|
206
|
+
3. Write module-owned types for any types the adapter exposes
|
|
207
|
+
4. Export only what business logic needs β keep the adapter surface minimal
|
|
208
|
+
5. Import from the adapter in `core/` files, never directly from the package
|
|
209
|
+
|
|
210
|
+
Example: adding a hypothetical `jose` utility to the http adapter:
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
// src/runtime/server/adapters/http.ts
|
|
214
|
+
import { decodeJwt as _decodeJwt } from 'jose'
|
|
215
|
+
import type { JWTPayload } from './types' // module-owned type, not jose's type
|
|
216
|
+
|
|
217
|
+
export function decodeJwt(token: string): JWTPayload {
|
|
218
|
+
return _decodeJwt(token) as JWTPayload
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Request Lifecycle
|
|
223
|
+
|
|
224
|
+
Every HTTP request to the Nuxt app passes through this sequence:
|
|
225
|
+
|
|
226
|
+
```
|
|
227
|
+
HTTP request
|
|
228
|
+
β
|
|
229
|
+
βΌ
|
|
230
|
+
Nitro server plugin (src/runtime/server/plugins/authts.ts)
|
|
231
|
+
β Reads session cookie β validateSession()
|
|
232
|
+
β If authenticated + near-expiry β refreshTokenIfNeeded() [mutex-protected per session]
|
|
233
|
+
β Sets event.context.sessionId
|
|
234
|
+
β
|
|
235
|
+
βΌ
|
|
236
|
+
Route handler (src/runtime/server/core/nuxtAuthtsHandler.ts)
|
|
237
|
+
β Reads action from URL path: GET_SESSION | TOKEN | LOGOUT | AUTHORIZE_URL
|
|
238
|
+
β Calls getSession(event.context.sessionId) β reads + decrypts session from storage
|
|
239
|
+
β Dispatches to action handler
|
|
240
|
+
β
|
|
241
|
+
βΌ
|
|
242
|
+
Client (src/runtime/app/plugins/authts.ts)
|
|
243
|
+
β useAsyncData('authts:session') fetches /api/_auth/session on SSR
|
|
244
|
+
β Sets $sessionReady promise β resolved before route middleware runs
|
|
245
|
+
β
|
|
246
|
+
βΌ
|
|
247
|
+
Route middleware (src/runtime/app/middleware/authts.ts)
|
|
248
|
+
β Awaits $sessionReady
|
|
249
|
+
β Reads definePageMeta({ authts: { required, unauthenticatedOnly } })
|
|
250
|
+
β Redirects or aborts navigation based on auth state
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Authorization code flow (login)
|
|
254
|
+
|
|
255
|
+
```
|
|
256
|
+
User clicks login
|
|
257
|
+
β
|
|
258
|
+
βΌ
|
|
259
|
+
useAuthContext.login(redirectTo)
|
|
260
|
+
β GET /api/_auth/authorize-url β returns OIDC authorization URL
|
|
261
|
+
β Stores redirectTo in session cookie (auths.redirect)
|
|
262
|
+
β
|
|
263
|
+
βΌ
|
|
264
|
+
Browser redirects to OIDC provider
|
|
265
|
+
β
|
|
266
|
+
βΌ
|
|
267
|
+
Provider redirects to /authorize?code=...&state=...
|
|
268
|
+
β
|
|
269
|
+
βΌ
|
|
270
|
+
authorize.vue (auto-registered OIDC callback page)
|
|
271
|
+
β useAuthContext.authorize(code, state)
|
|
272
|
+
β POST /api/_auth/token β validates state, exchanges code for tokens
|
|
273
|
+
β If autoFetchUser: fetches userinfo from provider
|
|
274
|
+
β Stores session in Nitro storage (tokenSet encrypted if secret is set)
|
|
275
|
+
β Fires user:loggedIn hook
|
|
276
|
+
β
|
|
277
|
+
βΌ
|
|
278
|
+
Browser redirects to intended destination
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
## Key Architectural Decisions
|
|
282
|
+
|
|
283
|
+
These decisions are enforced by tests or type constraints. Changing them requires updating the tests that guard them.
|
|
284
|
+
|
|
285
|
+
| Decision | What it means |
|
|
286
|
+
|---|---|
|
|
287
|
+
| `sessions.secret` empty = no signing | Backward compatible β existing deployments without a secret continue working. Insecure for new deployments. |
|
|
288
|
+
| HKDF-SHA256 derives AES key from `sessions.secret` | Domain-separated with `'devcoffee-authts-tokenset-v1'` info string |
|
|
289
|
+
| Cookie names are `auths.ssid`, `auths.state`, `auths.pkce`, `auths.redirect` | Configured in `sessions.names` defaults in `src/helpers.ts` |
|
|
290
|
+
| `deleteSession()` is separate from `renewSession()` | Logout deletes the session and does not create a replacement. Prevents zombie sessions. |
|
|
291
|
+
| Token refresh mutex per session | Lock is placed inside `accessExpired && refreshToken` branch only β no storage overhead for non-expiring tokens |
|
|
292
|
+
| Adapter import enforcement | Verified by grep in test suite. Zero direct `h3` or `openid-client` imports allowed in `core/`. |
|
|
293
|
+
| `usePkce: false` in E2E fixture | PKCE threading through test requires additional cookie management. PKCE is fully covered at the unit layer. |
|
|
294
|
+
|
|
295
|
+
Full decision log: see `## Decisions` in `.planning/STATE.md`.
|
|
296
|
+
|
|
297
|
+
## Release Pipeline
|
|
298
|
+
|
|
299
|
+
```bash
|
|
300
|
+
npm run release
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
This runs in sequence: `lint` β `test:all` β `prepack` β `changelogen` β `npm publish` β `git push --follow-tags`.
|
|
304
|
+
|
|
305
|
+
Individual steps:
|
|
306
|
+
|
|
307
|
+
```bash
|
|
308
|
+
npm run lint # ESLint check (with --fix)
|
|
309
|
+
npm run test # Vitest unit suite
|
|
310
|
+
npm run test:types # vue-tsc type check
|
|
311
|
+
npm run test:all # All unit + E2E tests combined
|
|
312
|
+
npm run prepack # Build module dist (ES module + .d.ts types via @nuxt/module-builder)
|
|
313
|
+
npm run release # Full pipeline (lint + test:all + prepack + changelogen + publish + push)
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
The published package is `@devcoffee/nuxt-core`. The `dist/` directory is generated by `@nuxt/module-builder` and is what consumers install.
|
|
317
|
+
|
|
318
|
+
## GSD Workflow
|
|
319
|
+
|
|
320
|
+
This project uses a phase-based planning system. All code changes must go through a GSD command to keep planning artifacts in sync.
|
|
321
|
+
|
|
322
|
+
| Task type | Entry point |
|
|
323
|
+
|---|---|
|
|
324
|
+
| Small fix or ad-hoc change | `/gsd:quick` |
|
|
325
|
+
| Bug investigation | `/gsd:debug` |
|
|
326
|
+
| Planned phase work | `/gsd:execute-phase` |
|
|
327
|
+
|
|
328
|
+
Planning artifacts live in `.planning/`:
|
|
329
|
+
|
|
330
|
+
- `ROADMAP.md` β all phases and their requirements
|
|
331
|
+
- `STATE.md` β current position, accumulated decisions, blockers
|
|
332
|
+
- `phases/NN-slug/` β per-phase RESEARCH.md, PLAN.md files, SUMMARY.md files
|
|
333
|
+
|
|
334
|
+
Do not make direct file edits outside a GSD workflow unless explicitly bypassing it.
|
|
335
|
+
|
|
336
|
+
## Coding Conventions
|
|
337
|
+
|
|
338
|
+
Key rules (full reference: `CLAUDE.md`):
|
|
339
|
+
|
|
340
|
+
- **No semicolons** β Prettier enforces this
|
|
341
|
+
- **Single quotes** for strings
|
|
342
|
+
- **2-space indent**, 120-char line length
|
|
343
|
+
- **Trailing commas:** es5 style (objects and arrays, not function parameters)
|
|
344
|
+
- **TypeScript strict** β no `any` in public API surface
|
|
345
|
+
- **Composables:** `use[Name].ts` file and function name
|
|
346
|
+
- **Server handlers:** `Nuxt[Name]Handler` (PascalCase with `Nuxt` prefix)
|
|
347
|
+
- **Getters:** `get[Name]`, **Setters:** `set[Name]`, **Validators:** `validate[Name]`
|
|
348
|
+
- **Logging:** Use `useLogger` on client, `useServerLogger` on server. Always include `tag` and `level`.
|
|
349
|
+
- **Error handling:** `createError()` from h3 for server errors, `abortNavigation(createError())` for middleware
|
|
350
|
+
- **Imports:** Prefer named imports. Use `#imports` for Nuxt auto-imports.
|
|
351
|
+
- **JSDoc:** All public functions and composables must have JSDoc with `@param`, `@returns`, `@example`, `@since 1.0.0`
|
package/README.md
CHANGED
|
@@ -252,8 +252,6 @@ export default NuxtForwardRequestHandler({
|
|
|
252
252
|
})
|
|
253
253
|
```
|
|
254
254
|
|
|
255
|
-
> **Note:** `NuxtForwardRequestHandller` (double-l) is a deprecated alias kept for backward compatibility. Use `NuxtForwardRequestHandler` in new code.
|
|
256
|
-
|
|
257
255
|
## Route Protection
|
|
258
256
|
|
|
259
257
|
The module registers a global Nuxt middleware that runs on every navigation. Control access per page with `definePageMeta`.
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { addCustomTab } from '@nuxt/devtools-kit';
|
|
|
2
2
|
import { defineNuxtModule, useLogger, createResolver, addServerImports, addServerImportsDir, addServerPlugin, addImportsDir, addPlugin, addRouteMiddleware, addTemplate, addServerHandler } from '@nuxt/kit';
|
|
3
3
|
import { deepMerge, pick } from '../dist/runtime/utils.js';
|
|
4
4
|
|
|
5
|
-
const version = "1.1.
|
|
5
|
+
const version = "1.1.1";
|
|
6
6
|
|
|
7
7
|
const defaultLocale = "vi-VN";
|
|
8
8
|
const defaultLanguage = "vi";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createError, deleteCookie, eventHandler, getCookie, getQuery, getRequestHeaders, getRequestURL, proxyRequest, readBody, readFormData, sendNoContent, setCookie, } from 'h3';
|
|
2
2
|
export type { EventHandlerRequest, H3Error, H3Event, ProxyOptions, RequestHeaders } from 'h3';
|
|
3
3
|
export { defineNitroPlugin, useRuntimeConfig } from 'nitropack/runtime';
|
|
4
|
-
export type { CoreLogLevel, NuxtAuthOptions } from '
|
|
4
|
+
export type { CoreLogLevel, NuxtAuthOptions } from '@devcoffee/nuxt-core';
|
|
5
5
|
export type { CookieSerializeOptions } from 'cookie-es';
|
|
@@ -12,10 +12,16 @@ import {
|
|
|
12
12
|
refreshTokenGrant as refreshTokenGrantFromOidc,
|
|
13
13
|
revokeToken
|
|
14
14
|
} from "#devcoffee-core/server/adapters/oidc";
|
|
15
|
+
import {
|
|
16
|
+
getSessionData,
|
|
17
|
+
hasSessionData,
|
|
18
|
+
removeSessionData,
|
|
19
|
+
setSessionData
|
|
20
|
+
} from "#devcoffee-core/server/adapters/storage";
|
|
15
21
|
import { deepMerge, omit } from "#devcoffee-core/server/adapters/utils";
|
|
16
22
|
import useServerLogger from "#devcoffee-core/server/composables/useServerLogger";
|
|
17
23
|
import { useRuntimeConfig } from "#imports";
|
|
18
|
-
import { useStorage } from "nitropack/runtime
|
|
24
|
+
import { useStorage } from "nitropack/runtime";
|
|
19
25
|
import { decryptTokenSet, encryptTokenSet, generateSessionId, isValidSessionId, verifySessionId } from "./crypto.js";
|
|
20
26
|
import { tryAcquireLock } from "./mutex.js";
|
|
21
27
|
function getAnonymousUser(extras) {
|
|
@@ -48,10 +54,9 @@ export function isSameOrigin(redirectUrl, requestUrl) {
|
|
|
48
54
|
}
|
|
49
55
|
}
|
|
50
56
|
export async function getSession(sessionId, opts) {
|
|
51
|
-
const storage = useStorage(opts.storageName);
|
|
52
57
|
const sessingKey = getSessionStorageKey(opts.storagePrefix, sessionId);
|
|
53
|
-
if (!await
|
|
54
|
-
const session = await
|
|
58
|
+
if (!await hasSessionData(opts.storageName, sessingKey)) return null;
|
|
59
|
+
const session = await getSessionData(opts.storageName, sessingKey);
|
|
55
60
|
if (!session) return null;
|
|
56
61
|
if (session.auth?.tokenSet && session.auth.tokenSet.encrypted === true) {
|
|
57
62
|
if (opts.secret) {
|
|
@@ -78,7 +83,6 @@ function newSession(sessionId, expiresAt) {
|
|
|
78
83
|
};
|
|
79
84
|
}
|
|
80
85
|
export async function validateSession(sessionCookieId, opts) {
|
|
81
|
-
const storage = useStorage(opts.storageName);
|
|
82
86
|
const now = Date.now();
|
|
83
87
|
const expiresAt = now + opts.expiresIn;
|
|
84
88
|
let sessionId = void 0;
|
|
@@ -91,8 +95,8 @@ export async function validateSession(sessionCookieId, opts) {
|
|
|
91
95
|
}
|
|
92
96
|
if (sessionId) {
|
|
93
97
|
sessionKey = getSessionStorageKey(opts.storagePrefix, sessionId);
|
|
94
|
-
if (await
|
|
95
|
-
session = await
|
|
98
|
+
if (await hasSessionData(opts.storageName, sessionKey)) {
|
|
99
|
+
session = await getSessionData(opts.storageName, sessionKey);
|
|
96
100
|
}
|
|
97
101
|
if (!session || session.expiresAt <= now) {
|
|
98
102
|
deleteSessionKey = session ? sessionKey : void 0;
|
|
@@ -105,22 +109,19 @@ export async function validateSession(sessionCookieId, opts) {
|
|
|
105
109
|
session = session || newSession(generateSessionId(), expiresAt);
|
|
106
110
|
sessionKey = sessionKey || getSessionStorageKey(opts.storagePrefix, session.id);
|
|
107
111
|
if (deleteSessionKey && deleteSessionKey !== sessionKey) {
|
|
108
|
-
await
|
|
112
|
+
await removeSessionData(opts.storageName, deleteSessionKey);
|
|
109
113
|
}
|
|
110
|
-
await
|
|
111
|
-
ttl: opts.expiresIn / 1e3 | 0
|
|
112
|
-
});
|
|
114
|
+
await setSessionData(opts.storageName, sessionKey, session, opts.expiresIn / 1e3 | 0);
|
|
113
115
|
return session;
|
|
114
116
|
}
|
|
115
117
|
export async function updateSession(sessionId, input, opts) {
|
|
116
118
|
const now = Date.now();
|
|
117
|
-
const storage = useStorage(opts.storageName);
|
|
118
119
|
const serverKey = `${opts.storagePrefix}:${sessionId}`;
|
|
119
120
|
const normalizedInput = omit(
|
|
120
121
|
input,
|
|
121
122
|
["id", "issuedAt", "expiresAt"]
|
|
122
123
|
);
|
|
123
|
-
let session = await
|
|
124
|
+
let session = await getSessionData(opts.storageName, serverKey);
|
|
124
125
|
if (!session) {
|
|
125
126
|
throw createError({
|
|
126
127
|
status: 500,
|
|
@@ -140,29 +141,23 @@ export async function updateSession(sessionId, input, opts) {
|
|
|
140
141
|
)
|
|
141
142
|
};
|
|
142
143
|
}
|
|
143
|
-
await
|
|
144
|
-
ttl: opts.expiresIn / 1e3 | 0
|
|
145
|
-
});
|
|
144
|
+
await setSessionData(opts.storageName, serverKey, sessionToStore, opts.expiresIn / 1e3 | 0);
|
|
146
145
|
return session;
|
|
147
146
|
}
|
|
148
147
|
export async function renewSession(sessionId, opts) {
|
|
149
|
-
const storage = useStorage(opts.storageName);
|
|
150
148
|
let sessionKey = getSessionStorageKey(opts.storagePrefix, sessionId);
|
|
151
|
-
if (await
|
|
152
|
-
await
|
|
149
|
+
if (await hasSessionData(opts.storageName, sessionKey)) {
|
|
150
|
+
await removeSessionData(opts.storageName, sessionKey);
|
|
153
151
|
}
|
|
154
152
|
const session = newSession(generateSessionId(), Date.now() + opts.expiresIn);
|
|
155
153
|
sessionKey = getSessionStorageKey(opts.storagePrefix, session.id);
|
|
156
|
-
await
|
|
157
|
-
ttl: opts.expiresIn / 1e3 | 0
|
|
158
|
-
});
|
|
154
|
+
await setSessionData(opts.storageName, sessionKey, session, opts.expiresIn / 1e3 | 0);
|
|
159
155
|
return session;
|
|
160
156
|
}
|
|
161
157
|
export async function deleteSession(sessionId, opts) {
|
|
162
|
-
const storage = useStorage(opts.storageName);
|
|
163
158
|
const sessionKey = getSessionStorageKey(opts.storagePrefix, sessionId);
|
|
164
|
-
if (await
|
|
165
|
-
await
|
|
159
|
+
if (await hasSessionData(opts.storageName, sessionKey)) {
|
|
160
|
+
await removeSessionData(opts.storageName, sessionKey);
|
|
166
161
|
}
|
|
167
162
|
}
|
|
168
163
|
export async function discoveryOpendId(wellKnownUrl, opts) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import useServerLogger from "#devcoffee-core/server/composables/useServerLogger";
|
|
2
|
-
import { useStorage } from "nitropack/runtime
|
|
2
|
+
import { useStorage } from "nitropack/runtime";
|
|
3
3
|
const logger = useServerLogger({ tag: "authts-mutex", level: 3 });
|
|
4
4
|
export async function tryAcquireLock(storage, lockKey, ttlSeconds, useAtomic) {
|
|
5
5
|
if (useAtomic) {
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
useRuntimeConfig
|
|
11
11
|
} from "#devcoffee-core/server/adapters/http";
|
|
12
12
|
import { deepMerge, omit } from "#devcoffee-core/server/adapters/utils";
|
|
13
|
-
import { useStorage } from "nitropack/runtime
|
|
13
|
+
import { useStorage } from "nitropack/runtime";
|
|
14
14
|
import {
|
|
15
15
|
authorizationCodeGrant,
|
|
16
16
|
buildAuthorizationUrl,
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
+
import { defineNitroPlugin, getCookie, setCookie, useRuntimeConfig } from "#devcoffee-core/server/adapters/http";
|
|
1
2
|
import { signSessionId } from "#devcoffee-core/server/core/crypto";
|
|
2
3
|
import { getSession, refreshTokenIfNeeded, updateSession, validateSession } from "#devcoffee-core/server/core/helpers";
|
|
3
|
-
import { getCookie, setCookie } from "h3";
|
|
4
|
-
import { defineNitroPlugin, useRuntimeConfig } from "nitropack/runtime";
|
|
5
4
|
export default defineNitroPlugin((nitroApp) => {
|
|
6
5
|
nitroApp.hooks.hook("request", async (event) => {
|
|
7
6
|
const {
|
|
8
7
|
enabled: authtsEnabled,
|
|
9
|
-
openid: { wellKnownUrl, cache, clientId, clientSecret, tokenRefreshBufferMs },
|
|
8
|
+
openid: { wellKnownUrl, cache, clientId, clientSecret, tokenRefreshBufferMs, distributedLock },
|
|
10
9
|
sessions: {
|
|
11
10
|
expiresIn,
|
|
12
11
|
secret = "",
|
|
@@ -27,7 +26,8 @@ export default defineNitroPlugin((nitroApp) => {
|
|
|
27
26
|
cache,
|
|
28
27
|
clientId,
|
|
29
28
|
clientSecret,
|
|
30
|
-
tokenRefreshBufferMs
|
|
29
|
+
tokenRefreshBufferMs,
|
|
30
|
+
distributedLock
|
|
31
31
|
});
|
|
32
32
|
if (Object.keys(sessionUpdate).length > 0) {
|
|
33
33
|
await updateSession(session.id, sessionUpdate, { storageName, storagePrefix, expiresIn, secret });
|
package/package.json
CHANGED
|
@@ -1,8 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devcoffee/nuxt-core",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"author": "Hieu Nguyen <hieunguyen@devcoffee.tech>",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "Nuxt 4 module providing OpenID Connect / OAuth 2.0 authorization code grant with PKCE, server-side session management via Nitro, client-side auth state composables, and universal route protection middleware.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"nuxt",
|
|
8
|
+
"nuxt-module",
|
|
9
|
+
"nuxt4",
|
|
10
|
+
"oidc",
|
|
11
|
+
"oauth2",
|
|
12
|
+
"pkce",
|
|
13
|
+
"openid-connect",
|
|
14
|
+
"authentication",
|
|
15
|
+
"session",
|
|
16
|
+
"jwt",
|
|
17
|
+
"nitro",
|
|
18
|
+
"vue3",
|
|
19
|
+
"auth",
|
|
20
|
+
"middleware",
|
|
21
|
+
"composables"
|
|
22
|
+
],
|
|
6
23
|
"license": "MIT",
|
|
7
24
|
"type": "module",
|
|
8
25
|
"exports": {
|
|
@@ -20,7 +37,9 @@
|
|
|
20
37
|
}
|
|
21
38
|
},
|
|
22
39
|
"files": [
|
|
23
|
-
"dist"
|
|
40
|
+
"dist",
|
|
41
|
+
"CHANGELOG.md",
|
|
42
|
+
"GUIDELINE.md"
|
|
24
43
|
],
|
|
25
44
|
"nuxt": {
|
|
26
45
|
"module": "src/module.ts"
|
|
@@ -71,4 +90,4 @@
|
|
|
71
90
|
"vitest": "^3.2.4",
|
|
72
91
|
"vue-tsc": "^3.1.0"
|
|
73
92
|
}
|
|
74
|
-
}
|
|
93
|
+
}
|