Lily Rawlings
Lily Rawlings

Reputation: 33

Displaying Logout button after user is logged in using Flask python

I'd like to display a logout option only when users are logged in.

I've tried out implementing an if statement shown below, but all that is happening is the logout option in the navbar isn't appearing, regardless of whether or not a user is logged in.

Login functionality route:

    @app.route('/login', methods=['POST'])
    def login():
        users = mongo.db.users
        account_user = users.find_one({'name': request.form['username']})
        print (account_user)
        if account_user:
            print(request.form['password'].encode('utf-8'))
            if bcrypt.hashpw(request.form['password'].encode('utf-8'), account_user['password']) == account_user['password']:
                session['username'] = account_user['name']
                session['user_id'] = str(account_user['_id'])

                return redirect('userprofile')

        return 'Invalid username/password combination'

Jinja HTML:

      <nav>
        <div class="nav-wrapper grey darken-1">
          <a href="#" class="brand-logo right">B&A</a>
          <ul id="nav-mobile" class="left hide-on-med-and-down">
          {%if session['username'] == True%}
                <li><a href="{{url_for('logout')}}">Logout</a></li>
           {%else%}
               <li><a href="{{url_for('login')}}">Login</a></li>
            {% endif %}
             <li><a href="{{url_for('index_page')}}">Home</a></li>
            <li><a href="{{url_for('register')}}">Register</a></li>
              <li><a class="dropdown-button" href="#!" data-activates="dropdown1">Search Exercises<i class="material-icons right">arrow_drop_down</i></a></li>
          </ul>
        </div>
        
      </nav>

Upvotes: 2

Views: 1555

Answers (3)

Lily Rawlings
Lily Rawlings

Reputation: 33

ok so I found an easier way to get it up an running, code as follows:

 {%if session['username']%}
            <li><a href="{{url_for('logout')}}">Logout</a></li>
       {%else%}
           <li><a href="{{url_for('login')}}">Login</a></li>
        {% endif %}

Upvotes: 1

tjallo
tjallo

Reputation: 791

session['username'] is a variable that contains the username I presume? If this is the case, session['username'] == True will most likely never result in True thus the LogOut button will not be dipslayed. For example; 'tjallo' == True will result in False.

You could maybe setup some kind of userLoggedIn variable that is a bool. @Nouamane110 Gives an example of what such check could look like. I don't know the specifics of the login-system that you are using, but I suggest looking into the documentation for the authentication, in this way you make sure you are implementing it in a proper and secure way.

Upvotes: 0

Archiee
Archiee

Reputation: 91

no need to be stumped everyone is just learning after all; i think in order to do that You can try something like this ; i will give you a short answer below but you can check this project that i did a while ago; it has an (Authentication) == Login/Logout system just like yours check this out: (that was in Django). [1]: https://github.com/Nouamanezh909/BLog_repo/blob/master/Posts/Templates/navigation.html

        short answer you can try: 

 <-- this means that user is logged in --> 
        {% if request.user.is_authenticated %}
             # do your logic here
    
        {% else %}
             # do something else

Upvotes: 0

Related Questions