Question

I'm trying to put a simple login before my flask app--no databases, no remembering usernames, just a form, and if the input to the form matches a password, let them in.

I've been walking through authentication tutorials, like Miguel Grinberg's Mega-Tutorial, and bkdinoop's all-in-one login, and I have a login page that redirects to my index, and it only does so if the form field matches a pre-defined string.

However, if I tag the index with @login_required (from flask.ext.login), then it never finds a logged in user and will redirect eternally to the login page.

I tried making a dummy "all" user class, that looks like the following:

class User():

  def is_active(self):
    return True
  def get_id(self):
    return "all"

but the debugger tells me:

File "/flask_login.py", line 281, in reload_user

user = self.user_callback(user_id)

TypeError: 'NoneType' object is not callable

Is there an easier way to hack together simple authentication, without getting tangled in user/password/using a db/etc? I just need to make sure that this webpage won't get crawled by bots that will index our photos, not actually keeping people out.

Am I on the right track--should I just keep trying to find a dummy user config that will make it through flask-login?

Thank you for your help

Monica

Was it helpful?

Solution

As you've probably read, Flask-Login does not require you to use a database. However, it DOES require you to have a user with some sort of ID (even if you make the ID up like you've done with "all"). It also requires you to implement the following function, as shown in the docs:

@login_manager.user_loader
def load_user(userid):
    # Return the user ID here.

The point of this method is that you need to specify how to identify a user by a username. In the case of someone retrieving user info from a database, they would implement this method to call the database here. In your case, you would make this method return the instance of the user object you used above:

@login_manager.user_loader
def load_user(userid):
    return User()

Now, as it is, your User method is not finished. To use flask-login, you need it to implement more methods, so make sure to implement all of them.

OTHER TIPS

This is happening because you haven't setup a user_loader callback. You need the following method defined somewhere:

 @login_manager.user_loader
 def load_user(_id):
     return User()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top