diff --git a/README.md b/README.md index d606f3a1..11cbe792 100644 --- a/README.md +++ b/README.md @@ -84,11 +84,11 @@ You can also create your own middleware for full control. ### 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. +`useHanko` and `$hanko` 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. ### 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`. +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`. `verifyHankoEvent` is exposed in the Nitro part of your app to expose the underlying utility used to create `event.context.hanko` if you want to handle things manually. diff --git a/package.json b/package.json index 5a26c794..14e77e8d 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ }, "dependencies": { "@nuxt/kit": "^4.0.0", - "@teamhanko/hanko-elements": "1.5.2", + "@teamhanko/hanko-elements": "2.3.0", "defu": "^6.1.4", "jose": "^6.0.0", "ufo": "^1.5.4" diff --git a/playground/pages/about.vue b/playground/pages/about.vue index c288fdb5..b9984822 100644 --- a/playground/pages/about.vue +++ b/playground/pages/about.vue @@ -2,7 +2,7 @@

About

- This page is accesible to both logged in and logged out users. No middleware is defined for + This page is accessible to both logged in and logged out users. No middleware is defined for this page.

diff --git a/playground/pages/index.vue b/playground/pages/index.vue index eb514a7e..57b5b341 100644 --- a/playground/pages/index.vue +++ b/playground/pages/index.vue @@ -4,7 +4,7 @@

Home

- This page is accesible to both logged in and logged out users. No middleware is defined for + This page is accessible to both logged in and logged out users. No middleware is defined for this page. Use the navigation above to navigate to other pages.

diff --git a/playground/pages/login.vue b/playground/pages/login.vue index 55ce65e0..7e4535bd 100644 --- a/playground/pages/login.vue +++ b/playground/pages/login.vue @@ -8,10 +8,13 @@ definePageMeta({

Log in

- Only logged out users can see this page

definePageMeta({
-  middleware: ['hanko-logged-out'],
-})
+ Only logged out users can see this page

+

+    definePageMeta({
+      middleware: ['hanko-logged-out'],
+    })
+      

You were redirected here from {{ $route.query.redirect }}, once you login, you'll be sent back automatically!

diff --git a/playground/pages/protected.vue b/playground/pages/protected.vue index 11a81886..ec1cf788 100644 --- a/playground/pages/protected.vue +++ b/playground/pages/protected.vue @@ -4,7 +4,7 @@ definePageMeta({ }) const hanko = useHanko() function logout() { - hanko!.user.logout() + hanko!.logout() } @@ -12,12 +12,18 @@ function logout() {

Protected Page

- Only logged in users can see this page

definePageMeta({
-  middleware: ['hanko-logged-in'],
-})
+ Only logged in users can see this page

+

+    definePageMeta({
+      middleware: ['hanko-logged-in'],
+    })
+      
+
diff --git a/playground/pages/user.vue b/playground/pages/user.vue index d1739f53..e81f05c9 100644 --- a/playground/pages/user.vue +++ b/playground/pages/user.vue @@ -3,9 +3,9 @@ definePageMeta({ middleware: ['hanko-logged-in'], }) -const hanko = useHanko() +const { $hanko } = useNuxtApp() function logout() { - hanko!.user.logout() + $hanko!.logout() } const result = ref() @@ -20,10 +20,13 @@ async function tryAuthenticatedRequest() {

You are logged in!

- Only logged in users can see this page

definePageMeta({
-  middleware: ['hanko-logged-in'],
-})
+ Only logged in users can see this page

+

+    definePageMeta({
+      middleware: ['hanko-logged-in'],
+    })
+      
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 664cffe8..5e327981 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -16,8 +16,8 @@ importers: specifier: ^4.0.0 version: 4.3.1(magicast@0.5.1) '@teamhanko/hanko-elements': - specifier: 1.5.2 - version: 1.5.2 + specifier: 2.3.0 + version: 2.3.0 defu: specifier: ^6.1.4 version: 6.1.4 @@ -1565,11 +1565,11 @@ packages: peerDependencies: eslint: ^9.0.0 || ^10.0.0 - '@teamhanko/hanko-elements@1.5.2': - resolution: {integrity: sha512-RrOgSoKwHbysDOOffOHn3p8s/zXxY2qvWwZFQdMeEP8Qp/C/RQBwGOzax0Y2gySWflDHM2uu4+tW8dXB+mZfQg==} + '@teamhanko/hanko-elements@2.3.0': + resolution: {integrity: sha512-hMDecxL4gNNxk8mOwWxqaRvguSNPZ9QPrUVcjFP6kwZN3wHj47iD4A+htWFB+VKLWAqyDiioyx/MAM4HhPMsAQ==} - '@teamhanko/hanko-frontend-sdk@1.5.2': - resolution: {integrity: sha512-sSY2NGsJRndrC4HwxBIdog9gOGUzJ3EGKmEn7p+rzXzJaLik3ywJb25DloegPekMZQ8hwOzliF0uJh7kNxFgTA==} + '@teamhanko/hanko-frontend-sdk@2.3.0': + resolution: {integrity: sha512-rZkyWddmozkgpsC0mN+1FuxG2L7OCYsD3D040Njp1o73VyZFpvZ1rbBQv7eOI3V7qZc4rtgKc+fPizM1Hp2pCQ==} '@teamhanko/preact-custom-element@4.2.2': resolution: {integrity: sha512-3djCQr25IxQIgpidxD8J0fTQ7/hMuoa5Nmv7delQc6sX4kXgv9e/pE01cywpmGZLi69H1KUH4dmZ4cPPJQrsQQ==} @@ -5866,15 +5866,15 @@ snapshots: estraverse: 5.3.0 picomatch: 4.0.3 - '@teamhanko/hanko-elements@1.5.2': + '@teamhanko/hanko-elements@2.3.0': dependencies: '@denysvuika/preact-translate': 0.5.1(preact@10.28.3) - '@teamhanko/hanko-frontend-sdk': 1.5.2 + '@teamhanko/hanko-frontend-sdk': 2.3.0 '@teamhanko/preact-custom-element': 4.2.2(preact@10.28.3) classnames: 2.5.1 preact: 10.28.3 - '@teamhanko/hanko-frontend-sdk@1.5.2': + '@teamhanko/hanko-frontend-sdk@2.3.0': dependencies: '@types/js-cookie': 3.0.6 diff --git a/src/module.ts b/src/module.ts index 87823a5d..d9f66bb8 100644 --- a/src/module.ts +++ b/src/module.ts @@ -28,6 +28,8 @@ export interface ModuleOptions { translations?: RegisterOptions['translations'] translationsLocation?: RegisterOptions['translationsLocation'] fallbackLanguage?: RegisterOptions['fallbackLanguage'] + sessionCheckInterval?: RegisterOptions['sessionCheckInterval'] + sessionTokenLocation?: RegisterOptions['sessionTokenLocation'] } } diff --git a/src/runtime/components.d.ts b/src/runtime/components.d.ts index f4ff11c7..69357f37 100644 --- a/src/runtime/components.d.ts +++ b/src/runtime/components.d.ts @@ -7,7 +7,7 @@ declare module 'vue' { * Currently supported values are "en" for English and "de" for German. * If the value is omitted, "en" is used. */ - lang?: 'en' | 'de' | (string & {}) + lang?: 'bn' | 'de' | 'en' | 'fr' | 'it' | 'pt-BR' | 'zh' | (string & {}) /** A space-separated list of experimental features to be enabled. See experimental features. */ experimental?: string }> @@ -16,7 +16,7 @@ declare module 'vue' { * Currently supported values are "en" for English and "de" for German. * If the value is omitted, "en" is used. */ - lang?: 'en' | 'de' | (string & {}) + lang?: 'bn' | 'de' | 'en' | 'fr' | 'it' | 'pt-BR' | 'zh' | (string & {}) }> HankoEvents: DefineComponent> } diff --git a/src/runtime/composables/index.ts b/src/runtime/composables/index.ts index 5dae66cd..10d7d041 100644 --- a/src/runtime/composables/index.ts +++ b/src/runtime/composables/index.ts @@ -1,5 +1,4 @@ -import { Hanko } from '@teamhanko/hanko-elements' -import { useRuntimeConfig } from '#imports' +import { useNuxtApp } from '#app' /** * This composable returns a Hanko instance. @@ -10,12 +9,5 @@ export function useHanko() { if (import.meta.server) { return null } - - const hankoConfig = useRuntimeConfig().public.hanko - return new Hanko(hankoConfig.apiURL, { - cookieName: hankoConfig.cookieName, - localStorageKey: hankoConfig.storageKey, - cookieSameSite: hankoConfig.cookieSameSite, - cookieDomain: hankoConfig.cookieDomain, - }) + return useNuxtApp().$hanko } diff --git a/src/runtime/middleware/logged-in.ts b/src/runtime/middleware/logged-in.ts index bd79eed1..96be4f27 100644 --- a/src/runtime/middleware/logged-in.ts +++ b/src/runtime/middleware/logged-in.ts @@ -1,6 +1,5 @@ import { withQuery } from 'ufo' -import { defineNuxtRouteMiddleware, navigateTo, useRouter, useAppConfig, useHanko, useRequestEvent } from '#imports' -import type {} from 'nuxt/app' +import { defineNuxtRouteMiddleware, navigateTo, useRouter, useAppConfig, useNuxtApp, useRequestEvent } from '#imports' export default defineNuxtRouteMiddleware(async (to) => { const redirects = useAppConfig().hanko.redirects @@ -14,9 +13,9 @@ export default defineNuxtRouteMiddleware(async (to) => { return } - const hanko = useHanko()! + const hanko = useNuxtApp().$hanko! - if (!(await hanko.user.getCurrent().catch(() => null)) && to.path !== redirects.login) { + if (!(await hanko.getUser().catch(() => null)) && to.path !== redirects.login) { return navigateTo(withQuery(redirects.login, { redirect: to.path })) } diff --git a/src/runtime/middleware/logged-out.ts b/src/runtime/middleware/logged-out.ts index e569ca6e..f4947e42 100644 --- a/src/runtime/middleware/logged-out.ts +++ b/src/runtime/middleware/logged-out.ts @@ -1,4 +1,4 @@ -import { defineNuxtRouteMiddleware, navigateTo, useRouter, useAppConfig, useHanko, useRequestEvent } from '#imports' +import { defineNuxtRouteMiddleware, navigateTo, useRouter, useAppConfig, useNuxtApp, useRequestEvent } from '#imports' export default defineNuxtRouteMiddleware(async (to) => { const redirects = useAppConfig().hanko.redirects @@ -12,9 +12,9 @@ export default defineNuxtRouteMiddleware(async (to) => { return } - const hanko = useHanko()! + const hanko = useNuxtApp().$hanko! - if ((await hanko.user.getCurrent().catch(() => null)) && to.path !== redirects.home) { + if ((await hanko.getUser().catch(() => null)) && to.path !== redirects.home) { return navigateTo(redirects.home) } diff --git a/src/runtime/plugins/components.client.ts b/src/runtime/plugins/components.client.ts index 878abb62..8db1162c 100644 --- a/src/runtime/plugins/components.client.ts +++ b/src/runtime/plugins/components.client.ts @@ -1,6 +1,18 @@ -import { register } from '@teamhanko/hanko-elements' +import { Hanko, register } from '@teamhanko/hanko-elements' import { defineNuxtPlugin, useRuntimeConfig } from '#imports' +declare module '#app' { + interface NuxtApp { + $hanko: InstanceType | null + } +} + +declare module 'vue' { + interface ComponentCustomProperties { + $hanko: InstanceType | null + } +} + export default defineNuxtPlugin((nuxtApp) => { const config = useRuntimeConfig() nuxtApp.hook('app:mounted', async () => { @@ -13,4 +25,12 @@ export default defineNuxtPlugin((nuxtApp) => { ...config.public.hanko.components, }) }) + + const hankoConfig = useRuntimeConfig().public.hanko + nuxtApp.provide('hanko', new Hanko(hankoConfig.apiURL, { + cookieName: hankoConfig.cookieName, + localStorageKey: hankoConfig.storageKey, + cookieSameSite: hankoConfig.cookieSameSite, + cookieDomain: hankoConfig.cookieDomain, + })) })