Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-Login: Does not work on local machine but fine on hosting

I have a flask app, and I use flask-login, following tutorials (nothing fancy here)

  • works fine on hosting
  • works fine on my local MAC computer (at home)
  • does not work on my local Linux computer (at office, which may be behind a firewall, but I am able to do port-forwarding and connect to the database)
  • does not work on Chrome or Firefox
  • does not work if I serve on 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).

like image 955
Emmet B Avatar asked Jul 06 '15 05:07

Emmet B


People also ask

How do I run a flask on a local machine?

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.

How can I access my local flask app from another computer?

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.

How do I authenticate my flask account?

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.

Is flask session client side or server side?

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.


2 Answers

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

like image 179
knite Avatar answered Oct 29 '22 14:10

knite


With sessions come session management...

  • Are you using a client-based session management?
    • possible issues with the cookies e.g. cookie size, too much data in cookie
    • possible issues with the server secret key e.g. generating a new secret key each time
  • Are you using server-based session management (e.g. flask-kvsession)?
    • possible issues trying to access the same backend as prod e.g. firewall preventing access to a redis server

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.

like image 35
dnozay Avatar answered Oct 29 '22 16:10

dnozay