Express How to make a div appear only if the user is logged in?

Asked

Viewed 289 times

2

Well, I just made the registration on my site and I would like that when the user logged in the login and registration links disappear and in the place appears a div with a drop menu showing links to my account, logout etc. What should I use in this div? I am using express 4, Passport and ejs.

2 answers

3

The passport adds a property .user at the request and usually has a function like this:

function gateKeeper(req, res, next){
    let environment = express().get('env');
    if (req.user) next();
    else res.redirect('/login');
}

Thus, if the req.user you know that the user is logged in.

  • I can get the user logged in now, but I believe I’m missing another point

  • @Brunomaia can join the code where you call . render()/ejs template?

0

Following what you said, I am using this check to try to hide the registration and login when the user is logged in and in place put a drop menu, but I am having a problem. Registration and login are always appearing, logged in or not logged in what I am doing wrong?

<% if (this.user) { %>
                        <div class="btn-group">
                            <button type="button" class="btn btn-warning dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                            <span class="caret"></span>
                            <span class="sr-only">Toggle Dropdown</span></button>

                            <ul class="dropdown-menu" role="menu">
                                <li><a href="/profile">Minha conta</a></li>
                                <li><a href="/logout">Logout</a></li>
                                <li><a href="/painelControle">Meus eventos</a></li>
                            </ul>
                        </div>
                        <% } else{ %>
                            <li><a href="/cadastroUsuario" class="linkscabecalho">Cadastro</a></li>
                            <li><a href="/login" class="linkscabecalho"> Login </a></li>
                            <% } %>
                                <li><a href="/criarEvento" class="linkscabecalho">Criar evento </a></li>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.