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

Configuring redis session #72

Open gjedlicska opened 1 year ago

gjedlicska commented 1 year ago

Ask your question

Should the working example below be the officially recommended way of configuring a nuxt-session?

Additional information

Using the example below based on the docs for configuring the redis session storage is partially broken / misleading.

// file: ~/nuxt.config.ts
export default defineNuxtConfig({
  modules: ['@sidebase/nuxt-session'],
  session: {
    session: {
      storageOptions: {
        driver: 'redis',
        options: {
          url: process.env['REDIS_URL']
        }
      }
    }
  }
})

It is working well in a local dev setup, but breaks, when the app is built into a docker container. Most probably, because at build time, we deliberately do not supply the value of the REDIS_URL env var, the storage driver falls back to its default value.

I've found, that the config is actually read via nuxt's useRuntimeConfig function and supplying the config below, makes it work in both local and prod setups.

export default defineNuxtConfig({
  modules: ['@sidebase/nuxt-session'],
  runtimeConfig: {
    session: {
      session: {
        storageOptions: {
          driver: 'redis',
          options: {
            url: () => process.env['REDIS_URL']
          }
        }
      }
    }
  }
})
odranoelBR commented 1 year ago

Hi @gjedlicska, i tried this runtimeConfig for build production and didn't work. My attempt commands:

yarn build
export REDIS_URL=redis://localhost:6380 (intentional changed port)
yarn preview

He don't log the error message "can't connect ..." but save nothing on redis.

Tks

luukgruijs commented 12 months ago

@gjedlicska i created this issue two weeks ago: #82 . I can't seem to figure this out. Would you have any ideas?

gjedlicska commented 12 months ago

@luukgruijs Turns out, if you only set the env var NUXT_SESSION_SESSION_STORAGE_OPTIONS_OPTIONS_URL , what should be the full url of the redis instance, you do not need to configure anything to your nuxt.config.ts file.

That's what I've started using.

gjedlicska commented 8 months ago

To provide a full example for anyone still struggling with this:

{
  modules: ['@sidebase/nuxt-session'],
  // the build time config is needed, so that the nuxt module selects the right driver module during build 
  session: {
    session: {
      storageOptions: {
        driver: 'redis',
        options: {
        }
      }
    }
  },
  // the runtime config is needed, to specify the shape of the config object.
  // a dummy value for the url, that can be overridden by nuxt's runtime env var system
  // https://nuxt.com/docs/guide/going-further/runtime-config#environment-variables
  runtimeConfig: {
    session: {
      session: {
        storageOptions: {
          driver: 'redis',
          options: {
            // overriden by NUXT_SESSION_SESSION_STORAGE_OPTIONS_OPTIONS_URL env var
            url: 'UNDEFINED' // you need to define the key, with a dummy value
          }
        }
      }
    }
  }
}

I'm adding a PR to update the readme with this.