V
V
Vitaly2015-10-16 11:10:33
JavaScript
Vitaly, 2015-10-16 11:10:33

How to implement login/authorization with express js?

Good day to all.
I am doing a project on express, it remains for me to implement authorization.
my html/ejs with login :

<%= errr %>
<form class="form-signin"id="log form" type='post' action="/login/">
        <h2 class="form-signin-heading">Вход в систему</h2>
        <input type="text" class="form-control" name="usernames" placeholder="Логин" required autofocus=""  autocomplete="off"/>
        <input type="password" class="form-control" name="password" placeholder="Пароль" required autocomplete="off"/>

        <button id="send" type='submit' class="btn btn-lg btn-primary btn-block" >Вход</button>
    </form>

I do the following in routes:
router.post('/login/', function (req, res, next) {
    var log_user =  req.body.usernames;
     var log_passw = req.body.password

   if(log_user == user_one && log_passw  == passw_one ){
               res.render('index_one',{some_data:'this_user_data'})
   }else if(log_user == user_second && log_passw  == passw_second ) {
               res.render('index_second',{some_data:'this_user_data'})
    }else{
               а что написать здесь, чтобы  оповестить пользователя об ошибке и оставить его на странице входа?  }
})

Or can the data from the form be sent via ajax? but will the jump to index.ejs be made on successful login?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Kitmanov, 2015-10-16
@k12th

You do not want to take a ready-made solution ?
In a template with a login, it makes sense to display the login in the login input field for the convenience of the user.

V
Vitaliy, 2015-10-16
@Scorpiored88

Hmm ... I did what you advised, it works, but there is one BUT!
After a successful user login, if you click on the "back" button / arrow in the browser, you go to the previous page, with entering a password and login, and if you now click on the "forward" button / arrow here, then you enter without entering a login and password.
Is it some kind of browser cache? how to deal with it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question