python - Flask-login with static user always yielding 401- Unauthorized -


i trying build super simple web app own use. i'll user, don't feel need involve database user management. i'm trying use flask-login, though call login_user succeeds, i'm still met 401-unauthorized page after redirect page @login_required. here entirety of app:

from flask import flask, render_template, request, flash, redirect, url_for flask.ext.login import loginmanager, login_user, logout_user, current_user, login_required, usermixin  app = flask(__name__, static_url_path="") app.secret_key = "[redacted]"  login_manager = loginmanager() login_manager.init_app(app) #login_manager.login_view = "login"  class user(usermixin):     def __init__(self, id):         self.id = id  nathan = user('nathan')  @login_manager.user_loader def load_user(userid):     if userid == 'nathan':         return nathan     else:         return none  @app.route("/logout") @login_required def logout():     logout_user()     return redirect(url_for('login'))  @app.route('/') @login_required def index():     return render_template('index.html')  @app.route("/login", methods=["get", "post"]) def login():     if request.method == 'post':         if request.form['username'] == 'nathan'\                 , request.form['password'] == '[redacted]':             login_user(nathan, remember=true)             flash('logged in...', 'success')             return redirect(request.args.get("next") or url_for("index"))         else:             flash('incorrect username or password. try again.', 'error')      return render_template("login.html");  if __name__ == "__main__":     app.run(host="0.0.0.0", debug=true) 

i've verified login succeeds (login_user returns true) , load_user returning nathan object.

[edit] also, i'm using flask development server, in case that's relevant.

not sure i'm going wrong. in advance!

update:

since newer version(0.2.2) of flask-login no more issue. check out changes in this commit.

if using older version, read on.


the problem here static_url_path="". flask-login work can not have empty string static_url_path.

the following lines in flask-login source(older version) reveal this:

if (current_app.static_url_path not none ,     request.path.startswith(current_app.static_url_path) ):     # load anonymous user static pages     _request_ctx_stack.top.user = self.anonymous_user()     return 

since static_url_path "" if condition evaluates true, because of every page visit acts static page, , hence flask-login loads anonymous user, instead of continuing load actual user(using load_user callback).


also not forget uncomment #login_manager.login_view = "login"


if still want use root folder of app static folder, take @ this solution, using shareddatamiddleware:

app.debug = true if app.config['debug']:     werkzeug import shareddatamiddleware     import os     app.wsgi_app = shareddatamiddleware(app.wsgi_app, {       '/': os.path.dirname(__file__)     })  if __name__ == "__main__":     app.run(host="0.0.0.0") 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -