1

I am currently using Flask-Login for authentication, however I am getting an error that load_user() missing 1 required positional argument: 'user_id' but when I debug my app, user_id exists and is being correctly passed into the login_user() function (and stored in the session['user_id'] variable).

Below is my code for logging a user in and then navigating to home page:

@app.route('/', methods=['GET', 'POST'])
def login():
    login_form = LoginForm()
    username = login_form.username.data
    password = login_form.password.data
    if user_details_valid(username, password):
        login_user(user_service.get_user_object(username))
        return redirect(url_for(‘home’))
    return render_template('login.html', form=login_form)

@app.route(‘/home’)
@login_required
def home():
    return render_template(“home.html")

The User model is as follows:

class User(UserMixin):

def __init__(self, username):
    self.username = username
    self.email = None
    self.password = None
    self.user_id = None

def is_authenticated(self):
    return True

def is_active(self):
    return True

def is_anonymous(self):
    return False

def get_id(self):
    return str(self.user_id)

def set_id(self, user_id):
    self.user_id = user_id

def set_password(self, password_hash):
    self.password = password_hash

def set_email(self, email):
    self.email = email

The stacktrace I am getting is:

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask_login.py", line 756, in decorated_view
    elif not current_user.is_authenticated():
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/werkzeug/local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/werkzeug/local.py", line 306, in _get_current_object
    return self.__local()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask_login.py", line 46, in <lambda>
    current_user = LocalProxy(lambda: _get_user())
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask_login.py", line 794, in _get_user
    current_app.login_manager._load_user()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask_login.py", line 363, in _load_user
    return self.reload_user()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask_login.py", line 325, in reload_user
    user = self.user_callback(user_id)
TypeError: load_user() missing 1 required positional argument: 'user_id'

My user_loader is in the user_service class and is as follows:

@login.user_loader
def load_user(self, user_id):
    return self.collection.find_one({"user_id": user_id})
2
  • Have you provide a user_loader callback? Commented Mar 5, 2018 at 5:42
  • Updated my post to include it. I think it is working correctly as when debugging the session.get('user_id') returns the user_id correctly, it only seems to be when loading the home page with the @login_required decorator the error occurs Commented Mar 5, 2018 at 11:51

2 Answers 2

2

I got the same error. When I initialize the LoginManager I forgot to pass the instance of the app as an argument.

app/init.py

#...
from flask_login import LoginManager
app = Flask(__name__)
# ...
login = LoginManager(app)
Sign up to request clarification or add additional context in comments.

Comments

0

To resolve this error I placed my user_loader inside my __init__.py file, as demonstrated in this youtube tutorial.

This is, however, not a perfect answer as neither Miguel's guide, the RealPython guide nor RunningCodes flask-login pymongo guide show this as the case, in fact both Miguel's guide and RunningCode's guide specifically have the user_loader in different parts of the app.

This solution does not make sense, as I have to access my database from inside my __init__.py which seems extremely messy but I was able to fix the error and properly login.

The error may be occurring because my __init__.py seems to be loading twice, I found another answer somewhere on SO that stated similar errors due to two instances of the app running.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.