使用passport.js进行无限循环的简单身份验证

时间:2014-07-09 17:20:57

标签: javascript node.js express passport.js

我确定,由于我缺乏这项技术的经验,我做错了。 所以我在这里试图验证我的用户。

在server.js中的

我有以下内容:

var express = require('express');
var app = express();
var port     = process.env.PORT || 8080;
//var configDB = require('./config/database.js');


require('./config/environement.js')(app, express);
require('./config/routes.client.js')(app);

//setting all modules routes
require('./api/oAuth/routes.js')(app);

app.listen(port);

在environement.js中:

module.exports = function(app, express) {
    app.configure(function() {
        var path = require('path');
        var mongoose = require('mongoose');
        var passport = require('passport');
        var LocalStrategy = require('passport-local').Strategy;

        /*           je pense que ce code n'a rien a faire ici*/
        var User = require('./../models/user.js');



        passport.use(new LocalStrategy(

        function(username, password, done) {
            User.findOne({
                username: username
            }, function(err, user) {
                if (err) {
                    return done(err);
                }
                if (!user) {
                    return done(null, false, {
                        message: 'Incorrect username.'
                    });
                }
                if (!user.validPassword(password)) {
                    return done(null, false, {
                        message: 'Incorrect password.'
                    });
                }
                return done(null, user);
            });
        }));

        app.use(express.logger());
        app.use(express.static(path.join(__dirname + '/../views')));
        app.use(express.cookieParser());
        app.use(express.bodyParser());
        app.use(express.session({secret: 'm4B1teD4nsTaG0rgE'}));
        app.use(passport.initialize());
        app.use(passport.session());
        app.use(app.router);
        passport.serializeUser(User.serializeUser());
        passport.deserializeUser(User.deserializeUser());

        /*fin*/



        mongoose.connect('mongodb://localhost/passport_local_mongoose');

        app.set('views', __dirname + '/../views');
        app.set('view engine', 'jade'); //extension of views



        console.log("config ok");



    });

    //development configuration
    app.configure('development', function() {
        app.use(express.errorHandler({
            dumpExceptions: true,
            showStack: true
        }));
    });

    //production configuration
    app.configure('production', function() {
        app.use(express.errorHandler());
    });

};

最后我的api / oAuth / routes.js

var User = require('../../models/user.js');
var passport = require('passport');


module.exports = function(app) {

    app.get('/register', function(req, res) {
        res.render('../api/oAuth/views/register.page.jade');
    });

    //Route vers /login en get et post
    app.get('/login', function(req, res) {
        res.render('../api/oAuth/views/login.page.jade');
    });

     app.post('/api/oAuth/login', function(req, res, next) {
        console.log("post login = ok");
       passport.authenticate('local',function(req, res) {
    // If this function gets called, authentication was successful.
    // `req.user` contains the authenticated user.
    res.redirect('/users/' + req.user.username);
  });
     });




    app.post('/api/oAuth/register', function(req, res) {
        User.register(
        new User({
            username: req.body.username
        }), req.body.password, function(err, user) {
            if (err) {
                res.send(err);

            }
            else {
                res.send("Success");

            }
        });
    });
}

编辑:添加了user.js

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    passportLocalMongoose = require('passport-local-mongoose');
    var passport = require('passport');

var User = new Schema({
    username: String,
    password: String
});

User.plugin(passportLocalMongoose);


passport.serializeUser(function(user, done) {
  console.log("serializeUser");
  done(null, user);
});

passport.deserializeUser(function(user, done) {
    console.log("deserializeUser");
  done(null, user);
});


module.exports = mongoose.model('User', User);

当应用程序进入passport.authenticate()时,它会做很多事情然后以循环方式返回到passport.authenticate()。当我使用自定义回调时,我意识到passport.authenticate()进展顺利,但问题似乎在req.logIn()函数中。我不知道该怎么做才能使这项工作,我尝试了很多。我觉得自己像一个试驾车的盲人:D。

0 个答案:

没有答案