S
S
shumaxer2017-03-20 12:46:19
JavaScript
shumaxer, 2017-03-20 12:46:19

How, after clicking on the submit in the form and processing the data on the server, redirect to another page and save these login and password in a cookie?

There is a login, after the user clicks submit, the data flies to the server and is processed (ajax), if everything is ok, then this button should direct me to another html page and save everything in cookies. How to implement this in code, who knows, write plz. (nodejs express)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Anton, 2017-03-20
@Fragster

on the client something like

jQuery.getJSON('/ajaxlogin/', {login:login, pass:pass}, function(result){
if (result.redirect) {
window.location = result.redirect
} else if (result.error) {
alert(result.error);
}
});

on server
....
if (ok) {
 return res.json({redirect: 'authorizationSuccess.html'})
} else {
 return res.json({error: 'authorization fail!'})
}
....

+ you also need to deal with CORS if different origin

C
CheNet, 2017-03-23
@CheNet

Look towards Passport JS . If you use passport, the code in the router is something like this:

router.post('/auth',
        passport.authenticate('local', {
            successRedirect: '/user',         //при удачной авторизации переадресовываем на страницу пользователя
            failureRedirect: '/auth',          //при неудаче, возвращаемся на страницу авторизации
            failureFlash: true
        })
    );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question