4

This is my first time here, so I hope I post this correctly.

I am working with Loopback here, and I am having a problem when I try to redirect to another route. Basically I have two middlewares. The first, checks with Firebase if the user if authenticated and allowed to visit /dashboard. If no, I call the next() middleware and this one redirects back to /login again.

Dashboard.js:

'use strict';
    const firebase = require("firebase");
    exports.dashboard = (req, res, next) => {
      firebase.auth().onAuthStateChanged((user) => {
          if (user) {
            console.log("Before render: "+res.headersSent); // false
            res.render("dashboard", {photoUrl: user.photoURL, displayName: user.displayName, email: user.email });
            console.log("after render: "+res.headersSent);
          } else {
            // console.log("on else"  );
            next()
          }
      });
    
    };
    
    exports.notLogged = (req, res) => {
      console.log("no one logged in");
      res.redirect('/login');
    };

Route on server.js:

 app.get('/dashboard', dashboard.dashboard, dashboard.notLogged);

The middleware on login.js:

exports.login = (req, res) => res.render("login")

And its route:

app.get('/login', login.login);

For example, if I try to access "/dashboard" and I am not logged, the "console.log("no one logged in"); " will be twice on the console, or any other log on the flow. It looks like another request was made, but I can't understand why or how. If I remove the redirect and simply end the response or render a page, the problem does not occur.

Thank you! :)

UPDATE

I am back here and still with no solution. This is driving me crazy.

Double GET happening after res.redirect :(

peterh
  • 11,875
  • 18
  • 85
  • 108
denyzprahy
  • 820
  • 11
  • 24

1 Answers1

2

You might used the middleware in a wrong way.

 app.get('/dashboard', dashboard.dashboard, dashboard.notLogged);

This line means after running dashboard.dashboard, dashboard.notLogged will be running anyway, whether or not you are calling next()

So you can solve the problem like this:

     if (user) {
        console.log("Before render: "+res.headersSent); // false
        res.render("dashboard", {photoUrl: user.photoURL, displayName: user.displayName, email: user.email });
        console.log("after render: "+res.headersSent);
      } else {
        // console.log("on else"  );
        notLogged()
      }

And remove notLogged from here:

 app.get('/dashboard', dashboard.dashboard)

http://expressjs.com/en/4x/api.html#router

YLS
  • 697
  • 5
  • 9
  • This is not correct. If you are building middleware chains, they are invoked sequentially and only if the previous middleware is calling `next()`. – Jonas Bürkel Aug 29 '16 at 08:10
  • Yes, that's in fact wrong. This way the response gets stuck in the "else" and nothing happens. Thanks anyway! :) – denyzprahy Aug 29 '16 at 12:06
  • I tried like this: In the else: `notLogged(req, res);` And the middleware: `function notLogged(req, res) { console.log("no one logged in"); res.status(200); res.redirect('/login'); res.end() } exports.notLogged = notLogged; ` This works as well, but did not fix the problem :/ – denyzprahy Aug 29 '16 at 13:42