question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

context.passport.getUser() is not working on the client side

See original GitHub issue

Problem: context.passport.getUser() is not working on the client side. When I login using the playground and then run the currentUser query, I am returned the expected results, however, when I try to reproduce this on the client side using react and apollo client, I get back undefined.

Note, my previous ticket had similar issues with getting context.passport.isAuthenticated to work client side. Not sure why this is working now but context.passport.getUser isn’t working.

To Reproduce: for the backend:

  1. Clone this repo/branch
  2. Run yarn
  3. Run yarn build:dev
  4. In a second terminal, run yarn run:dev.
  5. Login using the playground.
mutation {
  login(email: "j@gmail.com", password: "abc") {
    user {
      id
      firstName
      lastName
      email
    }
  }
}
  1. Run the isAuthenticated query. You should get the response of true.
query {
  isAuthenticated
}
  1. Run the currentUser query. You should get back an accurate response.
query{
  currentUser{
    email
    firstName
    lastName
  }
}

for the frontend:

  1. Clone this repo/branch
  2. Run yarn
  3. Run yarn build:dev
  4. Run yarn start
  5. The browser should open to localhost:8080.
  6. Click the ‘login’ button, which will login using the same credentials as the playground.
  7. Click the ‘isAuthenticated’ button, which will confirm that you are authenticated.
  8. (make sure you have the dev console open) Click the ‘Current User’ button, which will perform the current user query. You will see in the console that undefined results are returned.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jkettmanncommented, May 21, 2020

Hey Jimmy. Sorry for missing this. Still don’t get notifications somehow. I’ll try to have a look tomorrow. Thanks for providing an example repo btw

0reactions
jimmy-ecommented, Jun 14, 2020

I just needed to add withCredentials: true to my axios options 🤦‍♂️ apologies, should have probably posted this to stackoverflow instead.

BTW, need to do this for React Suspense. It’s currently in beta but is pretty cool and would recommend checking it out if you’re curious.

import axios from 'axios';

const get = async (gqlString: any): Promise<any> => {
  const body = JSON.stringify({ query: gqlString.loc.source.body });
  const config = {
    headers: { 'Content-Type': 'application/json' },
    withCredentials: true, // THIS FIXED IT
  };
  const response = await axios.post(`http://${process.env.URI}`, body, config);
  return response.data;
};

export default {
  get,
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Node + Express + Passport: req.user Undefined - Stack Overflow
My issue was not specifying to send cookies when using fetch on the client-side. It worked after including the credentials: 'include' field in...
Read more >
How to use getServerSideProps in a HOC for authentication
My problem is that I can find a way to make a HOC on which the ... You should only use "next/router" inside...
Read more >
Simple Passport Local Authentication w/ React & Node.js
This is a simple, easy-to-understand, demystifying video about connecting React with Passport.js and creating a user authentication within a ...
Read more >
Node.js and Express Tutorial: Authentication Using Passport
Learn how to use Passport.js to add user authentication to a web ... The client is server-side rendered using Pug templates styled with...
Read more >
Authentication and authorization - Apollo GraphQL Docs
Try to retrieve a user with the token. 27. const user = await getUser(token);. 28. 29. // Add the user to the context....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found