POST call does not work on mobile Chrome browser (but works fine on Desktop)

52 views Asked by At

I have an Angular application that is deployed to Heroku. The login feature page requires an email and password, which after filling out the user clicks a LOGIN button and my application makes a POST call to my separate backend. My backend is a NodeJS/Express app. The feature works as expected in Desktop. However, it does not work on mobile. I always use Chrome but I've tried it on someone's iPhone Safari browser and the same result.

But here's what happens that I find interesting yet I don't understand why: when the LOGIN button is pushed, the mobile client sends an options call to my backend app, and receives a 204 http status as a response. There's no content but it is successful because my UI app is whitelisted in my backend to make such POST calls. After all, it's been working just fine on desktop. However, it fails to make the actual POST call. As a result, the only thing I can see is the error message below, which I've temporarily rendered my html page for debugging purposes.

{ "headers": { "normalizedNames": {}, "lazyUpdate": null, "headers": {} }, "status": 0, "statusText": "Unknown Error", "url": "https://my-back-end-api-base-url/api/user/login", "ok": false, "name": "HttpErrorResponse", "message": "Http failure response for https://my-back-end-api-base-url/api/user/login: 0 Unknown Error", "error": { "isTrusted": true } }

I have no idea why this happening in mobile. Nowhere in my application does it evaluate the type of device. That wouldn't make much sense. Any ideas?

exports.loginUser = (req, res, next) => {
  console.log('test login feature');
  console.log(req);
  let fetchedUser;
  User.findOne({email: req.body.email})
    .then(user => {
      if (!user) {
        return res.status(401).json({
          message: 'Auth failed.  User does not exist.'
        });
      }
      fetchedUser = user;
      return bcrypt.compare(req.body.password, user.password);
    })
    .then(result => {
      if (!result) {
        return res.status(401).json({
          message: 'Username or password are incorrect.  Please try again.'
        });
      }
      const token = jwt.sign(
        {email: fetchedUser.email, userId: fetchedUser._id},
        `${process.env.JWT_KEY}`,
        { expiresIn: "1h" }
      );
      res.status(200).json({
        token: token,
        expiresIn: 3600,
        userId: fetchedUser._id
      })
    })
    .catch(err => {
      return res.status(401).json({
        message: 'Invalid authentication credentials!'
      });
    });
}
0

There are 0 answers