Redirection to route in plugin



  • I have a plugin that executes a "filter:topic.get"

    I need to doing a redirection to custom server route (localhost:4567/topicerror) when a "if clause" is true.

    I need that

    plugin.filterTopicGetMethod = function (postContent, callback) {
       if(awesomeCondition == true ) {
           //Do a redirection
       }
    callback(null, postContent)
    };
    

    What is the way for doing this?

    Thanks




  • Admin

    this is the usual way of doing it, unfortunately you don't have access to the res object...

    return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url.path);
    

    I added some functionality in this commit to allow you to redirect from anywhere, just send back an object with status 302 and a path as the error.

    callback({
    	status: 302,
    	path: '/user/psychobunny'
    });
    


  • Thanks for your colaboration, I'm going to try this



  • @psychobunny

    Well, Now I have this code:

    plugin.filterTopicGetMethod = function (postContent, callback) {
       if(awesomeCondition == true ) {
           //Do a redirection
               callback({
          status: 302,
          path: '/topicerror'
        });
       }
     callback(null, postContent)
     };
    

    And the console give me this error:
    Selección_003.png


  • Admin

    1 - are you up to date with the latest code? I just added that line today

    2 - there's a return missing in your conditional, right now callback is done twice



  • I'm up to date, in fact I wrote your commit in my nodebb core yesterday.

    2- Now I rewrite the code and I have This

    [...]
      if(topicTitle.indexOf('+hd')  >= 0) {
      //Contiene la etiqueta +HD
      if (userid <= 1 || tagsTitle.postCount  == 0 ) {
        //Redireccionar
        callback({
          status: 302,
          path: '/topicerror'
        }, postContent);
    }
      }
    [...]
    

    But console give me the same error, ''error: [plugins] filter:topic.get, undefined''



  • Up,
    Can anyone help me, please?


Log in to reply
 


Looks like your connection to NodeBB was lost, please wait while we try to reconnect.