Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cookie config #208

Merged
merged 9 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/pages/2.setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ Defaults:
{
url: process.env.STRAPI_URL || 'http://localhost:1337',
prefix: '/api',
version: 'v4'
version: 'v4',
cookie: {},
}
```

Expand All @@ -65,6 +66,12 @@ Prefix of the Strapi server. Only used when version is `v4`.

Version of the Strapi server. Can only be `v4` or `v3`.

### `cookie`

Cookie options of the Strapi token cookie `strapi_jwt`.

> All cookie options can be found in the [Nuxt documentation](https://v3.nuxtjs.org/docs/usage/cookies/#options)

## Edge channel

To use the latest updates pushed on the [`dev`](https://github.com/nuxt-community/strapi-module/tree/dev) branch, you can use `@nuxtjs/strapi-edge`.
Expand Down
6 changes: 4 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export default defineNuxtModule<StrapiOptions>({
defaults: {
url: process.env.STRAPI_URL || 'http://localhost:1337',
prefix: '/api',
version: 'v4'
version: 'v4',
cookie: {}
},
setup (options: StrapiOptions, nuxt) {
// Make sure url is set
Expand All @@ -27,7 +28,8 @@ export default defineNuxtModule<StrapiOptions>({
nuxt.options.publicRuntimeConfig.strapi = defu(nuxt.options.publicRuntimeConfig.strapi, {
url: options.url,
prefix: options.prefix,
version: options.version
version: options.version,
cookie: options.cookie
})

// Transpile runtime
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/composables/useStrapiToken.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { useCookie, useNuxtApp } from '#app'
import { useCookie, useNuxtApp, useRuntimeConfig } from '#app'

export const useStrapiToken = () => {
const nuxtApp = useNuxtApp()
const config = useRuntimeConfig()

nuxtApp._cookies = nuxtApp._cookies || {}
if (nuxtApp._cookies.strapi_jwt) {
return nuxtApp._cookies.strapi_jwt
}

const cookie = useCookie<string | null>('strapi_jwt')
const cookie = useCookie<string | null>('strapi_jwt', config.strapi.cookie)
nuxtApp._cookies.strapi_jwt = cookie
return cookie
}
8 changes: 8 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { CookieOptions } from '#app/composables/cookie'
import type { Ref } from 'vue'

export type StrapiLocale =
Expand Down Expand Up @@ -543,6 +544,13 @@ export interface StrapiOptions {
* @example 'v3'
*/
version?: StrapiOptionsVersion

/**
* Nuxt Cookie Options
* @default {}
* @type CookieOptions
*/
cookie?: CookieOptions
}

export type StrapiUser = object | null
Expand Down
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
"@nuxt/kit"
],
"paths": {
"#app": ["./node_modules/nuxt3/dist/app"]
"#app": [
"./node_modules/nuxt3/dist/app"
],
"#app/*": [
"./node_modules/nuxt3/dist/app/*"
]
}
}
}