Sails: GET requests automatically redirected in staging env

Created on 27 Jul 2018  ·  7Comments  ·  Source: balderdashy/sails

Sails version: 1.0.2
Node version: 8.10.0
NPM version: 6.1.0
DB adapter name: sails-postgresql
DB adapter version: 1.0.1
Operating system: Heroku servers




Hi, Im facing an issue while trying to handle a GET request on staging environement. (_It works fine in dev mode and also when I dockerize the apps on my dev server_). I've been hard searching for a solution since a Week but did not find anything.

Context

I'm implementing two apps on Heroku exchanging by HTTPs requests.

  • Backend : Sails _obviously_
  • Frontend : Stand-alone Vue Js app

Problem

Trying to request the datas associated to a logged in user. I use a GET request sended by 'axios' to a controller I've define in my routes config :

GET /api/v1/account/user-controller': { action: 'account/user-controller'},

The request is made with an axios instance named HTTP (it comports the authorization header and the aimed base url) :

HTTP({
      method: GET,
      url: '/api/v1/account/user-controller'
    })
      .then(resp => {
        console.log('Request for user successful, respond with the user ' + resp.data.user.id)
        // State change
        commit(USER_SUCCESS, resp)
        console.log('Commit state change : USER - succes')
      })
      .catch(err => {
        console.log('Request for user failed, respond with the err ' + err)
        // State Change
        commit(USER_ERROR, err)
        console.log('Commit state change : USER - error')
        // if resp is unauthorized, logout too
        dispatch(AUTH_LOGOUT)
      })

And the aimed controller (actions 2) in sails 👍

module.exports = {

  friendlyName: 'Get account information',

  exits: {
    success: {
      description: `Success`,
      extendedDescription:`User informations send in JSON format.`
    },
    unfound: {
      description: `Unfound`,
      responseType: 'unauthorized'
    }
  },

  fn: async function (inputs, exits) {

    var userRecord = await User.findOne({ id: this.req.token.id });
    sails.log.info('USER (cont.) - User founded', userRecord.id);

    // If there was no matching user, respond thru the "unfound" exit.
    if(!userRecord) {
      throw 'unfound';
    }

    return exits.success({user: userRecord});
  }
};

Each time the GET request is sent from my frontend, my sails instance logs this info : info: Redirecting GET request from 'undefined.' subdomain… & it "pings" the request to my frontend app.

Also if replace the method from GET to POST, it works.

Finally just for info, the relevant part of my env.files that I think is ok with all CORS settings 👍

  //--------------------------------------------------------------------------
  //     Blueprint API Configuration
  //--------------------------------------------------------------------------

  blueprints: Object.assign({}, PRODUCTION_CONFIG.blueprints, {
    actions: false,
    rest: false,
    shortcuts: false
  }),

  //--------------------------------------------------------------------------
  //      Security Settings Configuraiton
  //--------------------------------------------------------------------------

    cors: {
      allRoutes: true,
      allowOrigins: ['https://frontend-app-url.herokuapp.com'],
      allowRequestHeaders: 'Host, Origin, Referer, Content-Type, Authorization, Accept',
      allowRequestMethods: 'GET,PUT,POST,OPTIONS,HEAD'
    },
    csrf: false

  //--------------------------------------------------------------------------
  //      WebSocket Server Settings Configuration
  //--------------------------------------------------------------------------

    onlyAllowOrigins: ['https://frontend-app-url.herokuapp.com']

Most helpful comment

@qvdp At a glance, looks like this is happening because of the custom hook (see https://github.com/balderdashy/sails-generate/blob/3ac16f60b4fd9cd71f481f10aad8d3dddb222239/lib/core-generators/new/templates/api/hooks/custom/index.js.template#L139-L154)

All 7 comments

Hi @qvdp! It looks like you may have removed some required elements from the initial comment template, without which I can't verify that this post meets our contribution guidelines. To re-open this issue, please copy the template from here, paste it at the beginning of your initial comment, and follow the instructions in the text. Then post a new comment (e.g. "ok, fixed!") so that I know to go back and check.

Sorry to be a hassle, but following these instructions ensures that we can help you in the best way possible and keep the Sails project running smoothly.

*If you feel this message is in error, or you want to debate the merits of my existence (sniffle), please contact [email protected]

Sorry to be a hassle, but it looks like your issue is still missing some required info. Please double-check your initial comment and try again.

*If you feel this message is in error, or you want to debate the merits of my existence (sniffle), please contact [email protected]

@qvdp Thanks for posting, we'll take a look as soon as possible.


For help with questions about Sails, click here. If you’re interested in hiring @sailsbot and her minions in Austin, click here.

@qvdp At a glance, looks like this is happening because of the custom hook (see https://github.com/balderdashy/sails-generate/blob/3ac16f60b4fd9cd71f481f10aad8d3dddb222239/lib/core-generators/new/templates/api/hooks/custom/index.js.template#L139-L154)

@mikermcneil Your answer is spot on ! I just disabled this part of the custom hook and it works like a charm.

You solved my problem thank you.

Yes @mikermcneil - thanks for pointing that out!

May I suggest, that that redirection part with subdomains gets a small paragraph in the deployment section? I was absolutely going crazy about this for a couple of days, putting my whole infrastructure in doubt. Really tricky to find and impossible to actually find it without this issue post.

+1 for better documentation on redirection with subdomains.. I also spent awhile spinning the wheels.

Was this page helpful?
0 / 5 - 0 ratings