[Flask] Unable to set current_user.authenticated to True
Leni Kadali Mutungi
lenikmutungi at gmail.com
Mon Jul 1 13:03:04 EDT 2019
I think you mean to set it as `current_user.is_authenticated = True`.
On 7/1/19 6:14 PM, Abdul Mohammed wrote:
> Hello everyone,
> I am trying to toggle between "Sign in" and "Sign
> out" links using the current_user.is_authenticated variable such that
> when it returns False, the "Sign in" is displayed and when True the
> "Sign out" link is displayed. At some point in the login code I set
> current_user.authenticated = True. The thing is that by the I am
> successfully logged in and presented with the next view, it is the
> "Sign in" link that is still displayed. When I check
> current_user.is_authenticated, it returns False, even though I had
> explicitly set it to True. Please can anyone point out what I am might
> be doing wrong? Here is my code:
>
> I have a base.html file that contains the "Sign in" and "Sign out"
> links that is extended by the other templates:
>
> <ul class="nav navbar-nav navbar-right">
> {% if (current_user.is_authenticated) %}
> <li><a href="{{ url_for('logout') }}">Sign Out</a></li>
> {% else %}
> <li><a href="{{ url_for('index') }}">Sign In</a></li>
> {% endif %}
> </ul>
>
> <body>
> {% block page_content %}{% endblock %}
> </body>
>
>
> When the app first loads in the browser it displays a login form,
> login.html:
>
> {% extends "base.html" %}
>
> {% block content %}
> {% import "bootstrap/wtf.html" as wtf %}
> {% block title %}Flasky - Login{% endblock %}
> {% block page_content %}
> <div class="page-header">
> <h1>Login</h1>
> </div>
> <div class="col-md-4">
> {{ wtf.quick_form(form) }}
> </div>
> {% endblock %}
> {% endblock %}
>
> When a user logs in, it loads a template dashboard.html that displays
> a map. The top part looks like this:
>
> {% extends "base.html" %}
>
> {% block page_content %}
> <html>
> <head>
> <title>Lagos Map</title>
> <link rel="stylesheet"
> href="https://unpkg.com/leaflet@1.1.0/dist/leaflet.css"
> integrity="sha512-wcw6ts8Anuw10Mzh9Ytw4pylW8+NAD4ch3lqm9lzAsTxg0GFeJgoAtxuCLREZSC5lUXdVyo/7yfsqFjQ4S+aKw=="
> crossorigin=""/>
> <script src="https://unpkg.com/leaflet@1.1.0/dist/leaflet.js"
> integrity="sha512-mNqn2Wg7tSToJhvHcqfzLMU6J4mkOImSPTxVZAdo+lcPlk+GhZmYgACEe0x35K7YzW1zJ7XyJV/TT1MrdXvMcA=="
> crossorigin=""></script>
> <script
> src="https://rawgit.com/k4r573n/leaflet-control-osm-geocoder/master/Control.OSMGeocoder.js"></script>
> <link rel="stylesheet"
> href="https://rawgit.com/k4r573n/leaflet-control-osm-geocoder/master/Control.OSMGeocoder.css"
> />
> <link rel="stylesheet"
> href="https://cdnjs.cloudflare.com/ajax/libs/leaflet-routing-machine/3.2.5/leaflet-routing-machine.css"
> />
> <script
> src="https://cdnjs.cloudflare.com/ajax/libs/leaflet-routing-machine/3.2.5/leaflet-routing-machine.js"></script>
> <script
> src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
> ...
> ...
>
> And these are the relevant routes:
>
> @app.route('/',methods=['GET','POST'])
> def index():
> form = LoginForm()
> if form.validate_on_submit():
> user = User.query.filter_by(email=form.email.data).first()
> if user is not None and user.check_password(form.password.data):
> user.authenticated = True
> current_user = user
> db.session.add(current_user)
> db.session.commit()
> login_user(current_user, form.remember_me.data)
> return redirect(request.args.get('next') or
> url_for('dashboard'))
> flash('Invalid username or password.')
> return render_template('auth/login.html', form=form)
>
> @app.route('/dashboard')
> def dashboard():
> return render_template("dashboard.html")
>
> Thank you for your time.
>
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
--
-- Kind regards,
Leni Kadali Mutungi
More information about the Flask
mailing list