0

I have Apollo server mutation for my login:

// resolvers.js

  loginUser: async (root, args, context) => {
    const currentUser = await prisma.user.findUnique({
      where: {email: String(args.email)},
    });

    if (currentUser) {
      const token = jwt.sign(currentUser, 'supersecret');
      console.log('resolvers.js :', token);
      context.cookies.set('auth-token', token, {
        httpOnly: true,
        sameSite: 'lax',
        maxAge: 6 * 6 * 60,
      });
    }

    return currentUser;
  },

This works. It returns the currentUser and it places a JWT in the cookies:

index.js:  undefined
resolvers.js : eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZW1haWwiOiJwZXRlckBjbGVhbnBpeGVsLm5sIiwibmFtZSI6IlBldGVyIEJvb21zbWEiLCJwYXNzd29yZCI6IiQyYiQwNCREcW80TE4zUzVaT25rMVQxTXlhdE91aWpidkhwbDI4MW9EQ2hIZEV1MVpmRU95azdtakxxdSIsImlhdCI6MTYzMDU4MTgwNX0.GDbhFDVaVQaZVRZc7w7XUrCNffy2D0XTunEQ95Tl***

enter image description here

But in my Apollo server initialization I can't get the cookie:

// index.js

const server = new ApolloServer({resolvers, typeDefs,
  context: ({req, res}) => {
    prisma;
    const cookies = new Cookies(req, res);
    const token = cookies.get('auth-token');
    console.log('index.js: ', token);
    const user = verifyToken(token);
    return {
      cookies,
      user,
    };
  },
});

When I do do something, for example add a movie to my database I get an:

index.js: undefined

Peter Boomsma
  • 8,851
  • 16
  • 93
  • 185
  • i thought you have to wait for another round? ex. you set cookie on client, the client has it, how does the server get to know you have a cookie change? Via the next http call. This is how I understand it. – windmaomao Sep 02 '21 at 11:28
  • Problem is that I can't get the cookie to pass it to the server, because in the `context` the `cookies.get('auth-token)` is always undefined. – Peter Boomsma Sep 02 '21 at 11:33
  • ok, this is all client code, if that's the case, you just make sure you run this code after you set the cookie. Try this if you Cookie class isn't working. https://stackoverflow.com/questions/14573223/set-cookie-and-get-cookie-with-javascript – windmaomao Sep 02 '21 at 11:37

0 Answers0