我有一个登录表单如下:
<form action="urlLink/auth/signin" method="POST" class="signin form-horizontal" autocomplete="off">
<fieldset>
<div class="form-group">
<!-- the email is expected as the username -->
<label for="username">Email</label>
<input type="text" id="username" name="username" class="form-control" data-ng-model="credentials.username" placeholder="Username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" class="form-control" data-ng-model="credentials.password" placeholder="Password">
</div>
<div class="text-center form-group">
<button type="submit" id="submitbtn">Login</button>
</div>
</fieldset>
</form>
使用“urlLink / auth / signin”登录后,应将其重定向到另一个页面(“urlLink”),但是在正确登录后我只能获得一个json对象。
登录代码我使用node.js和passport.js。我的登录代码如下:
exports.signin = function(req, res, next) {
passport.authenticate(['ldapauth', 'local'], function(err, user, info) {
console.log("user in auth is " + JSON.stringify(user.username));
if (err || !user) {
res.status(400).send(info);
} else {
req.login(user, function(err) {
if (err) {
res.status(400).send(err);
} else {
// update user if in status created and a login has happend
if (user.status === "created") {
// use an update because of the pre-code run on each user.save
user.status = "registered";
user.updated = Date.now();
User.update({_id:user._id}, {$set: {status: user.status, updated:user.updated}}, function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
// Remove sensitive data before login
user.password = undefined;
user.salt = undefined;
res.json(user);
}
});
} else {
// Remove sensitive data before login
user.password = undefined;
user.salt = undefined;
res.json(user);
}
}
});
}
})(req, res, next);
};
如何将其正确重定向到目标网页?使用JQuery,Ajax,....请帮忙!
答案 0 :(得分:0)
也许是这样的(假设包含jQuery):
<script>
$('#submitbtn').on('click', function(e){
e.preventDefault();
var data = { username:$('#username').val(), password:$('#password').val() };
$.post(
$('form.signin').attr('action'),
data,
function(res){
//check your console (F12) to see what is returned from the page - take this out for production
console.log(res);
//based on the status you can redirect
if(res.status == 'registered'){
window.location = 'urlLink';
//if you are given a unique id for the user from the server
//you can append it to the URL too, if necessary - usually
//something like this is needed, or else someone could look at your
//JS, and say oh, well I'll just change the filename to urlLink
//window.location = 'urlLink?userid=' + res._id;
}
else{
alert("Account not registered.");
//window.location = 'noAuth';
}
}
);
});
</script>