0
  • I've built a react native app that has a Node js backend. Users can sign In, sign up and view a profile page.

  • All my users can sign In but some of them can't view the profile page.

  • When I look at the request made to my backend, I get:

    POST /UserRouter/SignIn 200 212.537 ms - 130342

  • Signing in works, it finds the user, returns the JWT token. When it's in the app no other requests are made. I get JSON Parse error: Unexpected EOF
  • Once you sign in, its supposed to immediately make a request to get your profile. With some accounts, this doesn't happen

My initial hypothesis of this problem is that the token for some users has expired, so they are not able to access protected routes. I use p***assport-jwt*** for my tokens. Hence, the backend not registering any requests.

Please find my code below:

_fetchData = () => {

  AsyncStorage.getItem('jwt', (err, token) => {
    fetch(`${backendUri }/UserRouter/Profile`, {
      method: 'GET',
      headers: {
        Accept: 'application/json',
        Authorization: token
      }
    })
    .then((response) => response.json())
    .then((json) => {
      this.setState({name:json.name})
    })
    .catch((error) => {
      console.log(error)
      alert('There was an error ')
    })
    .done()
  })
}

Here is my node JS code

app.get('/UserRouter/profile', passport.authenticate('jwt1', { session: false }), function (req, res) {
  const token = req.headers.authorization
  const decoded = jwt.decode(token.substring(4), config.secret)
  User.findOne({
    _id: decoded._id
  },
  function (err, user) {
    if (err) throw err

    res.json({ email: user.email, name: user.fName})
  })
})

Thank you

Efe
  • 1
  • 2
  • In the case of an error in `User.findOne`, your server doesn't return anything. The request times out on the client, which I guess causes the EOF error. In the case of an error you need to send back appropriate JSON and handle that on the client-side, for instance by resetting the React state to logged out. –  Dec 13 '18 at 12:13
  • @ChrisG thanks. I'll check it out. Although, that code should only be called when you have signed in. so you should exist. Also, my backend doesn't register that calls are being made to that endpoint for affected users – Efe Dec 13 '18 at 12:37
  • ... it sounds like you're claiming your app is free of bugs, while asking why it doesn't work...? edit: alright, you edited your comment. Well. edit2: ok, how are handling it if passport auth fails? –  Dec 13 '18 at 12:40
  • @ChrisG This is what I'm doing ` User.findOne({id: jwt_payload.sub}, function(err, user) { if (err) { console.log(err) return done(err, false); } if (user) { return done(null, user); } else { return done(null, false); // or you could create a new account } })` – Efe Dec 13 '18 at 16:47
  • Hey @ChrisG I removed passport.authenticate from the API request and my backend still didn't receive the request. What do you think this could be? – Efe Dec 14 '18 at 20:31

1 Answers1

0

This was the answer: https://stackoverflow.com/a/33617414/6542299

Turns out I was encoding my token with the users' document. the users' document was too large. so I just needed to reduce it

Efe
  • 1
  • 2