Search code examples
cluster-computingrestful-authenticationstronglooppm2angular-fullstack

AUTHORIZATION_REQUIRED error when running Strongloop with PM2 in cluster mode


I'm running Strongloop using PM2 as my process manager.

  • pm2 start server/server.js -i 0 (starts strongloop in cluster mode).
  • pm2 list (shows me two instances of server with different PID). pm2 list image example

In my Angular-fullstack application:

  • Run a POST call with email and password to get token, ttl etc. Response was correct: {"id":"z2MgpdN2dyXHHWw2DLhAI026vHBgxw43jpCz9khWa7U9GykAmnQZAaeXV2nvEvQ8","ttl":1209600,"created":"2016-07-21T16:47:34.020Z","userId":"571609e4c88c6db26b75aacd"}
  • Run a GET call using the id (token) to get more information about the user: http://myrestservice.com:3000/api/Users/571609e4c88c6db26b75aacd?access_token=z2MgpdN2dyXHHWw2DLhAI026vHBgxw43jpCz9khWa7U9GykAmnQZAaeXV2nvEvQ8 The response was Error 401 AUTHORIZATION_REQUIRED

Question!!: How can I configure StrongLoop to handle with multiple instances of the same server/server.js ? I read about to configure stored REDIS session, but the guides talks about Express + REDIS stored session management. I think this is a different approach.


Thanks in advance!


Solution

  • It seems you are not persisting your AccessTokens on a common dataSource for all your running servers/processes.

    Try inspecting your model-config.json file and verify the dataSource for the AccessToken model is shared.

    This will probably NOT be shared between all your servers/processes:

    "AccessToken": {
      "dataSource": "memory",
      "public": false
    },
    

    This WILL be shared between all your servers/processes:

    "AccessToken": {
      "dataSource": "mysqlDs",
      "public": false
    },
    

    *Where mysqlDs is stored on MySQL

    This is the relevant section of the Loopback docs to configuring datasources https://docs.strongloop.com/display/public/LB/Attaching+models+to+data+sources#Attachingmodelstodatasources-Makethemodelusethedatasource