M. Node
M. Node

Reputation: 763

Page is not properly redirecting with express-session and passport nodejs

I am using passport-local and express-session but my problem is that when I enter an address, after I close the session, I get the problem the browser "Page is not properly redirecting", so what I want to do is redirect to The address / login instead of showing that problem

I'll leave my code

Routes.js

app.get('/index', isLoggedIn, function(req, res) {
    console.log(req.session);
        if (req.session == null  || req.session == undefined)
        {
             res.redirect('/login');
        }
        else
        {
        res.render('index.ejs', {
            user : req.user // get the user out of session and pass to template
        });
    }

    });

app.js

 app.use(session({
     secret  : 'asjdknas',

 }));


 app.use(passport.initialize());
 app.use(passport.session());
 app.use(flash());

update solved

The verification I did the function I was calling in app.get call

Function isLoggedIn (req, res, next) {

If (req.isAuthenticated ()) Return next ();

Res.redirect ('/login'); }

Upvotes: 1

Views: 1270

Answers (2)

HarshaHR
HarshaHR

Reputation: 73

in passport, there is a inbuilt function, req.isAuthenticated()

Upvotes: 0

DrunkDevKek
DrunkDevKek

Reputation: 469

Try using return res.redirect instead of res.redirect and tell me what happens. (same for render)

Also, what does the console.log(req.session) give you ?

Upvotes: 0

Related Questions