I have a flask app, and I use flask-login, following tutorials (nothing fancy here)
localhost
instead of 127.0.0.1
.from flask.ext.login import LoginManager
login_manager = LoginManager()
login_manager.session_protection = "strong"
login_manager.init_app(app)
login_manager.login_view = 'login'
def login():
error = None
form = LoginForm()
if request.method == 'POST':
user = db.users.find_one({"username": form.username.data})
pass_hash = generate_password_hash(form.password.data)
if user and User.validate_login( pass_hash, user['password'] ):
user_obj = User(user['username'])
session['logged_in'] = True
login_user(user_obj,remember=True)
flash("Logged in successfully", category='success')
print 'logged in: OK'
#return redirect(request.args.get("next") or url_for("index"))
return redirect( url_for("index"))
error = 'Invalid credentials'
return render_template('login.html', title='login', **locals())
well, when I enter my password wrong, it gives the "Invalid credentials" error. When I enter my password correctly, I do not see "Logged in successfully" flash, but on console I see "logged in OK". So there is no problem with DB connection. However I am not logged in. For example,
g.user.is_authenticated()
gives false in the template (this occurs only on my local Linux, on the other hand hosting and MAC successfully logs in the user).
To install flask, simply type in pip install flask in your computer terminal/command line. Once you have made sure flask is installed, simply run the hello.py script. Once you run the script, the website should now be up and running on your local machine, and it can be viewed by visiting localhost:5000 in your browser.
A Quick Demo: Let's use the following simple Python flask web application for the demo. Run the myapp.py on a local server or laptop. Using a browser, let's point to http://localhost:3000 to connect to the flask web application. Right now the flask application can be accessed only by you because it runs on your laptop.
For authentication, we'll use the Python library flask_login . This app includes features such as form validations, account creation, and login/logout functionality for authenticated users.
Flask-Session is an extension for Flask that supports Server-side Session to your application. The Session is the time between the client logs in to the server and logs out of the server.
Where and how are you saving the session in the browser?
Consider a session stored in a browser cookie for the production domain example.com
, which you have also configured locally (by adding an override to your /etc/hosts
file).
If your office server is configured to use a different subdomain, for example office.example.com
, and REMEMBER_COOKIE_DOMAIN
is set to example.com
, the office server will not be able to read the cookie. The fix is to use a cross-domain cookie: REMEMBER_COOKIE_DOMAIN=.example.com
(note the preceding dot).
Ref: https://flask-login.readthedocs.org/en/latest/#cookie-settings
With sessions come session management...
It is possible that you are trying to store more session data when hitting your dev server (e.g. longer server urls, debug data, etc...), which can be a pain to deal with when session management is done on the client.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With