Directus is an instant Headless CMS that provides a flexible REST and GraphQL API for your SQL database.
The @ginjou/with-directus package provides the necessary providers to integrate Ginjou with a Directus backend, enabling seamless data management and authentication.
Install the Directus provider along with the official Directus SDK:
pnpm add @ginjou/with-directus @directus/sdk
yarn add @ginjou/with-directus @directus/sdk
npm install @ginjou/with-directus @directus/sdk
bun add @ginjou/with-directus @directus/sdk
To use the providers in your application, register them using the configuration hooks in your root component.
In a Vue application, use defineFetchersContext and defineAuthContext within your App.vue setup.
<script setup lang="ts">
import { authentication, createDirectus, rest } from '@directus/sdk'
import { defineAuthContext, defineFetchersContext } from '@ginjou/vue'
import { createAuth, createFetcher } from '@ginjou/with-directus'
const client = createDirectus('https://your-directus-url.com')
.with(rest())
.with(authentication())
defineFetchersContext({
default: createFetcher({ client })
})
defineAuthContext(createAuth({ client }))
</script>
<template>
<RouterView />
</template>
For Nuxt applications, register the providers in your root app.vue component.
<script setup lang="ts">
import { authentication, createDirectus, rest } from '@directus/sdk'
import { defineAuthContext, defineFetchersContext } from '@ginjou/vue'
import { createAuth, createFetcher } from '@ginjou/with-directus'
const client = createDirectus('https://your-directus-url.com')
.with(rest())
.with(authentication())
defineFetchersContext({
default: createFetcher({ client })
})
defineAuthContext(createAuth({ client }))
</script>
<template>
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</template>
The createFetcher function maps Ginjou data hooks to Directus API requests.
By default, the resource name maps to the corresponding Directus collection.
// Requests /items/posts
const { data } = useList({ resource: 'posts' })
meta.query object is passed directly to the @directus/sdk rest methods, allowing you to use any standard Directus query parameters.const { data } = useList({
resource: 'posts',
meta: {
query: {
fields: ['*', 'author.*'],
sort: ['-date_created'],
}
}
})
directus_ or directus/ are mapped to their respective system endpoints:directus_users maps to /usersdirectus_files maps to /filesdirectus_roles maps to /rolesThe createAuth function facilitates integration with Directus authentication services.
The provider supports both standard password-based authentication and Single Sign-On (SSO).
const { login } = useAuth()
// Password Authentication
await login({
type: 'password',
params: {
email: 'admin@example.com',
password: 'password',
},
})
// SSO Authentication
await login({
type: 'sso',
params: {
provider: 'google',
options: {
redirect: 'https://your-app.com/callback',
},
},
})
/users/me endpoint to fetch the full user profile.const { data: user } = useIdentity()
The Auth provider handles session verification and termination:
useAuth().check(): Verifies the existence of a valid authentication token.useAuth().logout(): Terminates the current session and revokes the token.