title: unable to set JSON object as cookie in nextjs using js-cookie
body: <p>i am trying to set some user data along with token ID in Cookie. Token is set perfectly and also in console.log output but my user JSON object is unable to set and shows me &quot;object Object&quot; as Cookies but my user data present in console.log. i think i am unable to convert JSON data into cookie. here is my code of nextjs and packages i use for this.</p>
  <p>package use</p>
  <ol>
  <li>js-cookies</li>
  <li>nextjs - latest version</li>
  </ol>
  <p>here is my API for user login</p>
  <pre><code>import db from &quot;../../helpers/initDB&quot;;
  import User from &quot;../../models/User&quot;;
  import bcrypt from 'bcryptjs'
  import jwt from 'jsonwebtoken'
  
  db.connect();
  
  // eslint-disable-next-line import/no-anonymous-default-export
  export default async (req,res)=&gt;{
   const {email,password} = req.body
   try{
      if(!email || !password){
        return res.status(422).json({error:&quot;All Fields Are Required&quot;})
      }
    const user = await User.findOne({email})
    if(!user){
        return res.status(404).json({error:&quot;user dont exists with that email&quot;})
    }
      const doMatch =  await bcrypt.compare(password,user.password)
      if(doMatch){
         const token =  jwt.sign({userId:user._id},process.env.JWT_SECRET,{
              expiresIn:&quot;7d&quot;
          })
          const {name,role,email} = user
          res.status(201).json({token,user:{name,role,email}})
      }else{
         return res.status(401).json({error:&quot;email or password dont match&quot;})
      }
   }catch(err){
       console.log(err)
   }
  }
  
  db.disconnect();
  </code></pre>
  <p>my api is working fine and set the res.status with all my data.</p>
  <p>here is code part where i am trying to set cookie</p>
  <pre><code>console.log(res2)
    Cookie.set('user', res2.user);
    Cookie.set('token', res2.token);
    router.push('/dashboard/')
  </code></pre>
  <p>and here is my console.log output where user data and token is present</p>
  <pre><code> Object { token:&quot;eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2MWE4YzcwMGIxZmI3OWJmOGNjOWY3ZjUiLCJpYXQiOjE2Mzg1MTczODIsImV4cCI6MTYzOTEyMjE4Mn0.9T-B4c3xtsgCSGSWUMCZ_GU56FYC5VLeVDhGzAWiwjA&quot;, user: {…} }
  </code></pre>
  <p>​
  user: Object { name: &quot;mohit nagar&quot;, role: &quot;user&quot;, email: &quot;gujjarmaxx@gmail.com&quot; }
  ​
  : Object { … }</p>
  <p>and here is my cookies</p>
  <p>{
  &quot;Request Cookies&quot;: {
  &quot;token&quot;: &quot;eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2MWE4YzcwMGIxZmI3OWJmOGNjOWY3ZjUiLCJpYXQiOjE2Mzg1MTY1NzcsImV4cCI6MTYzOTEyMTM3N30.PoBD03Qp-7-iN5yvnjvQfflTI5DO8z3Lqk3sMYZs0Y0&quot;,
  &quot;user&quot;: &quot;[object Object]&quot;
  }
  }</p>
  <p>i don't know what i am doing wrong.</p>