Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
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
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
9 changes: 6 additions & 3 deletions playground/pages/protected.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ function logout() {
<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
</button>
Expand Down
9 changes: 6 additions & 3 deletions playground/pages/user.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 13 additions & 8 deletions src/runtime/composables/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Hanko } from '@teamhanko/hanko-elements'
import { useRuntimeConfig } from '#imports'
import { useRuntimeConfig, useState, toValue } from '#imports'

/**
* This composable returns a Hanko instance.
Expand All @@ -10,12 +10,17 @@ 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,
})

const hanko = useState(
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should probably use nuxtApp.provide('hanko', hankoInstance) (and then nuxtApp.$hanko) rather than useState as state is really meant to be for data that could be passed from server -> client.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I agree.
I've changed it to a provide from within the client plugin and added the types there as well.
My second commit is updating the documentation and inner usage of the provided $hanko instead of useHanko()
I kept useHanko alive because of compatibility.
I hope you agree on this.
Thanks for the catch.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would rather keep useHanko (and use everywhere instead of nuxtApp.$hanko) but we can return nuxtApp.$hanko inside it.

Copy link
Author

Choose a reason for hiding this comment

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

Fair enough, I reverted it within the src folder

'single-hanko-instance',
() =>
new Hanko(hankoConfig.apiURL, {
cookieName: hankoConfig.cookieName,
localStorageKey: hankoConfig.storageKey,
cookieSameSite: hankoConfig.cookieSameSite,
cookieDomain: hankoConfig.cookieDomain,
}),
)
return toValue(hanko)
}
3 changes: 1 addition & 2 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'

export default defineNuxtRouteMiddleware(async (to) => {
const redirects = useAppConfig().hanko.redirects
Expand All @@ -16,7 +15,7 @@ export default defineNuxtRouteMiddleware(async (to) => {

const hanko = useHanko()!

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
2 changes: 1 addition & 1 deletion src/runtime/middleware/logged-out.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default defineNuxtRouteMiddleware(async (to) => {

const hanko = useHanko()!

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