2016-04-22 19 views
6

Tôi đang làm việc trên một trang web blog đơn giản dựa trên angular.js + node.jsmongodb sử dụng mẫu nhanh. Tôi nhấn với $http từ bộ điều khiển góc theo phương thức POST thành api có tên users.js trong đó thông tin đăng nhập được xác thực bằng phương thức passport.authenticate. Tôi yêu cầu các chiến lược đăng nhập hộ chiếu địa phương ở users.js.
Nhưng nó không hoạt động. Có mã dịch vụ đăng nhập góc và mã api của người dùng nút. Ai có thể cho tôi biết làm thế nào có thể sử dụng passport.js trong góc và nút?cách sử dụng hộ chiếu với ứng dụng nút góc cạnh?

định tuyến góc thông qua một dịch vụ

app.service('Auth',function($location,$http,$localStorage){     
    var userLogin ; 
    return{ 
    setLogIN:function(email,password){ 
     $http({ 
     method: 'POST', 
     url: '/users/login', //users.js having node routing. 
     data: {email:email, password:password}, 
     }) 

nút định tuyến trong sử dụng

router.post('/login',passport.authenticate('local', { 
    // use passport-local for authentication 
    successRedirect : '/profile', 
    failureRedirect : '/login', 
    failureFlash : true 
})); 

hộ chiếu cục bộ chiến lược

app.use(passport.initialize()); 

app.use(passport.session()); 
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, {alert: 'Incorrect username.'}); 
      } 
      if (user.password != password) { 
       return done(null, false, {alert: 'Incorrect password.'}); 
      } 
      return done(null, user); 
     }); 
    } 

)); 


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

passport.deserializeUser(function(id, done) { 
    User.findById(id, function(err, user) { 
     done(err, user); 
    }); 
}); 

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

Vì vậy, tôi muốn xác thực bằng hộ chiếu, nhưng sử dụng phía khách hàng templating/định tuyến để giữ xác thực thích hợp.

Ai đó có thể vui lòng chỉ cho tôi đúng hướng không? Hoặc cho tôi biết nếu những gì tôi đang làm là hoàn toàn sai lầm?

chỉnh sửa: lỗi TÔI nhận với mã của tôi là nó không chuyển hướng đến trang tiểu

TypeError: POST http://localhost:3000/users/login 500 Internal Server Error

Not a valid User

+0

bài viết mã của chiến lược xác thực của bạn cũng. – ayushgp

Trả lời

2

Theo mặc định, LocalStrategy hy vọng các thông số từ điển để được đặt tên usernamepassword.

Nếu bạn muốn sử dụng email thay vì username, thì bạn nên xác định chúng trong chiến lược của bạn:

passport.use(new LocalStrategy({ 
    usernameField: 'email', 
    passwordField: 'password' 
    }, 
    function(username, password, done) { 
    // ... 
    } 
)); 

Đối với trường hợp của bạn, nó phải là:

passport.use(new LocalStrategy({ 
    usernameField: 'email', 
    passwordField: 'password' 
    }, 
function (username, password, done) { 

    User.findOne({username: username}, function (err, user) { 

     if (err) { 
      return done(err); 
     } 
     if (!user) { 
      return done(null, false, {alert: 'Incorrect username.'}); 
     } 
     if (user.password != password) { 
      return done(null, false, {alert: 'Incorrect password.'}); 
     } 
     return done(null, user); 
    }); 
} 
)); 
2

tôi tìm thấy giải pháp cho tôi câu hỏi .. cách sử dụng hộ chiếu có định hướng angular-nodejs .......

//angular js routing 
$scope.userlogin=function(){ 
    $http({ 
     method:"post", 
     url:'/users/login', 
     data:{username:$scope.username,password:$scope.password}, 
    }).success(function(response){ 
     $scope.userData = response; 
     $localStorage.userData = $scope.userData; 
     console.log("success!!"); 
     $location.path("/profile") 
    }).error(function(response){ 
     console.log("error!!"); 
     $location.path("/login") 
    }); 
} 

tôi sử dụng phương thức POST và nhấn nút điều khiển node (users.js) và nhận phản hồi từ nó. nếu xác thực người dùng thành công thì nó sẽ chuyển sang chế độ xem hồ sơ nếu không thì vẫn ở chế độ xem đăng nhập.

//add these two lines to app.js 
// var app = express(); 
app.use(passport.initialize()); 

app.use(passport.session()); 

//node routing 
// add passport-stretegies to users.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.password != password) { 
      return done(null, false, { message: 'Incorrect password.' }); 
     } 
     return done(null, user); 
     // console.log(user) 
    }); 
})); 

//passport serialize user for their session 
passport.serializeUser(function(user, done) { 
    done(null, user.id); 
}); 
//passport deserialize user 
passport.deserializeUser(function(id, done) { 
    user.findById(id, function(err, user) { 
     done(err, user); 
    }); 
}); 

//router on same page 
router.post('/login',passport.authenticate('local'),function(req,res){ 

res.send(req.user); 
    //console.log(req.user); 
}); 

có được một hit từ phía góc Xuyên bài phương pháp nó sử dụng phương pháp hộ chiếu địa phương để xác thực nếu người dùng được xác thực seccessfully sau đó người dùng xác thực được gửi như phản ứng ..

Các vấn đề liên quan