Redirect to login before the plugin's template is rendered



  • My plugin controller.js

    var Controllers = {};
    Controllers.renderMapPage = function (req, res, next) {
    
            //Render the templates/map.tpl
    	res.render('map', {
                 ...
    	});
    };
    

    When a user browses to the map and if he is not logged in, she should be redirected to Login page and after successful login redirect her to the map page? How can I achieve this in my controller? Pl. guide.


  • Community Rep

    You need to set the returnTo property. This will redirect them back to the mappage route after login.

    var Controllers = {};
    Controllers.renderMapPage = function (req, res, next) {
    
            if (!req.uid) {
                    req.session.returnTo = '/mappage';
                    return res.redirect('/login');
            }
    
            //Render the templates/map.tpl
    	res.render('map', {
                 ...
    	});
    };
    


  • Thank you for the reply.

    But when I try to browse the plugin map route without logging in, the browser is throwing this error:

    ajaxify.js:95 Uncaught TypeError: Cannot read property 'name' of undefined
        at ajaxify.js:95
        at Object.success (ajaxify.js:318)
        at c (jquery.js:3317)
        at Object.fireWith [as resolveWith] (jquery.js:3447)
        at C (jquery.js:9272)
        at XMLHttpRequest.<anonymous> (jquery.js:9514)
    (anonymous) @ ajaxify.js:95
    success @ ajaxify.js:318
    c @ jquery.js:3317
    fireWith @ jquery.js:3447
    C @ jquery.js:9272
    (anonymous) @ jquery.js:9514
    XMLHttpRequest.send (async)
    send @ jquery.js:9566
    ajax @ jquery.js:9173
    ajaxify.loadData @ ajaxify.js:292
    ajaxify.go @ ajaxify.js:85
    s @ ajaxify.js:363
    (anonymous) @ ajaxify.js:414
    dispatch @ jquery.js:5206
    g.handle @ jquery.js:5014
    

  • Community Rep

    I see, one step I missed, you need to set some request headers for the API when redirecting a route.

    var Controllers = {};
    Controllers.renderMapPage = function (req, res, next) {
    
            if (!req.uid) {
                    req.session.returnTo = '/mappage';
    
                    if (res.locals.isAPI) {
                            res.set('X-Redirect', '/login').status(200).json('/login');
                    } else {
                            res.redirect('/login');
                    }
    
                    return;
            }
    
            //Render the templates/map.tpl
    	res.render('map', {
                 ...
    	});
    };
    


  • Thanks it is almost working now. The only exception is that after successful login, it takes me to the Home page with the following URL displayed:
    http://localhost:4567/?loggedin=true

    and not the /map route.


  • Global Moderator

    Just FYI, you can use helpers.redirect as seen here:
    https://github.com/NodeBB/NodeBB/blob/master/src/controllers/helpers.js

    Used like this:

    var helpers = require.main.require('./src/controllers/helpers');
    
    if (whatever) {
      helpers.redirect(wherever);
    }
    


  • 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

    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

| |