2016-11-27 142 views
0

我想創建一個帳戶後,自動登錄用戶。 需要將它們重定向到/auth/local/發送他剛剛創建的用戶和密碼。註冊後自動登錄

這是我middleware/signup.js

'use strict'; 

module.exports = function(app) { 
return function(req, res, next) { 
const body = req.body; 

// Get the user service and `create` a new user 
app.service('users').create({ 
    email: body.email, 
    password: body.password 
}) 
// Then redirect to the login page 
.then(user => app.post('/auth/local', function(req, res){ 
    req.body(user); 
})) 
// On errors, just call our error middleware 
.catch(next); 
}; 
}; 

這不給任何錯誤...只是一個永恆的負載創建一個用戶後。 幫助

回答

0

首先它是一個永恆的負載,因爲你沒有發送迴應或致電next。請閱讀about express middlewares here

除此之外,你爲什麼要從自己的POST請求到你自己的API?在這裏,我沒有足夠的上下文來了解app是什麼,但是你的代碼看起來更像是一個路由定義,而不像一個請求。 這裏你應該有一些方法來調用一個認證用戶的函數。 例如,如果使用護照:

'use strict'; 

module.exports = function(app) { 
return function(req, res, next) { 
const body = req.body; 

// Get the user service and `create` a new user 
app.service('users').create({ 
    email: body.email, 
    password: body.password 
}) 
// Then redirect to the login page 
.then(user => passport.authenticate('local')(req, res, next)) 
// On errors, just call our error middleware 
.catch(next); 
}; 
};