From 9c06d8bde92fb990dbe151e50f9a90ebebfa9658 Mon Sep 17 00:00:00 2001 From: manchenkoff Date: Thu, 21 Sep 2023 23:04:43 +0200 Subject: [PATCH 1/3] ignore package info by prettier --- .prettierignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.prettierignore b/.prettierignore index 75c7421..f7d97b6 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,7 +2,7 @@ .nuxt dist node_modules - +package.json playground/.nuxt playground/.output playground/node_modules \ No newline at end of file From 544284a0f5b00a80de3ddb96a79aa5711a2c9351 Mon Sep 17 00:00:00 2001 From: manchenkoff Date: Thu, 21 Sep 2023 23:06:09 +0200 Subject: [PATCH 2/3] ignore changelog --- .prettierignore | 1 + CHANGELOG.md | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.prettierignore b/.prettierignore index f7d97b6..6601033 100644 --- a/.prettierignore +++ b/.prettierignore @@ -3,6 +3,7 @@ dist node_modules package.json +CHANGELOG.md playground/.nuxt playground/.output playground/node_modules \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 7455687..df966f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,5 +3,8 @@ ## v0.0.1 -[compare changes](https://github.com/manchenkoff/nuxt-auth-sanctum/compare/v1.0.1...v0.0.1) +- Initial release +- Added first implementation of the module +- Added readme +- Published NPM package From 32909829f882ce85e060caa54e221f46983e19f0 Mon Sep 17 00:00:00 2001 From: manchenkoff Date: Thu, 21 Sep 2023 23:43:59 +0200 Subject: [PATCH 3/3] made runtime config partial --- src/index.d.ts | 2 +- src/module.ts | 3 +-- src/runtime/composables/useSanctumAuth.ts | 3 ++- src/runtime/composables/useSanctumUser.ts | 8 +++----- src/runtime/httpFactory.ts | 3 ++- src/runtime/middleware/sanctum.auth.ts | 3 ++- src/runtime/middleware/sanctum.guest.ts | 3 ++- src/runtime/plugin.ts | 3 ++- 8 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index a8e637e..7081b99 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -2,6 +2,6 @@ import { SanctumOptions } from './types'; declare module 'nuxt/schema' { interface PublicRuntimeConfig { - sanctum: SanctumOptions; + sanctum: Partial; } } diff --git a/src/module.ts b/src/module.ts index 07e3dfa..8b51fe0 100644 --- a/src/module.ts +++ b/src/module.ts @@ -46,8 +46,7 @@ export default defineNuxtModule({ const resolver = createResolver(import.meta.url); const publicConfig = nuxt.options.runtimeConfig.public; - const userModuleConfig = - publicConfig.sanctum as Partial; + const userModuleConfig = publicConfig.sanctum; nuxt.options.runtimeConfig.public.sanctum = defu( userModuleConfig, diff --git a/src/runtime/composables/useSanctumAuth.ts b/src/runtime/composables/useSanctumAuth.ts index 501be43..d4364e0 100644 --- a/src/runtime/composables/useSanctumAuth.ts +++ b/src/runtime/composables/useSanctumAuth.ts @@ -2,6 +2,7 @@ import { computed } from 'vue'; import { useSanctumClient } from './useSanctumClient'; import { useSanctumUser } from './useSanctumUser'; import { useRoute, useRouter, useRuntimeConfig } from '#app'; +import { SanctumOptions } from '~/src/types'; /** * Provides authentication methods for Laravel Sanctum @@ -12,7 +13,7 @@ export const useSanctumAuth = () => { const user = useSanctumUser(); const client = useSanctumClient(); const router = useRouter(); - const options = useRuntimeConfig().public.sanctum; + const options = useRuntimeConfig().public.sanctum as SanctumOptions; const isAuthenticated = computed(() => { return user.value !== null; diff --git a/src/runtime/composables/useSanctumUser.ts b/src/runtime/composables/useSanctumUser.ts index ace16fc..13dc712 100644 --- a/src/runtime/composables/useSanctumUser.ts +++ b/src/runtime/composables/useSanctumUser.ts @@ -1,12 +1,10 @@ import { useState, useRuntimeConfig } from '#app'; +import { SanctumOptions } from '~/src/types'; export const useSanctumUser = () => { - const config = useRuntimeConfig(); + const options = useRuntimeConfig().public.sanctum as SanctumOptions; - const user = useState( - config.public.sanctum.userStateKey, - () => null - ); + const user = useState(options.userStateKey, () => null); return user; }; diff --git a/src/runtime/httpFactory.ts b/src/runtime/httpFactory.ts index 1ab7c48..130b1f0 100644 --- a/src/runtime/httpFactory.ts +++ b/src/runtime/httpFactory.ts @@ -8,11 +8,12 @@ import { useRouter, useRuntimeConfig, } from '#app'; +import { SanctumOptions } from '../types'; export const SECURE_METHODS = new Set(['post', 'delete', 'put', 'patch']); export function createHttpClient(): $Fetch { - const options = useRuntimeConfig().public.sanctum; + const options = useRuntimeConfig().public.sanctum as SanctumOptions; const event = useRequestEvent(); const router = useRouter(); diff --git a/src/runtime/middleware/sanctum.auth.ts b/src/runtime/middleware/sanctum.auth.ts index 522f1a7..bf1105f 100644 --- a/src/runtime/middleware/sanctum.auth.ts +++ b/src/runtime/middleware/sanctum.auth.ts @@ -6,10 +6,11 @@ import { } from '#app'; import { RouteLocationRaw } from 'vue-router'; import { useSanctumUser } from '../composables/useSanctumUser'; +import { SanctumOptions } from '~/src/types'; export default defineNuxtRouteMiddleware((to) => { const user = useSanctumUser(); - const options = useRuntimeConfig().public.sanctum; + const options = useRuntimeConfig().public.sanctum as SanctumOptions; const isAuthenticated = user.value !== null; diff --git a/src/runtime/middleware/sanctum.guest.ts b/src/runtime/middleware/sanctum.guest.ts index 52905cc..2b718d4 100644 --- a/src/runtime/middleware/sanctum.guest.ts +++ b/src/runtime/middleware/sanctum.guest.ts @@ -4,11 +4,12 @@ import { useRuntimeConfig, createError, } from '#app'; +import { SanctumOptions } from '~/src/types'; import { useSanctumUser } from '../composables/useSanctumUser'; export default defineNuxtRouteMiddleware(() => { const user = useSanctumUser(); - const options = useRuntimeConfig().public.sanctum; + const options = useRuntimeConfig().public.sanctum as SanctumOptions; const isAuthenticated = user.value !== null; diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index 8233e25..f6d23da 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -4,6 +4,7 @@ import { createHttpClient } from './httpFactory'; import { useSanctumUser } from './composables/useSanctumUser'; import sanctumAuth from './middleware/sanctum.auth'; import sanctumGuest from './middleware/sanctum.guest'; +import { SanctumOptions } from '../types'; function handleIdentityLoadError(error: Error) { if ( @@ -21,7 +22,7 @@ export default defineNuxtPlugin(async (nuxtApp) => { const user = useSanctumUser(); const client = createHttpClient(); - const options = nuxtApp.$config.public.sanctum; + const options = nuxtApp.$config.public.sanctum as SanctumOptions; addRouteMiddleware('sanctum:auth', sanctumAuth); addRouteMiddleware('sanctum:guest', sanctumGuest);