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

Argument of type 'ReadonlyRequestCookies' is not assignable to parameter of type 'CookieStore' #840

Open
Barzi-Ahmed opened this issue Sep 2, 2024 · 8 comments

Comments

@Barzi-Ahmed
Copy link

Hello,

Next.js version: 15.0.0-canary.134
iron-session version: 8.0.3

cookies() in the getIronSession parameter, has the following TS error:

Argument of type 'ReadonlyRequestCookies' is not assignable to parameter of type 'CookieStore'.
  Types of property 'set' are incompatible.
    Type '(...args: [key: string, value: string, cookie?: Partial<ResponseCookie>] | [options: ResponseCookie]) => ResponseCookies' is not assignable to type '{ (name: string, value: string, cookie?: Partial<ResponseCookie> | undefined): void; (options: ResponseCookie): void; }'.
      Types of parameters 'args' and 'name' are incompatible.
        Type '[name: string, value: string, cookie?: Partial<ResponseCookie> | undefined]' is not assignable to type '[key: string, value: string, cookie?: Partial<ResponseCookie>] | [options: ResponseCookie]'.
          Type '[name: string, value: string, cookie?: Partial<ResponseCookie> | undefined]' is not assignable to type '[key: string, value: string, cookie?: Partial<ResponseCookie>]'.
            Type at position 2 in source is not compatible with type at position 2 in target.
              Type 'Partial<ResponseCookie> | undefined' is not assignable to type 'Partial<ResponseCookie>'.
                Type 'undefined' is not assignable to type 'Partial<ResponseCookie>'.ts(2345)
(alias) cookies(): ReadonlyRequestCookies
import cookies

This is my code:

'use server'

import { type SessionOptions, getIronSession } from 'iron-session'
import { cookies } from 'next/headers'
import { SESSION_SECRET_KEY } from '@/constants'

export interface SessionData {
    username: string
    isLoggedIn: boolean
    counter: number
}

const sessionOptions: SessionOptions = {
    password: SESSION_SECRET_KEY,
    cookieName: 'session',
    cookieOptions: {
        secure: process.env.NODE_ENV === 'production',
    },
}

export const getServerActionSession = async () => {
    const session = await getIronSession<SessionData>(cookies(), sessionOptions)
    return session
}

export default getServerActionSession
@fitimbytyqi
Copy link

+1

@dsmikeyorke
Copy link

@Barzi-Ahmed we are dealing with this same error. Do you by any chance have react-email installed as well? When we remove this package we no longer see the error but the second we install react-email the error comes back.

@Barzi-Ahmed
Copy link
Author

@dsmikeyorke , no I don't have react-email, and still have the issue. But look, I find that iron-session is not very necessary, as next.js already has a lot of cookie features and its own cookies api, so why use this third party library? I would only make use of sealData and unsealdata functionality, but even for these two I can easily implement my own using web crypto api...

@fitimbytyqi
Copy link

fitimbytyqi commented Sep 24, 2024

@Barzi-Ahmed How would you encrypt and decrypt the user's data coming after login from an external api and set that in a cookie ?

Could you show some code using web crypto api ?

@coronapl
Copy link

This is similar to #690. Installing @types/cookie fixes the problem.

@TomiwaPhilip
Copy link

TomiwaPhilip commented Oct 14, 2024

@Barzi-Ahmed we are dealing with this same error. Do you by any chance have react-email installed as well? When we remove this package we no longer see the error but the second we install react-email the error comes back.

I also have react-email installed but this resolved it

  const cookiesStore: any = cookies();
  
  const session = await getIronSession<SessionData>(cookiesStore, sessionOptions);

Although not really advisable to type it as any, but this is a temporary fix

@dasveloper
Copy link

dasveloper commented Oct 31, 2024

@Barzi-Ahmed we are dealing with this same error. Do you by any chance have react-email installed as well? When we remove this package we no longer see the error but the second we install react-email the error comes back.

I also have react-email installed but this resolved it

  const cookiesStore: any = cookies();
  
  const session = await getIronSession<SessionData>(cookiesStore, sessionOptions);

Although not really advisable to type it as any, but this is a temporary fix

I was able to get it to work without the any by just awaiting the cookies()

const cookieStore = await cookies();
const session = await getIronSession<SessionData>(cookieStore, sessionOptions);

@jimmyhoran
Copy link

@Barzi-Ahmed we are dealing with this same error. Do you by any chance have react-email installed as well? When we remove this package we no longer see the error but the second we install react-email the error comes back.

I also have react-email installed but this resolved it

  const cookiesStore: any = cookies();
  
  const session = await getIronSession<SessionData>(cookiesStore, sessionOptions);

Although not really advisable to type it as any, but this is a temporary fix

I was able to get it to work without the any by just awaiting the cookies()

const cookieStore = await cookies();
const session = await getIronSession<SessionData>(cookieStore, sessionOptions);

@dasveloper is that with or without @types/cookie installed? Only because cookies() is of a non-Promise value - so not sure how you got that to work 🤔

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

No branches or pull requests

7 participants