Redirect to login before the plugin's template is rendered


  • Ok Pita .. I will give it a try. I found an interesting point and I'd like to share it with you:

    1. Cold loading :

    User is not logged in, she enters the URL: localhost:4567/map and presses Enter
    She is taken to the Login screen and after successful login, she is successfully redirected to the map page.

    1. User is not logged in. She clicks the Map navigation icon, she is taken to the Login screen and after successful login, she is taken to the Home page/URL. Why not to the /map page I am wondering.

    Kindly enlighten me.


  • What I mean to say in the post above is that req.session.returnTo is working properly in case of Cold Load but in the other case, it is not.

  • Community Rep

    Hmm,

    Try setting this additional header maybe

    res.set('X-Redirect', '/login').set('x-return-to', '/mappage').status(200).json('/login');


  • @pitaj It didn't work. Looks like I am missing something.
    @yariplus Neither this header worked. Same problem.

  • Global Moderator Plugin & Theme Dev

    What code did you try?


  • I tried this code assuming that returnTo or its equivalent property is set by the helpers.redirect (or its associated code down the line) itself.

    var helpers = require.main.require('./src/controllers/helpers');
    var Controllers = {};
    
    Controllers.renderMapPage = function (req, res, next) {
    
        if (!req.uid) {
    	helpers.redirect(res, '/login');
        }
    ...
    res.reder(...)
    

    Where am I wrong, kindly guide.

  • Community Rep

    helpers.redirect does not set returnTo, it does exactly what I had in the verbose code I posted.

    You are also missing a return statement after the redirect. That wouldn't cause it not to function, but it will throw an error to the console.

  • Community Rep

    Hmm, you are definitely right it doesn't work.

    With the way ajaxify.js works, it always returns to the previously visited url, not the one redirected from.

  • Community Rep

    Okay, I figured it out, you just have to use helpers.notAllowed (401 status), this stores the previousUrl in ajaxify.js

    var helpers = require.main.require('./src/controllers/helpers');
    var Controllers = {};
    Controllers.renderMapPage = function (req, res, next) {
    
            if (!req.uid) return helpers.notAllowed(req, res);
    
            //Render the templates/map.tpl
    	res.render('map', {
                 ...
    	});
    };
    

    This will return you to the map page on cold load and ajaxing.


  • Finally it worked. Thank you very much. Just two points:

    a. Ajaxing ends up with this URL:
    http://localhost:4567/map?_=1514797485831&loggedin=true

    I mean the internal details are visible to the user. An ideal solution is to reset the URL to something like this: localhost:4567/map

    b. Suppose I cold load with an invalid URL:
    http://localhost:4567/map?loggedin=FALSE

    it still renders the map page. Ideally, it should display the "Page Not found message"

    Any solution to these two? Or, shall I consider them as "OK let it behave so!"?

Suggested Topics

| |