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:
- 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.- 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.
-
Hmm,
Try setting this additional header maybe
res.set('X-Redirect', '/login').set('x-return-to', '/mappage').status(200).json('/login');
-
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.
-
helpers.redirect
does not setreturnTo
, it does exactly what I had in the verbose code I posted.You are also missing a
return
statement after theredirect
. That wouldn't cause it not to function, but it will throw an error to the console. -
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.
-
Okay, I figured it out, you just have to use
helpers.notAllowed
(401 status), this stores the previousUrl in ajaxify.jsvar 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=trueI 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=FALSEit 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!"?