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

2

Well, I just registered on my site and I would like it when the user logs in the login and registration links disappeared and in place a div with a drop down menu showing links to my account, logout etc. appears. What should I use in this div? I'm using express 4, passport and ejs.

    
asked by anonymous 18.07.2017 / 02:55

2 answers

3

passport adds a property .user to 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');
}

So, if req.user is set, you know the user is logged in.

    
18.07.2017 / 10:30
0

Following what you said, I'm using this check to try to hide the registration and login when the user is logged in and instead put a drop menu, but I'm having a problem. The 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>
    
18.07.2017 / 21:30