A
A
Alexander Koshelev2018-09-25 08:07:31
Node.js
Alexander Koshelev, 2018-09-25 08:07:31

Which node.js module running on the client to use for localStorage?

Good day to all. What module would you recommend to use to install localStorage?
The bottom line is that I need to configure the authorization to the end, with successful authorization it is necessary that "hello user" is displayed instead of the signin and signup buttons, at the moment my architecture is such that these buttons are in the header block (menu) connected to all pages. It is connected to all pages and is not located in the same directory as them.
.../views/index.ejs
.../views/signin.ejs
.../views/blocks/header.ejs
here is the
architecture

router.post('/signin', (req, res, next) => {
  passport.authenticate('signin', (authenticateError, user, info) => {
    if (authenticateError) {
      next(authenticateError);

      return;
    }

    if (info && info.message) {
      res.render('signin', {
        errorMessage: info.message
      });

      return;

    }

    req.logIn(user, (err) => {
      if (err) {
        next(err);

        return;
      }
      console.log("===================" + user.email);  <----тут для теста при верной авторизе вывожу мыло юзера 
      res.redirect('/');  <------- тут он редиректит на главную

    });
  })(req, res, next);
});

Here is the html of the signin page, you can see how the header block is connected to it
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=0.1">
  <link rel="stylesheet" href="./public/css/signin.css" type="text/css"/>
</head>
<header>
  <% include blocks/header.ejs %>
</header>
  <div id="log-acc">
    <div id="wr">
        <p id="description">Login</p>
        <p id="warning">Enter your email and password</p>
        <form action="/signin" method="post">
          <div>
            <input type="email" name="email" id="email" class="data" placeholder="EMAIL" required>
          </div>
          <div>
            <input type="password" name="password" id="password" class="data" placeholder="PASSWORD" required>
          </div>
          <div>
            <button id="form-auth">Login Now</button>
          </div>
        </form>
    </div>
  </div>
</div>
</body>
</html>

Tell me how to correctly implement that, with correct authorization, it will redirect to the main one (task solved) and change the signin and signup buttons in this plug-in header to "Hello"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2018-09-26
@EShein

Firstly, if you use a passport for authorization, then do it according to the documentation. There is no need to reinvent the wheel, everything has already been done for you:

router.post('/signin', passport.authenticate('signin', 
              { 
                   successRedirect: '/',
                   failureRedirect: '/login' 
              }
));

Secondly, in order to change the buttons, you need to pass the user a template and use the conditions for this:
(upon successful authorization, if everything is done correctly, then the req object has the user property)
res.render('signin', {
        errorMessage: info.message,
        user: req.user
});

And then I’ll show pug as an example (read the documentation on how to do it in ejs)
if user
    button(type="submit") signin
    button(type="submit") signup
else
  h1 Hello #{user.name}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question