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

feat: add cookie config #208

merged 9 commits into from
Jan 13, 2022

Conversation

luke-z
Copy link
Contributor

@luke-z luke-z commented Jan 12, 2022

As this is my first open source pr, I hope I did everything well :)

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

Currently the strapi token is saved as a session cookie which expires as soon as the browser is closed.

Back in v0, there was a config option which allowed to set the expires property.
With this pull request I have added such a feature.

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes (if not applicable, please state why)

@benjamincanac
Copy link
Member

Hi @luke-z,

First of all, thank you for this PR!

Back in the v0 of the module, the expires key was here to tell the module to use sessionStorage instead of cookies. With this new module, we're only using cookies and the default is indeed set as session as stated in the Nuxt 3 useCookie expires documentation, which will behave differently depending on the browser.

To change this behaviour, I would rather only have a cookie key in the options, typed by CookieOptions to configure useCookie composable.

@luke-z
Copy link
Contributor Author

luke-z commented Jan 12, 2022

Hello @benjamincanac

Thank you for the feedback!
It makes more sense to have all the options available.

I changed the config from expires to cookie as you suggested and updated the docs.

@luke-z luke-z changed the title feat: add expiry date to strapi config feat: add cookie config Jan 12, 2022
@@ -1,3 +1,4 @@
import { CookieOptions } from 'nuxt3/dist/app/composables/cookie'
Copy link
Member

@benjamincanac benjamincanac Jan 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielroe To confirm with you, is this the right way to get this type? Shouldn't it be #app/composables/cookie?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried using the type without any import, this results in the build error

error TS4033: Property 'cookie' of exported interface has or is using private name 'CookieOptions'.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With nuxt3 import it resulted into a build error, I've updated it with the #app alias but we still have a warning on build:

WARN  Inlining implicit external #app/composables/cookie                                                                                                                    

'#app/composables/cookie' is imported by src/types/index.ts, but could not be resolved – treating it as an external dependency

I'm gonna wait for Daniel validation on this.

Copy link
Contributor Author

@luke-z luke-z Jan 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put #app in the build.config.ts, which seems to resolve the problem (I am not familiar with unbuild)

externals: ['@nuxt/kit', '@nuxt/schema', 'vue', '#app']

Copy link
Contributor

@danielroe danielroe Jan 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The #app alias won't work in published libraries, unless those library source files are added in the end user's tsconfig.json include array. So you'll need to use 'nuxt3/dist/app/composables/cookie but add nuxt3 to your externals list in your build config.

(This is a unique point for types. For any runtime import of the cookie composable we should import directly from #app or #imports.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @danielroe, it works perfectly!

@luke-z
Copy link
Contributor Author

luke-z commented Jan 12, 2022

Next time I definitely need to go for lint & build, just checking if it works while in dev mode is not a good approach...

Thank you for the improvements & fixes!

const cookie = useCookie<string | null>('strapi_jwt', config.strapi.cookie) looks a lot cleaner :)

@benjamincanac benjamincanac merged commit 0baf7e0 into nuxt-modules:dev Jan 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants