iron-session icon indicating copy to clipboard operation
iron-session copied to clipboard

Iron session with graphql

Open ssakib4040 opened this issue 2 years ago • 7 comments

Hi, Is it possible to use iron-session with graphql apollo server?

ssakib4040 avatar Oct 13 '22 17:10 ssakib4040

Also very interested in using this with @apollo/client

everdrone avatar Oct 27 '22 10:10 everdrone

Also been trying to figure this out 🤔

justlevine avatar Oct 21 '23 08:10 justlevine

Any pointers/suggestions on how to use @apollo/client with the V8 update for Server Actions

jorgev259 avatar Nov 05 '23 20:11 jorgev259

Hey there, can you provide specific details of what you're trying to achieve, what worked and what failed? Thanks!

vvo avatar Nov 20 '23 00:11 vvo

Will try the new release first and will get back to you! It has been a couple of days since ive tried it so i dont remember the finer details.

jorgev259 avatar Nov 20 '23 00:11 jorgev259

This is on the new V8 release my current setup is a /src/api/graphql route as follows:

import { cookies } from 'next/header'
import { ApolloServer } from '@apollo/server'
import { startServerAndCreateNextHandler } from '@as-integrations/next'
import { getIronSession } from 'iron-session'

import db from '@/next/server/sequelize/startDB'
import sessionOptions from '@/next/lib/sessionOptions'
import { schema } from '@/next/lib/graphql'

const server = new ApolloServer({  schema,  introspection: process.env.NODE_ENV !== 'production' })

const handler = startServerAndCreateNextHandler(server, {
  context: async (req, res) => {
    const session = await getIronSession(cookies(), sessionOptions)
    const { username } = session
    const user = username && await db.models.user.findByPk(username)

    return { username, user, session }
  }
})

export { handler as GET, handler as POST }

a /lib/getSession file:

'use server'
import { getIronSession } from 'iron-session'
import { cookies } from 'next/headers'

import sessionOptions from './sessionOptions'

const getSession = getIronSession(cookies(), sessionOptions)

export default getSession

and when using it on an action like this:

export async function login (formData) {
  try {
    const session = await getSession()
    const client = await getClient()

    const username = formData.get('username')
    const password = formData.get('password')

    const user = await db.models.user.findByPk(username)
    if (!user) throw loginError

    const valid = await bcrypt.compare(password, user.password)
    if (!valid) throw loginError

    session.username = user.username
    await session.save()
    client.resetStore()

    return { ok: true }
  } catch (err) {
    return { ok: false, message: err.message }
  }
}

The following error happens on dev mode page build: imagen

jorgev259 avatar Nov 20 '23 02:11 jorgev259

Hey @jorgev259 you can see a live example of using cookies() with iron session here: https://get-iron-session.vercel.app/app-router-server-component-and-action. Open a different issue if this is unrelated to graphql. Thanks!

I believe you issue is that you call cookies() too early here.

vvo avatar Nov 20 '23 08:11 vvo

I am gonna close this for now. If you happen to try to use iron-session with any GraphQL tooling and it fails because of how iron-session is being written, please open a new issue with a reproducible repository where I can have a look. Thanks!

vvo avatar Jun 14 '24 07:06 vvo