2014-06-24 16 views
5

Tôi đang thử số này git example.cách gửi json dưới dạng phản hồi sau khi xác thực hộ chiếu node.js

Hoạt động tốt khi tôi tích hợp nó với dự án của mình, nhưng những gì tôi muốn đạt được là gửi json như một phản hồi cho khách hàng/yêu cầu, thay vì successRedirect: '/ profile' & failRedirect: '/ signup'.

Có thể gửi một json, hoặc là có một số phương pháp khác để có được như vậy?

Bất kỳ trợ giúp sẽ được đánh giá cao, TU

Trả lời

2

ở đây tôi đổi mã của tôi để gửi json như một phản ứng

// process the signup form 
app.post('/signup', passport.authenticate('local-signup', { 
    successRedirect : '/successjson', // redirect to the secure profile section 
    failureRedirect : '/failurejson', // redirect back to the signup page if there is an error 
    failureFlash : true // allow flash messages 
})); 

app.get('/successjson', function(req, res) { 
    res.sendfile('public/index.htm'); 
}); 

app.get('/failurejson', function(req, res) { 
    res.json({ message: 'hello' }); 
}); 
4

Tạo tuyến đường mới, ví dụ .: /jsonSend với res.json trong đó và làm cho successRedirect: '/jsonSend'. Nên làm vậy.

+0

GRT người đàn ông, hãy để tôi thử – pitu

+0

này cảm ơn bạn vì đã giúp @thyforhtian nó workd – pitu

+0

kiểm tra câu trả lời dưới đây của tôi – pitu

1

Sử dụng hộ chiếu như một trung gian.

router.get('/auth/callback', passport.authenticate('facebook'), 
    function(req, res){ 
     if (req.user) { res.send(req.user); } 
     else { res.send(401); } 
    }); 
3

Bạn có thể sử dụng chức năng xác thực hộ chiếu làm phần mềm trung gian trong ứng dụng nhanh.

app.post('/login', 
    passport.authenticate('local'), 
    function(req, res) { 
    // If this function gets called, authentication was successful. 
    // `req.user` contains the authenticated user. 
    // Then you can send your json as response. 
    res.json({message:"Success", username: req.user.username}); 
    }); 

Theo mặc định, nếu xác thực không thành công, Hộ chiếu sẽ đáp ứng trạng thái không được phép 401 và mọi trình xử lý bổ sung tuyến đường sẽ không được gọi. Nếu xác thực thành công, trình xử lý tiếp theo sẽ được gọi và thuộc tính req.user sẽ được đặt thành người dùng được xác thực.

0
// process the signup form 
app.post('/signup', passport.authenticate('local-signup', { 
    successRedirect : '/successjson', // redirect to the secure profile section 
    failureRedirect : '/failurejson', // redirect back to the signup page if there is an error 
    failureFlash : true // allow flash messages 
})); 

app.get('/successjson', function(req, res) { 
    res.sendfile('public/index.htm'); 
}); 

app.get('/failurejson', function(req, res) { 
    res.json({ message: 'hello' }); 
}); 
2

Có một quan chức Tuỳ chỉnh Callback tài liệu:

app.get('/login', function(req, res, next) { 
    passport.authenticate('local', function(err, user, info) { 
    if (err) { return next(err); } 
    if (!user) { return res.redirect('/login'); } 
    req.logIn(user, function(err) { 
     if (err) { return next(err); } 
     return res.redirect('/users/' + user.username); 
    }); 
    })(req, res, next); 
}); 

https://github.com/passport/www.passportjs.org/blob/master/views/docs/authenticate.md

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