2014-12-23 117 views
1

這是我的節點JS應用程序的代碼:護照JS +快遞4.故障結束登錄會話

"use strict"; 
var express = require('express'); 
var app = express(); 
var port = process.env.PORT || 8080; 
var passport = require('passport'); 
var LocalStrategy = require('passport-local').Strategy; 
var morgan = require('morgan'); 
var cookieParser = require('cookie-parser'); 
var bodyParser = require('body-parser'); 
var session = require('express-session'); 
var flash = require('connect-flash'); 
var favicon = require('serve-favicon'); 
app.use(morgan('dev')); 
app.use(cookieParser()); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ 
    extended: true 
})); 
app.use(session({secret: 'kushagrasingh', resave: false, saveUninitialized: false })); 
app.use(flash()); 
app.use(express.static(__dirname)); 
app.use(favicon(__dirname + '/favicon.ico')); 
app.use(passport.initialize()); 
app.use(passport.session()); 

app.get('/welcome', function (req, res) { 
    res.sendFile(__dirname + '/login.html'); 
}); 

app.get('/logout', function (req, res) { 
    req.logOut(); 
    req.session.destroy(function (err) { 
     if (err) { 
      console.log(err); 
     } 
     res.redirect('/welcome'); 
    }); 
}); 

function isLoggedIn(req, res, next) { 
    if (req.isAuthenticated()) { 
     return next(); 
    } 
    res.redirect('/welcome'); 
} 

app.get('/', isLoggedIn, function (req, res) { 
    res.sendFile(__dirname + '/index.html'); 
}); 

var users = [ 
    { id: '1', username: 'kush', password: 'abc'}, 
    { id: '2', username: 'kushagra', password: 'abcd'} 
]; 

function findByUserName(username, fn) { 
    var check = 0; 
    users.every(function(user){ 
     console.log(user.username+'************'+username); 
     if(user.username === username) { 
      console.log('true'); 
      check = 1; 
      return fn(null, user); 
      //return false; 
     } 
    }); 
    if (check === 0) { 
     return fn('Wrong username', null); 
    } 
    //return fn(null, null); 
} 

function findById(id, fn) { 
    var check = 0; 
    users.every(function(user) { 
     if(user.id === id) { 
      check = 1; 
      return fn(null, user); 
     } 
    }); 
    if(check === 0) { 
     console.log('false'); 
     return fn(new Error("No user with id: "+id)); 
    } 
} 



passport.serializeUser(function(user, done) { 
    console.log('serialize'); 
    done(null, user.id); 
}); 

passport.deserializeUser(function(id, done) { 
    console.log('deserialize'); 
    findById(id, function (err, user) { 
     done(err, user); 
    }); 
}); 

passport.use('local', new LocalStrategy(
    function(username, password, done) { 
     process.nextTick(function() { 
      console.log('check'); 
      findByUserName(username, function(err, user) { 
       if (err) { console.log(err); return done(err); } 
       else if (!user) { return done(null, false, { message: 'Unknown user ' + username }); } 
       else if (user.password !== password) { return done(null, false, { message: 'Invalid password' }); } 
       else { 
        console.log("USER RETURNED"); 
        return done(null, user); 
       } 
      }); 
     }); 
    } 
)); 


app.post('/login', 
    passport.authenticate('local', { successRedirect: '/', failureRedirect: '/loginFailure', failureFlash: true })); 

app.listen(port); 
console.log("server started at " + port); 

ensureAuthenticated功能沒有限制即訪問 如果用戶已註銷完成其任務。因此,即使退出後,'/'get請求也正在完成。 真的很感激任何幫助。 在此先感謝:)

回答

0

重新定位app.use(express.static(__dirname));行做了這項工作。 將它放在路線後面並修復所有問題。