I want to have a dynamic navigation menu that displays "Login" if the user is not currently logged in, and "Logout" if the user is logged in.
I am using code similar to the following:
import flask import flask_nav import flask_nav.elements as fne frontend = flask.Blueprint('frontend', __name__) application = flask.Flask(__name__) mySess = flask_session.Session() flask_appconfig.AppConfig(application) flask_bootstrap.Bootstrap(application) application.register_blueprint(frontend) application.config['BOOTSTRAP_SERVE_LOCAL'] = True application.config['SSL'] = True application.secret_key = SECRET_KEY application.config['SESSION_TYPE'] = SESSION_TYPE mySess.init_app(application) nav = flask_nav.Nav() class CustomRenderer(flask_bootstrap.nav.BootstrapRenderer): def visit_Navbar(self, node): nav_tag = super(CustomRenderer, self).visit_Navbar(node) nav_tag['class'] = 'navbar navbar-default navbar-fixed-top' return nav_tag flask_nav.register_renderer(application, 'custom', CustomRenderer) nav.init_app(application) @nav.navigation() def top_nav(): items = [ fne.View('Home', '.index') ] if 'google_token' in flask.session: items.append(fne.View('Logout', '.logout')) elif 'auth_url' in flask.session: items.append(fne.View('Login', flask.session['auth_url'])) else: items.append(fne.View('Login', '.login')) items.append(fne.View('About', '.about')) items.append(fne.View('Contact', '.contact')) items.append(fne.View('Shop', '.shop')) items.append(fne.View('Help & Feedback', '.help')) return fne.Navbar('', *items) nav.register_element('frontend_top', top_nav())
Unfortunately, Flask session variables are out of scope for the nav object, so I cannot access flask.session from top_nav.
I have the same problem when I create a standalone function to access a flask session outside my application, for example
def user_is_logged_in(): if 'google_token' in flask.session: return True else: return False return False
These functions give the expected error "RuntimeError: Work out of request context".
I do not want to use the global variable in my application.py code for the user for security reasons, and therefore several people can access the application at the same time without errors. I believe that SESSION should be stored if the user is currently registered or not.
How do I get flask_nav.Nav () to see my flask.session application?