Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. isAuthenticated() always returns false irrespective whether user is logged in or not

isAuthenticated() always returns false irrespective whether user is logged in or not

Scheduled Pinned Locked Moved JavaScript
javascriptjsonhelpmongodbdata-structures
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 13725205
    wrote on last edited by
    #1

    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
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups