2016-11-27 148 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); 
}; 
};