From 9247d79e9ba5091851a2ac792adabf6b58946552 Mon Sep 17 00:00:00 2001 From: daedalus <44623501+ComfortablyCoding@users.noreply.github.com> Date: Thu, 1 Aug 2024 14:29:03 -0400 Subject: [PATCH] refactor(directus plugin): prefer `session` over `json` mode (#103) --- modules/directus/runtime/plugins/directus.ts | 44 +++----------------- 1 file changed, 5 insertions(+), 39 deletions(-) diff --git a/modules/directus/runtime/plugins/directus.ts b/modules/directus/runtime/plugins/directus.ts index c1b2ef5..010b12b 100644 --- a/modules/directus/runtime/plugins/directus.ts +++ b/modules/directus/runtime/plugins/directus.ts @@ -1,50 +1,16 @@ import { createDirectus, rest, authentication } from '@directus/sdk'; -import type { AuthenticationClient, RestClient, AuthenticationStorage, AuthenticationData } from '@directus/sdk'; import type { Schema } from '~/types/schema'; -import { - defineNuxtPlugin, - addRouteMiddleware, - useRuntimeConfig, - useState, - useDirectusAuth, - useRoute, - useNuxtApp, -} from '#imports'; +import { defineNuxtPlugin, useRuntimeConfig, useRoute } from '#imports'; export default defineNuxtPlugin((nuxtApp) => { const route = useRoute(); const config = useRuntimeConfig(); - const directusUrl = config.public.directus.rest.baseUrl as string; + const directusURL = config.public.directus.rest.baseUrl as string; - // We're creating a custom storage class to use the Nuxt so we can use auth on the server and clien - class CookieStorage { - get() { - const cookie = useCookie('directus-auth'); - return cookie.value; - } - - set(value: AuthenticationData) { - const cookie = useCookie('directus-auth'); - cookie.value = value as any; - } - } - - const directus: RestClient & AuthenticationClient = createDirectus(directusUrl, { - globals: { - fetch: $fetch, // We're using the built-in Nuxt $fetch from ofetch - }, - }) - .with(authentication('json', { storage: new CookieStorage() as AuthenticationStorage, credentials: 'include' })) - .with( - rest({ - onRequest: async (request) => { - const userToken = await directus.getToken(); - - return request; - }, - }), - ); + const directus = createDirectus(directusURL, { globals: { fetch: $fetch } }) + .with(authentication('session', { credentials: 'include' })) + .with(rest({ credentials: 'include' })); // ** Live Preview Bits ** // Check if we are in preview mode