Skip to content

Commit

Permalink
fix: fix failed to find "useDirectusSession"
Browse files Browse the repository at this point in the history
close #72
  • Loading branch information
becem-gharbi committed Feb 20, 2024
1 parent d0953a6 commit 93dcc98
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default defineNuxtModule<ModuleOptions>({
// ################# Rest setup #################
// ##############################################

const restPlugin = resolve(runtimeDir, './plugins/rest')
const restPlugin = resolve(runtimeDir, options.auth.enabled ? './plugins/rest' : './plugins/rest.basic')
addPlugin(restPlugin, { append: true })

addImports({
Expand Down Expand Up @@ -174,8 +174,10 @@ export default defineNuxtModule<ModuleOptions>({
if (options.graphql.enabled) {
nuxt.options.build.transpile.push('@vue/apollo-composable')

const graphqlPlugin = resolve(runtimeDir, './plugins/graphql')
addPlugin(graphqlPlugin, { append: true })
if (options.auth.enabled) {
const graphqlPlugin = resolve(runtimeDir, './plugins/graphql')
addPlugin(graphqlPlugin, { append: true })
}

await installModule('nuxt-apollo', {
httpEndpoint: options.graphql.httpEndpoint,
Expand Down
26 changes: 26 additions & 0 deletions src/runtime/plugins/rest.basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createDirectus, rest } from '@directus/sdk'
import type { PublicConfig } from '../types'
import {
defineNuxtPlugin,
useRuntimeConfig
} from '#imports'

export default defineNuxtPlugin(() => {
const config = useRuntimeConfig().public.directus as PublicConfig

const directus = createDirectus<DirectusSchema>(config.rest.baseUrl, {
globals: {
fetch: $fetch
}
})

const restClient = directus.with(rest())

return {
provide: {
directus: {
rest: restClient
}
}
}
})
12 changes: 5 additions & 7 deletions src/runtime/plugins/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ export default defineNuxtPlugin(() => {
const restClient = directus.with(
rest({
onRequest: async (request) => {
if (config.auth.enabled) {
const accessToken = await useDirectusSession().getToken()
const accessToken = await useDirectusSession().getToken()

if (accessToken) {
request.headers = {
...request.headers,
authorization: `Bearer ${accessToken}`
}
if (accessToken) {
request.headers = {
...request.headers,
authorization: `Bearer ${accessToken}`
}
}

Expand Down

0 comments on commit 93dcc98

Please sign in to comment.