• [Rails] API endpoints returning 401 errors

    From Joe Tseng@21:1/5 to All on Wed Mar 1 05:53:25 2017
    I know this issue isn't entirely Ruby-specific, but I'm sure lots of RoR people use Passenger so maybe someone's seen this...

    I'm working on an SPA calling a RESTful API driven by Rails/Passenger/nginx. I did notice that on occasion, despite having already logged into the SPA, if I access a page that called a JWT token-protected endpoint, the API would return a 401 error and I'
    d be logged out. The only way I could get the 401 to go away was if I restarted nginx.

    After some googling, I found this: https://www.phusionpassenger.com/library/config/nginx/reference/#passenger_pool_idle_time
    And I thought the solution was to assign the value to 0 to allow for no timing out. I went ahead and did this and restarted the service to make the value take effect, but the 401 error came back again after a bit of idle time.

    I also upped the idle time to 86400 seconds to see if that has any effect, but my API still fails after ~5 minutes. Has anyone seen this, and what did you do to solve the issue?

    Thanks,

    - Joe

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Joe Tseng@21:1/5 to Joe Tseng on Thu Mar 2 05:06:02 2017
    On Wednesday, March 1, 2017 at 8:53:36 AM UTC-5, Joe Tseng wrote:
    I know this issue isn't entirely Ruby-specific, but I'm sure lots of RoR people use Passenger so maybe someone's seen this...

    I'm working on an SPA calling a RESTful API driven by Rails/Passenger/nginx. I did notice that on occasion, despite having already logged into the SPA, if I access a page that called a JWT token-protected endpoint, the API would return a 401 error and
    I'd be logged out. The only way I could get the 401 to go away was if I restarted nginx.

    After some googling, I found this: https://www.phusionpassenger.com/library/config/nginx/reference/#passenger_pool_idle_time
    And I thought the solution was to assign the value to 0 to allow for no timing out. I went ahead and did this and restarted the service to make the value take effect, but the 401 error came back again after a bit of idle time.

    I also upped the idle time to 86400 seconds to see if that has any effect, but my API still fails after ~5 minutes. Has anyone seen this, and what did you do to solve the issue?

    Thanks,

    - Joe

    Chalk this one up to a rookie mistake... When I first started this application I wanted to try and parameterize some values so I could change them at a future date without having to dig through source code. I didn't realize that that value (eg 15.minutes.
    from_now) in the variable was set when the app first starts and stays the same for the lifetime of the run.

    So I directly set that value as my JWT exp value and that would explain why everything would work for a few minutes every time I restarted my server.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Joe Tseng@21:1/5 to Joe Tseng on Thu Mar 2 05:02:46 2017
    After a good bit tracing tonight, turns out the problem was THIS GUY.

    In the link I referenced earlier, the example files are very close to production-ready, but there's some customization/tweaking that needs to be done. I noticed in authenticate_user.rb, I had to provide an email and password and replace the generic User
    class with my own class (in .user()); I updated that and was able to log in and that was that - or so I thought... I wasn't able to get the API to return a valid token in my response header. In a case of tl;dr, it turns out I also needed to make the same
    customization and replace User in authorize_api_request.rb as well (in .user()).

    On Wednesday, March 1, 2017 at 8:53:36 AM UTC-5, Joe Tseng wrote:
    I know this issue isn't entirely Ruby-specific, but I'm sure lots of RoR people use Passenger so maybe someone's seen this...

    I'm working on an SPA calling a RESTful API driven by Rails/Passenger/nginx. I did notice that on occasion, despite having already logged into the SPA, if I access a page that called a JWT token-protected endpoint, the API would return a 401 error and
    I'd be logged out. The only way I could get the 401 to go away was if I restarted nginx.

    After some googling, I found this: https://www.phusionpassenger.com/library/config/nginx/reference/#passenger_pool_idle_time
    And I thought the solution was to assign the value to 0 to allow for no timing out. I went ahead and did this and restarted the service to make the value take effect, but the 401 error came back again after a bit of idle time.

    I also upped the idle time to 86400 seconds to see if that has any effect, but my API still fails after ~5 minutes. Has anyone seen this, and what did you do to solve the issue?

    Thanks,

    - Joe

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)