使用本地策略的Passport登录返回丢失的凭据

时间:2016-08-01 04:03:54

标签: angularjs authentication mean-stack passport-local passport.js

我正在尝试使用MEAN Stack和passportjs构建一个带有用户身份验证的Web应用程序,因为我正在尝试登录,401:返回丢失的凭据。

这是当地战略:

    passport.use(new localStrategy({
        usernameField: 'mobileNumber'
    }, 
    function(username, password, done){
    User.findOne({mobileNumber: username}, function(err, user){
        if(err) {
            return done(err);
        }
        if(!user) { 
            return done(null, false, {
            message: 'User not found'
                });
            }   
        if(!user.validPassword(password)) {
            return done(null, false, {
                message: 'Incorrect Password'
            });
        }
        return done(null, user);
    });
    }
));

来自身份验证控制器的功能

module.exports.login = function (req, res){
    console.log("logging in");
    passport.authenticate('local', function(err, user, info){
        console.log("passport logging in");
        var token;
        if(err){
            res.status(404).json(err);
            return;
        }
        if(user){
            token = user.generateJwt();
            res.status(200);
            res.json({
                "token": token
            });
        }
        else {
            res.status(401).json(info);
        }
    })(req, res);
}

由此路线调用:

app.post('/api/login', authCtrl.login);

这是我的主要app.js:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var cors = require('cors');

var passport = require('passport');
require('./config/passport');


require('dotenv').load();
app.use(cors());

app.use(express.static('public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());

app.use(passport.initialize());

require('./routes')(app);

这是启动注册并在角度控制器中登录的功能:

var loginUser = {
        mobileNumber : $scope.mobileNumber,
        password : $scope.password
    }
$scope.signIn = function(){
    AuthSrv.login(loginUser)
    .error(function(err){
        $scope.loginErrorTrigger = true;
        $scope.loginError = err.message;
        console.log(err);
    })
    .then(function(){
        $location.url('/account');
    });
}
$scope.signUp = function(){
    $location.url('/signUp');
} 

$scope.continue = function (){
    $scope.fillingError = checkFields();
    if(!$scope.fillingError){
        var signedUpUser = {
            mobileNumber : $scope.mobileNumber,
            password : $scope.password,
            firstName : $scope.firstName,
            lastName : $scope.lastName,
            email : $scope.email,
            address : $scope.address,
            university : $scope.university,
            faculty : $scope.faculty,
            academicYear : $scope.academicYear
        };
        AuthSrv.register(signedUpUser)
        .error(function(err){
            console.log(err);
            $scope.apiError = true;
        })
        .then(function (){
            $location.url('/signUpComplete');
        });
    }
}

此处console.log(err)始终打印Object {message: "Missing credentials"}

这是AuthSrv中的函数:

login = function(user){
        console.log("Attempt to login");
        return $http.post('/api/login', user).success(function(data){
            console.log("log in succesful");
            console.log(data.token);
            console.log($window.localStorage['mean-token']);
            saveToken(data.token);
        });
    }

这个周期出了什么问题?请帮助它已经两天了,这非常令人沮丧!

0 个答案:

没有答案