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

Unable to get session cookies to work #156

Closed
giacomorebonato opened this issue Feb 4, 2018 · 10 comments
Closed

Unable to get session cookies to work #156

giacomorebonato opened this issue Feb 4, 2018 · 10 comments

Comments

@giacomorebonato
Copy link

giacomorebonato commented Feb 4, 2018

I am trying to save some session data inside of a mutation.
This is how I set the middleware and how I pass the session to the context.

const server = new GraphQLServer({
  context: (req: ContextParameters) => {
    return {
      session: req.request.session
    }
  },
  resolvers,
  typeDefs
})

server.express.use(session({
  cookie: {
    httpOnly: false
  },
  saveUninitialized: true,
  resave: false,
  secret: 'keyboard cat'
}))

I successfully add values to the session object... but later on they are not present on the next requests (I am using the playground).
Is it a problem with my middleware configuration or something more?
In a plain Express JS app this configurations is working correctly.

@schickling
Copy link
Contributor

This definitely seems odd. Would be great to further look into this and ideally write a test for this.

@giacomorebonato
Copy link
Author

I'll try to give a closer look during the week.

@alexneo2003
Copy link

@giacomorebonato hey, bro. did you fix your problem? and how?
i'm have same issues

@alexneo2003
Copy link

Hey, @schickling
i'm forking auth example from examples with MongoDB modify (for DB and sessions via connect-mongo).
after running it's working perfect but when i'm upgrade graphql-yoga up to 1.16.2 v my server is stopped working as expected.
signup/login - working fine
isLogin (sessions) - don't working (sessions in DB exists)

repo with my project
can you check this out? pls

@captDaylight
Copy link

@alexneo2003 I had the same issue from looking at that auth example, I didn't notice these lines essentially renaming request to req:


// context
const context = (req) => ({
  req: req.request,
});

// server
const server = new GraphQLServer({
  typeDefs,
  resolvers,
  context,
});

So, instead of renaming it, I did something similar like this:

const isLoggedIn = (parent, args, { request }) => typeof request.session.user !== 'undefined';

@phongever
Copy link

phongever commented Dec 6, 2018

I have same problem. When I query, I can't get user in session. I also try JWT and it is the same. Does anyone have solution for this problem?
This is my demo
https://glitch.com/edit/#!/verdant-keyboard?path=server.js:22:27

@heyztb
Copy link

heyztb commented Dec 14, 2018

(I am using the playground).

Unfortunately it appears that Playground does not send cookies by default to the server. Setting cookies is fine, but sending them back in requests is not enabled by default because it relies on the server having cors configured properly. See this issue graphql/graphql-playground#470.

I'm not sure how to go about enabling that feature, but I've just accepted that my code works despite things not working in Playground. My tests pass, and other people have the same issue, so I'm content. I hope you all have found peace with this rather annoying feature/bug.

I've heard that GraphiQL works fine for a situation like this, but can't confirm it.

@stale
Copy link

stale bot commented Feb 12, 2019

Due to inactivity of this issue we have marked it stale. It will be closed if no further activity occurs.

@stale stale bot added the status/stale label Feb 12, 2019
@stale
Copy link

stale bot commented Feb 19, 2019

Hey 👋, It seems like this issue has been inactive for some time. In need for maintaining clear overview of the issues concerning the latest version of graphql-yoga we'll close it.
Feel free to reopen it at any time if you believe we should futher discuss its content. 🙂

@stale stale bot closed this as completed Feb 19, 2019
@bakhaa
Copy link

bakhaa commented May 26, 2019

I solved this problem in the onConnect callback function.

const options = {
  cors: { credentials: true, origin },
  port: PORT,
  subscriptions: {
    onConnect: async (connectionParams, webSocket) => {
      try {
        const promise = new Promise((resolve, reject) => {
          session(webSocket.upgradeReq, {}, () => {
            resolve(webSocket.upgradeReq.session.passport);
          });
        });
        const user = await promise;
        return user;
      } catch (error) {
        console.log('error', error);
      }
    },
  },
};

Next, when you initialize the server, you can get the user in context.

const server = new GraphQLServer({
  typeDefs,
  resolvers,
  context: ({ request, connection }) => {
    let user = request ? request.user : null;
    if (connection) {
      if (connection.context.user) user = connection.context.user;
    }
    return { user, request, pubsub };
  },
});

An example can be found here https://github.com/bakhaa/pw/blob/master/api/app.js.

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