Next-Auth | When user object have too many items then session request is without data

Viewed 2187

I will try to explain my problem as best as possible for me.

I'm using Strapi as a backend and Nextjs as a frontend.

For the authentication I using NextAuth.

[...nextauth].js:

const options = {
  providers: [
    Providers.Credentials({
      name: 'Credentials',
      credentials: {
        username: { label: "Email", type: "email", placeholder: "jsmith" },
        password: { label: "Password", type: "password" }
      },
      authorize: async (credentials) => {
        try {
          const user = await axios.post(`${process.env.NEXT_PUBLIC_API_URL}/auth/local`, {
            identifier: credentials.username,
            password: credentials.password,
          });
          if (user.data) {
            return user.data
          } else {
            return null
          }
        } catch (error) {
          const errorMessage = error.response.data.message[0].messages[0].message
          throw new Error(errorMessage)
        }
      }
    }),
  ],
  database: process.env.NEXT_PUBLIC_DATABASE_URL,
  session: {
    jwt: true,
  },
  callbacks: {
    jwt: async (token, user) => {
      if (user) {
        token.jwt = user.jwt;
        token.user = user.user;
      }
      return Promise.resolve(token);
    },
    session: async (session, token) => {
      session.jwt = token.jwt;
      session.user = token.user;
      return Promise.resolve(session);
    },
  },
  pages: {
    signIn: '/login',
    error: '/login'
  },
};

const Auth = (req, res) =>
  NextAuth(req, res, options);

export default Auth;

When I send form with identifier and password I getting session response with user data and jwt.

Everything working well, but if user objects have few more objects assigned to him, then something goes wrong and the session is empty.

Example: I creating in Strapi simple collection with just two fields - image field, and owner (relation to user). Every response with image contents few lines:

"photo": {
            "_id": "60ca03fa20bd43033a53950e",
            "name": "Zrzut ekranu 2021-06-16 o 14.59.48.png",
            "alternativeText": "",
            "caption": "",
            "hash": "Zrzut_ekranu_2021_06_16_o_14_59_48_2bc223567b",
            "ext": ".png",
            "mime": "image/png",
            "size": 170.69,
            "width": 668,
            "height": 636,
            "url": "/uploads/Zrzut_ekranu_2021_06_16_o_14_59_48_2bc223567b.png",
            "formats": {
                "thumbnail": {
                    "name": "thumbnail_Zrzut ekranu 2021-06-16 o 14.59.48.png",
                    "hash": "thumbnail_Zrzut_ekranu_2021_06_16_o_14_59_48_2bc223567b",
                    "ext": ".png",
                    "mime": "image/png",
                    "width": 164,
                    "height": 156,
                    "size": 14.92,
                    "path": null,
                    "url": "/uploads/thumbnail_Zrzut_ekranu_2021_06_16_o_14_59_48_2bc223567b.png"
                },
                "small": {
                    "name": "small_Zrzut ekranu 2021-06-16 o 14.59.48.png",
                    "hash": "small_Zrzut_ekranu_2021_06_16_o_14_59_48_2bc223567b",
                    "ext": ".png",
                    "mime": "image/png",
                    "width": 500,
                    "height": 476,
                    "size": 121.79,
                    "path": null,
                    "url": "/uploads/small_Zrzut_ekranu_2021_06_16_o_14_59_48_2bc223567b.png"
                }
            },

When I add 1 or 2 items with image and try to login to user who own that items then everything is fine, I can log in and receive full user object and jwt token.

But when I add 3 items there, session object is empty, and I can't log in. I do not receiving user object and jwt token.

With 3 items With 2 items

When I using postman, even with 50 items response is good and include full user object and jwt token.

I think is because response is too large or something like that, but I have no clue how to deal with it.

I working on localhost (both - frontend and backend) on MacOS.

Is there anyone who can help me find a solution for that problem?

Kind Regards, GP

1 Answers

The reason of above issue is that in api response is passed to many data, and next-auth store that all data in JWT, and JWT is stored in full in cookie, cookie is limited to 4096 bytes, and that simply brake that limit and response.

The simplest way to sort it out is to save only most important data in JWT.

You can do that in callback function.

Related