sidebase / nuxt-session

Nuxt session middleware to get a persistent session per app user, e.g., to store data across multiple requests. The nuxt session module provides the useSession() composable out of the box and sets up API endpoints to interact with your session to make working with sessions feel like a breeze.
https://sidebase.io/nuxt-session/
MIT License
189 stars 19 forks source link

fix: allow unstorage drivers other than 'memory' #7

Closed Twitch0125 closed 1 year ago

Twitch0125 commented 1 year ago

Closes #8

Contributes to nuxt/nuxt.js#11734

Fixed by using Nitro's storage layer. The storageOptions were getting stored in the runtime config, but that looks like it does some kind of serializing. I had this config:

session: {
    session: {
      cookieSameSite: 'strict',
      storageOptions: {
        driver: redisDriver({
          base: 'session:',
          url: process.env.REDIS_URL || 'redis://localhost:6379',
        }),
      },
    },
  },

but was getting a " fn is not a function" error. When I checked the storageOptions in storage.ts, the driver value was getting set to {}.

So now the equivalent config would look like this:

  session: {
    session: {
      storageOptions: {
        driver: 'redis',
        url: process.env.REDIS_URL || 'redis://localhost:6379'
      }
    }
  }

Also, in storage.ts, Typescript is yelling at me saying useStorage isn't exported from '#imports'. I'm not sure where that actually gets exported if not from there.

BracketJohn commented 1 year ago

Btw, I opened https://github.com/nuxt/nuxt.js/issues/15237 to see if this is a feature or a bug.

Twitch0125 commented 1 year ago

@BracketJohn Well, I'm stumped. I'm trying to find a way to provide a useSessionStorage function that works similar to useStorage but with a separate unstorage instance. Initially I was trying to do that with a Nitro plugin, but there's very little documentation on what exactly you can do with those, and how you can add one from a nuxt module.

Looking at the source code for Nitro's useStorage (which I believe is here https://github.com/unjs/nitro/blob/main/src/rollup/plugins/storage.ts), I feel like I'm missing something because surely we don't have to do this whole virtual module thing that nitro is doing, with a nuxt module...

BracketJohn commented 1 year ago

@Twitch0125 I see, then we probably should switch to an approach where we provide: driver-name + options and then use the driver name to instantiate the right storage backend at runtime inside storage.ts?

Twitch0125 commented 1 year ago

After getting some help from the nuxt discord, I was able to figure this out. nuxt-session now has its own unstorage instance completely separate from nitro's

BracketJohn commented 1 year ago

Yay, this is great - thanks for keeping on pushing!

Can you by resolve the remaining test problems? Right now it's types:

image

Other than that: @valiafetisov will review the next week and then publish once merged (:

Twitch0125 commented 1 year ago

I was over complicating by creating a virtual module for the session config instead of using useRuntimeConfig. I ended up adding a @ts-ignore to where I'm using the other virtual module. I tried creating a type for it with declare module '#session-driver' but that didn't seem to be fixing it. But I'm still fairly noobish when it comes to typescript.

valiafetisov commented 1 year ago

Can you please resolve conflicts? Then its ready to be merged!

Twitch0125 commented 1 year ago

@valiafetisov resolved!

BracketJohn commented 1 year ago

Thanks @Twitch0125 for the contribution - it was awesome getting this in, I'll do the release shortly.

BracketJohn commented 1 year ago

alright @Twitch0125 the fix is now available at @sidebase/nuxt-session@0.2.4, see the release notes here: https://github.com/sidebase/nuxt-session/releases/tag/0.2.4