Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,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.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion playground/pages/about.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<main>
<h1>About</h1>
<p>
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.
</p>
</main>
Expand Down
2 changes: 1 addition & 1 deletion playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<main>
<h1>Home</h1>
<p>
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.
</p>
</main>
Expand Down
9 changes: 6 additions & 3 deletions playground/pages/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ definePageMeta({
<main>
<h1>Log in</h1>
<p>
Only logged out users can see this page<pre>definePageMeta({
middleware: ['hanko-logged-out'],
})</pre>
Only logged out users can see this page
</p>
<pre><code>
definePageMeta({
middleware: ['hanko-logged-out'],
})
</code></pre>
<p v-if="$route.query.redirect">
You were redirected here from {{ $route.query.redirect }}, once you login, you'll be sent back automatically!
</p>
Expand Down
16 changes: 11 additions & 5 deletions playground/pages/protected.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@ definePageMeta({
})
const hanko = useHanko()
function logout() {
hanko!.user.logout()
hanko!.logout()
}
</script>

<template>
<div>
<h1>Protected Page</h1>
<p>
Only logged in users can see this page<pre>definePageMeta({
middleware: ['hanko-logged-in'],
})</pre>
Only logged in users can see this page
</p>
<pre><code>
definePageMeta({
middleware: ['hanko-logged-in'],
})
</code></pre>
<button @click="logout">
Log me out
Log me out by composable
</button>
<button @click="$hanko!.logout()">
Log me out by provide
</button>
</div>
</template>
13 changes: 8 additions & 5 deletions playground/pages/user.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -20,10 +20,13 @@ async function tryAuthenticatedRequest() {
<main>
<h1>You are logged in!</h1>
<p>
Only logged in users can see this page<pre>definePageMeta({
middleware: ['hanko-logged-in'],
})</pre>
Only logged in users can see this page
</p>
<pre><code>
definePageMeta({
middleware: ['hanko-logged-in'],
})
</code></pre>
<button @click="logout">
Log me out
</button>
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface ModuleOptions {
translations?: RegisterOptions['translations']
translationsLocation?: RegisterOptions['translationsLocation']
fallbackLanguage?: RegisterOptions['fallbackLanguage']
sessionCheckInterval?: RegisterOptions['sessionCheckInterval']
sessionTokenLocation?: RegisterOptions['sessionTokenLocation']
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}>
Expand All @@ -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<Record<string, unknown>>
}
Expand Down
12 changes: 2 additions & 10 deletions src/runtime/composables/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Hanko } from '@teamhanko/hanko-elements'
import { useRuntimeConfig } from '#imports'
import { useNuxtApp } from '#app'

/**
* This composable returns a Hanko instance.
Expand All @@ -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
}
7 changes: 3 additions & 4 deletions src/runtime/middleware/logged-in.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 }))
}

Expand Down
6 changes: 3 additions & 3 deletions src/runtime/middleware/logged-out.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}

Expand Down
22 changes: 21 additions & 1 deletion src/runtime/plugins/components.client.ts
Original file line number Diff line number Diff line change
@@ -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<typeof Hanko> | null
}
}

declare module 'vue' {
interface ComponentCustomProperties {
$hanko: InstanceType<typeof Hanko> | null
}
}

export default defineNuxtPlugin((nuxtApp) => {
const config = useRuntimeConfig()
nuxtApp.hook('app:mounted', async () => {
Expand All @@ -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,
}))
})