护照JS-为什么用户为空?

时间:2018-12-16 21:27:38

标签: node.js ionic3 passport.js

我使用的是ionic v.3,我想做的是使用护照使用“ localstrategy”创建身份验证。 问题是我找不到用户,请查看“ console.log('passport用户'+用户)”行

server.js 在下面的配置中,包括护照,中间件配置和会话

                //INTEGRAZIONE FILE CONFIG PASSPORT
            const passport = require('passport');
            //MIDDLEWARE SESSION
            app.use(require('express-session')({
                secret: 'keyboard cat',
                resave: false,
                saveUninitialized: false
            }));
            app.use(passport.initialize());
            require('./config/passport')(passport);
            app.use(passport.session());
            //
      app.use(cors({origin: 'http://localhost:8100', credentials: true}));


                //EFFETTUA LOGIN
            app.post('/api/login', (req, res, next)=>{ 
                passport.authenticate('local', function(err, user){
                console.log('UTENTE... '+user);
                if(err){
                    return next('errore: '+err);
                    }
                if(!user){
                    return res.send({redirect: '/signup'});
                    }
                req.logIn(user, function(err) {
                    if (err) { return next(err); }
                    return res.send({redirect: '/lista_note'});
                });
                }) (req, res, next);
            });

配置/护照

const LocalStrategy = require('passport-local').Strategy;
            const mongoose = require('mongoose');
            const bcrypt = require('bcrypt');

            //SCHEMA E MODELLO PER UTENTI
            require('../model/utente');
            const Utente = mongoose.model('Utente');

            module.exports = function(passport){
                passport.use(new LocalStrategy({usernameField:'email'},(email,password,done)=>{
                        // Match user
                        Utente.findOne({
                            email:email
                        }).then(utente => {
                            console.log('passport user '+utente)
                            if(!utente){
                                return done(null, false, {message: 'Utente non trovato'});
                            } 
                            // Match password
                            bcrypt.compare(password, utente.password, (err, isMatch) => {
                                if(err) throw err;
                                if(isMatch){
                                    return done(null, utente);
                                } else {
                                    return done(null, false, {message: 'password non corretta'});
                                }
                            })
                    })
                }));

                    passport.serializeUser(function(utente, done) {
                        done(null, utente.id);
                    });

                    passport.deserializeUser(function(id, done) {
                        Utenti.findById(id, function(err, utente) {
                            done(err, utente);
                        });
                    }); 
            } 

设置错了什么?谢谢

0 个答案:

没有答案