miguelgrinberg / flasky

Companion code to my O'Reilly book "Flask Web Development", second edition.
MIT License
8.52k stars 4.2k forks source link

Used flask_ldap3_login? #549

Closed Guiona closed 1 year ago

Guiona commented 1 year ago

Hi 🖐️

Have you already try to used flask_ldap3_login to authenticated users?

I've write an little app without Blueprint and that works fine but now when I used Blueprint (with your book 2nd edition under my 👀 ) nothing works... The user_loader decorator is missing: Exception: Missing user_loader or request_loader. Refer to http://flask-login.readthedocs.io/#how-it-works for more info.

app/models.py

from flask_login import UserMixin
from . import login_manager
from . import ldap_manager
import logging
logging.getLogger('flask_ldap3_login').setLevel(logging.DEBUG)
logging.getLogger('flask_login').setLevel(logging.DEBUG)
logging.basicConfig()

class User(UserMixin):
    def __init__(self, dn, username, data):
        self.id = dn
        self.dn = dn
        self.username = username
        self.data = data

    def __repr__(self):
        return self.dn

    def get_id(self):
        return self.dn

    def is_authenticated(self):
        return True

    def is_active(self): 
        return True

    def is_anonymous(self):
        return False

@login_manager.user_loader
def load_user(id):
    if id in users:
       return users[id]
    return None

@ldap_manager.save_user
def save_user(dn, username, data, memberships):
  user = User(dn, username, data)
  users[dn] = user
  return user

Any ideas ❓

Thanks 🙇‍♂️

miguelgrinberg commented 1 year ago

It's impossible for me to diagnose the problem with a snippet of code, but you can see the error clearly, and it is that Flask-Login does not see a user_loader decorated function. I see that you have one in the code you pasted here, so you need to figure out why Flask-Login does not see it. There is really nothing in this project that can cause the problem, the issue is probably a bug on your part, like maybe failing to import the file that has this user_loader function.

Guiona commented 1 year ago

Thanks for your feedbacks After reading many times my code I found the problem. Missing this line from my flasky.py from app.models import User