Skip to content

Commit

Permalink
feat(module): add token option to be used as fallback (#459)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Canac <[email protected]>
  • Loading branch information
MaloLebrin and benjamincanac authored Feb 24, 2025
1 parent c6284e6 commit 6eaa30f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/content/2.setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion docs/content/4.auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -84,6 +90,7 @@ export default defineNuxtModule<ModuleOptions>({
},
defaults: {
url: process.env.STRAPI_URL || 'http://localhost:1337',
token: process.env.STRAPI_TOKEN,
prefix: '/api',
admin: '/admin',
version: 'v5',
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/composables/useStrapiToken.ts
Original file line number Diff line number Diff line change
@@ -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<string | null> => {
Expand All @@ -12,5 +12,10 @@ export const useStrapiToken = (): Ref<string | null> => {

const cookie = useCookie<string | null>(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
}

0 comments on commit 6eaa30f

Please sign in to comment.