Redirect to login before the plugin's template is rendered
-
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
-
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=trueand not the /map route.
-
Just FYI, you can use
helpers.redirect
as seen here:
https://github.com/NodeBB/NodeBB/blob/master/src/controllers/helpers.jsUsed 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:
- 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!"?