diff --git a/.github/workflows/autofix.yml b/.github/workflows/autofix.yml index 9701bd33..83235cce 100644 --- a/.github/workflows/autofix.yml +++ b/.github/workflows/autofix.yml @@ -20,6 +20,9 @@ jobs: - name: 📦 Install dependencies run: pnpm install + - name: 🎭 Install playwright + run: pnpm exec playwright install + - name: 🚧 Set up project run: pnpm dev:prepare diff --git a/README.md b/README.md index d606f3a1..36af6ea9 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ export default defineNuxtConfig({ // translations: {}, // fallbackLanguage: 'en' // } + // globalMiddleware: false, }, }) ``` @@ -75,17 +76,39 @@ Check out the [Hanko documentation](https://docs.hanko.io/guides/vue) to learn m ### Middleware -By default two new route middleware are available in your Nuxt app: `hanko-logged-in` and `hanko-logged-out`. +By default two new route middlewares are available in your Nuxt app: `hanko-logged-in` and `hanko-logged-out`. - `hanko-logged-in` will prevent access to a page unless you are logged in. (It will redirect you to `redirects.login` instead, and then redirect back to this page once you login. You can disable this behaviour by setting `redirects.followRedirect` to `false`.) - `hanko-logged-out` will prevent access to a page unless you are logged out. (It will redirect you to `redirects.success` when you log in, and otherwise to `redirects.home`.) You can also create your own middleware for full control. +### Global Middleware + +If the `globalMiddleware` configuration is set to `true`, all of your pages are protected by default. +You can still override this behavior on each page, by applying a custom hanko pageMeta. +You can set a value for **either** allow or deny as shown below. + +```TypeScript +definePageMeta({ + hanko: { + // allow: 'all' | 'logged-in' | 'logged-out' + // deny: 'logged-in' | 'logged-out' + } +}) +``` + +This pageMeta will only be taken into consideration when the `globalMiddleware` option is enabled. +You should not use hanko-middleware when `globalMiddleware` is enabled and instead use pageMeta. + +**Note**: The `globalMiddleware` option will not apply any authentication checks to your API-paths. + ### Auto-imports `useHanko` is exposed in the Vue part of your app to allow you direct access to the Hanko API. You can access the current user and much more. **Note**: It will return `null` on the server. +The `hankoLoggedIn` and `hankoLoggedOut` middleware are exposed to enable you to extend their functionality, such as creating a custom global middleware. + ### Server utilities By default you can access a verified JWT context on `event.context.hanko`. (It will be undefined if the user is not logged in.) If you want to handle this yourself you can set `augmentContext: false`. diff --git a/playground/app.vue b/playground/app.vue index 11ddebf6..a0d6f05a 100644 --- a/playground/app.vue +++ b/playground/app.vue @@ -1,20 +1,71 @@ + + diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 5197f9bc..08eae0ea 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -13,6 +13,12 @@ export default defineNuxtConfig({ }, }, compatibilityDate: '2024-08-19', + // Expose VITEST to the client when running under Vitest so the nav shows both link groups in tests + vite: { + define: { + 'process.env.VITEST': JSON.stringify(process.env.VITEST ?? ''), + }, + }, hanko: { // You need to provide the Hanko API URL in order for it to work apiURL: '', diff --git a/playground/pages/global/allow/all.vue b/playground/pages/global/allow/all.vue new file mode 100644 index 00000000..0f09ffbd --- /dev/null +++ b/playground/pages/global/allow/all.vue @@ -0,0 +1,22 @@ + + + diff --git a/playground/pages/global/allow/logged-in.vue b/playground/pages/global/allow/logged-in.vue new file mode 100644 index 00000000..1ec86b39 --- /dev/null +++ b/playground/pages/global/allow/logged-in.vue @@ -0,0 +1,18 @@ + + + diff --git a/playground/pages/global/allow/logged-out.vue b/playground/pages/global/allow/logged-out.vue new file mode 100644 index 00000000..7036bd5f --- /dev/null +++ b/playground/pages/global/allow/logged-out.vue @@ -0,0 +1,18 @@ + + + diff --git a/playground/pages/global/deny/logged-in.vue b/playground/pages/global/deny/logged-in.vue new file mode 100644 index 00000000..cc745c22 --- /dev/null +++ b/playground/pages/global/deny/logged-in.vue @@ -0,0 +1,22 @@ + + + diff --git a/playground/pages/global/deny/logged-out.vue b/playground/pages/global/deny/logged-out.vue new file mode 100644 index 00000000..fc2b64ba --- /dev/null +++ b/playground/pages/global/deny/logged-out.vue @@ -0,0 +1,22 @@ + + + diff --git a/playground/pages/global/incorrect-usage.vue b/playground/pages/global/incorrect-usage.vue new file mode 100644 index 00000000..2ea23d5f --- /dev/null +++ b/playground/pages/global/incorrect-usage.vue @@ -0,0 +1,13 @@ + + + diff --git a/playground/pages/login.vue b/playground/pages/login.vue index 55ce65e0..c159e4c1 100644 --- a/playground/pages/login.vue +++ b/playground/pages/login.vue @@ -7,13 +7,16 @@ definePageMeta({