From 6eaa30fa9f447488a3209993e4e98e4a52cfea2d Mon Sep 17 00:00:00 2001 From: Malo Date: Mon, 24 Feb 2025 14:09:38 +0100 Subject: [PATCH] feat(module): add `token` option to be used as fallback (#459) Co-authored-by: Benjamin Canac --- docs/content/2.setup.md | 1 + docs/content/4.auth.md | 2 +- src/module.ts | 7 +++++++ src/runtime/composables/useStrapiToken.ts | 7 ++++++- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/docs/content/2.setup.md b/docs/content/2.setup.md index d72e385c..1b87fdc9 100644 --- a/docs/content/2.setup.md +++ b/docs/content/2.setup.md @@ -30,6 +30,7 @@ Defaults: ```ts { url: process.env.STRAPI_URL || 'http://localhost:1337', + token: process.env.STRAPI_TOKEN || undefined prefix: '/api', admin: '/admin', version: 'v5', diff --git a/docs/content/4.auth.md b/docs/content/4.auth.md index 4c21fb2b..d09397b6 100644 --- a/docs/content/4.auth.md +++ b/docs/content/4.auth.md @@ -36,7 +36,7 @@ const user = useStrapiUser() ## `useStrapiToken` -This composable is an helper to get the jwt token. It is used internally to get the `strapi_jwt` cookie. +This composable is an helper to get the jwt token. It is used internally to get the `strapi_jwt` cookie. If the latter does not exist, this uses the config variable `token` ```ts const token = useStrapiToken() diff --git a/src/module.ts b/src/module.ts index b075b810..7f2321f9 100644 --- a/src/module.ts +++ b/src/module.ts @@ -18,6 +18,12 @@ export interface ModuleOptions { * @type string */ url?: string + /** + * Strapi API TOKEN + * @default process.env.STRAPI_TOKEN + * @type string + */ + token?: string /** * Strapi Prefix @@ -84,6 +90,7 @@ export default defineNuxtModule({ }, defaults: { url: process.env.STRAPI_URL || 'http://localhost:1337', + token: process.env.STRAPI_TOKEN, prefix: '/api', admin: '/admin', version: 'v5', diff --git a/src/runtime/composables/useStrapiToken.ts b/src/runtime/composables/useStrapiToken.ts index 0d94c60e..04ebcc3d 100644 --- a/src/runtime/composables/useStrapiToken.ts +++ b/src/runtime/composables/useStrapiToken.ts @@ -1,4 +1,4 @@ -import type { Ref } from 'vue' +import { ref, type Ref } from 'vue' import { useCookie, useNuxtApp, useRuntimeConfig } from '#imports' export const useStrapiToken = (): Ref => { @@ -12,5 +12,10 @@ export const useStrapiToken = (): Ref => { const cookie = useCookie(config.strapi.cookieName, config.strapi.cookie) nuxt._cookies[config.strapi.cookieName] = cookie + + if (!cookie.value && config.strapi.token) { + return ref(config.strapi.token) + } + return cookie }