Skip to content

feat: globalMiddleware option#163

Open
felix-dolderer wants to merge 9 commits intonuxt-modules:mainfrom
felix-dolderer:main
Open

feat: globalMiddleware option#163
felix-dolderer wants to merge 9 commits intonuxt-modules:mainfrom
felix-dolderer:main

Conversation

@felix-dolderer
Copy link
Copy Markdown
Contributor

@felix-dolderer felix-dolderer commented Aug 16, 2024

🔗 Linked issue

Closes #114

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Adds a configuration option that adds a global middleware. This option requires being logged in for all pages (unless otherwise specified on the individual page)

Exposes and auto-imports the middleware functions (hankoLoggedIn, hankoLoggedOut) so they can be imported in a simple and typesafe way.

ℹ️ Help / Review wanted

  1. I'm not sure if auto-importing the existing middleware functions (hankoLoggedIn, hankoLoggedOut) is necessary or the best way to make them accessible for building custom middleware on top of them.
  2. How can I write tests for the logged-in behaviour? I logged in with my demo Hanko-API, took my current cookie and used that to make sure my tests were passing. This approach is obviously hard to replicate for other people who want to run the tests. (Addition: And it breaks automated testing.)

Copy link
Copy Markdown
Collaborator

@danielroe danielroe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I missed this PR! one comment/question

@felix-dolderer felix-dolderer marked this pull request as draft April 25, 2025 15:41
@felix-dolderer felix-dolderer marked this pull request as ready for review June 15, 2025 15:12
@codecov
Copy link
Copy Markdown

codecov bot commented Jun 15, 2025

Codecov Report

❌ Patch coverage is 16.66667% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 29.09%. Comparing base (c8d99bf) to head (074d569).
⚠️ Report is 297 commits behind head on main.

Files with missing lines Patch % Lines
src/runtime/middleware/global-logged-in.ts 0.00% 8 Missing and 8 partials ⚠️
src/module.ts 66.66% 1 Missing and 1 partial ⚠️
src/runtime/middleware/logged-in.ts 0.00% 1 Missing ⚠️
src/runtime/middleware/logged-out.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #163      +/-   ##
==========================================
- Coverage   36.82%   29.09%   -7.74%     
==========================================
  Files          16        9       -7     
  Lines         410      110     -300     
  Branches       25       30       +5     
==========================================
- Hits          151       32     -119     
+ Misses        245       57     -188     
- Partials       14       21       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@felix-dolderer felix-dolderer force-pushed the main branch 2 times, most recently from 02ad6a5 to b9471a9 Compare June 17, 2025 19:12
@felix-dolderer felix-dolderer requested a review from danielroe June 17, 2025 19:18
@felix-dolderer
Copy link
Copy Markdown
Contributor Author

I noticed none of the auth modules for nuxt have tests for the logged in state. I decided to fix that and added tests that mock useHanko and useRequestEvent in a way that the middleware accepts the request as authenticated.

- Introduced `globalMiddleware` option in the Nuxt configuration to protect all pages by default.
- Added new route middleware for handling access based on user authentication status.
- Updated README to include details on global middleware usage and configuration.
- Created example pages demonstrating the use of `allow` and `deny` settings in pageMeta.
- Enhanced test coverage for global middleware scenarios, including client-side and SSR tests.
…tion

- Introduced a new `page-meta.d.ts` file to define the `hanko` property in the `PageMeta` interface.
- Updated the module to prepare type references for the new page metadata definitions.
…ting

- Added a computed property to determine if global middleware is active.
- Updated the navigation structure in `app.vue` to conditionally render links based on the global middleware state.
- Enhanced the module to include `globalMiddleware` in the Nuxt configuration for improved route protection.
- Replaced the old page-meta.d.ts file with a new page-meta-global.d.ts to avoid automatic inclusion in the TypeScript project.
- Updated type preparation in the module to reference the new file for conditional loading based on globalMiddleware settings.
- Removed the outdated page-meta.d.ts file.
felix-dolderer and others added 3 commits March 1, 2026 00:14
- Added a computed property to check if the application is running in a test environment.
- Updated the navigation structure in `app.vue` to conditionally render links based on the test environment and global middleware state.
@felix-dolderer
Copy link
Copy Markdown
Contributor Author

I synced all forked repositories and didn't pay attention that I have a PR tied to this fork. I really dislike doing code review when people rebase their branches because you lose your review progress - So I'm sorry for doing that myself now. 🙈

Made some small improvements to the PR after the rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Global middleware option

3 participants