我有以下ajax帖子,它与passportjs完美配合。
$(document).ready(function(){
$('#btn-login').click(function() {
var email = $('#loginEmail').val();
var password = $('#loginPassword').val();
var emailValidation = new RegExp(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i);
if(!email | !password) {
$('#loginLabel').text('Email and password are required.');
}
else if(!emailValidation.test(email)) {
$('#loginLabel').text('Email format is invalid.');
}
else {
var userData = {
username: email,
password: password
};
var request = $.ajax({
type: 'POST',
url: '/login',
data: userData
});
request.done(function (response, textStatus, jqXHR) {
window.location.href = '/profile';
}).fail(function (jqXHR, exception) {
$('#loginLabel').text('Invalid email and/or password.');
});
}
});
});
现在我正在尝试从节点做同样的事情,但它无法正常工作。
activateUserAccount(payload, res) {
const token = payload.token;
signUpService.validateEmailToken(token)
.then(isVerified => {
if(isVerified[0]) {
const userData = JSON.stringify({
username: isVerified[1],
password: isVerified[2]
});
const options = {
hostname: 'localhost',
port: 3000,
path: '/login',
agent: false,
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
};
const req = http.request(options, (response) => {
let responseString = '';
response.on('data', data => {
responseString += data;
});
response.on('end', (data) => {
res.redirect('profile');
});
});
req.write(userData);
req.end();
}
else {
res.redirect('error-page' + '?status=errit');
}
})
.catch(err => console.log(err));
}
不介意JSON部分,因为值正确传递给passportjs,我已经控制台记录了所有内容。
Passportjs part。
const expiryDate = new Date(Date.now() + 60 * 60 * 1000);
app.use(session({
secret: 'XeGcW4Vb23',
resave: false,
saveUninitialized: false,
httpOnly: true,
expires: expiryDate
}));
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
const restrictedArea = (req, res, next) => {
if (req.isAuthenticated()) {
next();
} else {
res.redirect('/login');
}
};
passport.use(new LocalStrategy(
(username, password, done) => {
console.log("strategy username");
console.log(username);
console.log("strategy password");
console.log(password);
console.log("strategy done");
console.log(done);
databaseManagement.selectUser(username, password)
.then(user => _.isEmpty(user)
? done(null, false)
: done(null, { username: username, password: password }));
}
));
app.post('/login', (req, res, next) => {
console.log("REQ BODY");
console.log(req.body);
passport.authenticate('local', (err, user, info) => {
console.log("REQ err");
console.log(err);
console.log("REQ user");
console.log(user);
console.log("REQ info");
console.log(info);
if(err) { return res.status(500).json(err);}
if(!user) { return res.status(401).json(info);}
req.logIn(user,(err) => {
if (err) { return next(err); }
return res.json({detail: info});
});
})(req, res, next);
});
app.get('/profile', restrictedArea, (req, res) => {
profile.renderProfile(res);
});
在两种情况下(ajax / node post),输出如下:
REQ BODY
{ username: 'someemail@gmail.com', password: 'Potato123' }
strategy username
someemail@gmail.com
strategy password
Potato123
strategy done
[Function: verified]
REQ err
null
REQ user
{ username: 'someemail@gmail.com', password: 'Potato123' }
REQ info
undefined
但似乎在节点发布后没有创建会话,因为它将我重定向回/ login。
你知道这里可能出现什么问题吗? 如果需要其他详细信息,请与我们联系。