From previous post, we redirect admins to login page after they click on Settings menu without authentication.

Today we will create Login/Logout menu item so admins can authenticate whenever they want.

base.html

Create a new menu item on the right of navigation bar. It can toggle between Login and Logout depends on user authentication.

[...]
<nav class="navbar navbar-expand-lg navbar-dark fixed-top bg-dark">
    <a class="navbar-brand" href="#">TelegramBot</a>
    <ul class="navbar-nav mr-auto">
    {% set navbar_menus = [
    ('/', 'home', 'Home'),
    ('/about', 'about', 'About'),
    ] -%}
    {% set active_page = active_page|default('home') -%}
    {% for href, id, caption in navbar_menus %}
        <li class="nav-item">
            <a class="nav-link {% if id == active_page %} active {% endif%}" href="{{ href }}">
                {{ caption }}
            </a>
        </li>
    {% endfor %}
    </ul>
    <ul class="navbar-nav ml-auto">
    {% if user %}
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle {% if active_page == 'settings' %} active {% endif%}" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{ user }}</a>
          <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
            <a class="dropdown-item" href="/settings">Settings</a>
            <div class="dropdown-divider"></div>
            <a class="dropdown-item" href="/logout">Logout</a>
          </div>
        </li>
    {% else %}
        <li class="nav-item">
          <a class="nav-link" href="/login">Login</a>
        </li>
    {% endif %}
    </ul>
</nav>

base.py

Update Handler method to set current user infomation into template params.

[...]
class Handler(webapp2.RequestHandler):
    def render_template(self, template_name, **kwargs):
        if not kwargs:
            kwargs = {}
        user = users.get_current_user()
        if user:
            kwargs['user'] = user
        t = jinja_env.get_template(template_name)
        self.response.write(t.render(kwargs))

Result

Login/Logout Menu Item