Skip to content

Commit

Permalink
refactor(directus plugin): prefer session over json mode (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
ComfortablyCoding authored Aug 1, 2024
1 parent 28d1125 commit 9247d79
Showing 1 changed file with 5 additions and 39 deletions.
44 changes: 5 additions & 39 deletions modules/directus/runtime/plugins/directus.ts
Original file line number Diff line number Diff line change
@@ -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<Schema> & AuthenticationClient<Schema> = createDirectus<Schema>(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<Schema>(directusURL, { globals: { fetch: $fetch } })
.with(authentication('session', { credentials: 'include' }))
.with(rest({ credentials: 'include' }));

// ** Live Preview Bits **
// Check if we are in preview mode
Expand Down

0 comments on commit 9247d79

Please sign in to comment.