The req.isAuthenticated() of api.js function always returns false whether or not the user is logged in or not. Thus user is redirected to /#login at all times. I have not yet connected to MongoDB so it's just the javascript and nodejs part. I am testing my code on advanced rest client. I tried a few solutions which suggested using cookies but it doesn't seem to work. I know there already exists a lot of answers to this question and I have tried nearly most of the answers on stack overflow but somehow nothing seems to work. I don't know what I am missing out on. I am stuck on this for a long time ao any kind of help is highly appreciated. app.js (main file):
var http_errors = require('http-errors');
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var passport = require('passport');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var api = require('./routes/api');
var authenticate = require('./routes/authenticate')(passport);
var app = express();
app.use(cookieParser('super duper secret'));
app.use(session({
secret: 'super duper secret',
resave: true,
saveUninitialized: true
}));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(\_\_dirname, 'public')));
app.use(passport.initialize());
app.use(passport.session());
// view engine setup
app.set('views', path.join(\_\_dirname, 'views'));
app.set('view engine', 'ejs');
app.use('/api', api);
app.use('/auth', authenticate);
//Initialize passport
var initPassport = require('./passport-init');
initPassport(passport);
module.exports = app;
Routing files:- api.js:
var express = require('express');
var router = express.Router();
router.use(function(req, res, next){
if(req.method === "GET"){
//continue to the next middleware or request handler
return next();
}
if (!req.isAuthenticated()){
//user not authenticated, redirect to login page
return res.redirect('/#login');
}
//user authenticated continue to next mi