-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
directus.ts
70 lines (59 loc) · 1.79 KB
/
directus.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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';
export default defineNuxtPlugin((nuxtApp) => {
const route = useRoute();
const config = useRuntimeConfig();
const directusUrl = config.public.directus.rest.baseUrl as string;
const { isTokenExpired } = useDirectusAuth();
// 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;
},
}),
);
// ** Live Preview Bits **
// Check if we are in preview mode
const preview = route.query.preview && route.query.preview === 'true';
const token = route.query.token as string | undefined;
// If we are in preview mode, we need to use the token from the query string
if (preview && token) {
directus.setToken(token);
nuxtApp.hook('page:finish', () => {
refreshNuxtData();
});
}
return {
provide: {
directus,
},
};
});