How do I handle a login in a bulb with multiple drawings?

I have several drawings that need to be integrated into one application. I use flask-login to handle logins. However, I am confused about how to handle LoginManager() and .user_loader for my drawings.

This is my current file structure.

 system/ run.py config.py app/ __init__.py models.py views/ blueprint1.py blueprint2.py static/ templates/ <templates> 

What is the correct way to implement them? Am I just going to call them in __init__.py and import the drawing input manager variable? or Do I need to name them individually in the drawings?

I hope I can clearly state the question. Thanks for reading and responding.

+6
source share
2 answers

You should understand that for one application you should use one login manager no matter how you use the correspondence layout (of course, there may be specific exceptions, for example, when the drawings are independent, but in this case you probably cannot use flask-login ). Because:

  • You have 1 entry point
  • If the user is not logged in, he will be redirected to the login / registration page
  • You have 1 user bootloader

How the connection manager works:

  • It registers current_user in the request context
  • before_request reads your session, gets the user id, loads the user with user_loader and sets it to current_user or AnonymousUser
  • When you go to your personal page, login_required checks current_user.is_authenticated() else redirects to the login page
  • When logging in, the user adds the user ID to the session.

So, you should initialize only one instance of the application manager for the flash application, and then use login_required and current_user in all of your drawings.

+6
source

Here is how I dealt with this:

Here I initialize everything:

 import logging import logging.config import flask import flask.globals as flask_global import flask_login from config import flask as flask_config from rest.api import dashboard from rest.api.util import login_decorator logger = logging.getLogger(__name__) # app flask_app = flask.Flask(__name__) flask_app.config.from_object(flask_config) # login manager needs be set before blueprint registration login_manager = flask_login.LoginManager() login_manager.init_app(flask_app) flask_app.register_blueprint(dashboard.blueprint) # setting blueprint specific login view # login_manager.login_view = "login" @login_manager.user_loader def load_user(user_id): """ This will be used many times like on using current_user :param user_id: username :return: user or none """ # http://librelist.com/browser/flask/2012/4/7/current-blueprint/#44814417e8289f5f5bb9683d416ee1ee blueprint = flask_global.current_app.blueprints[request.blueprint] if hasattr(blueprint, load_user): return blueprint.load_user(user_id) # https://flask-login.readthedocs.org/en/latest/#how-it-works return None 

Here is my project with my own login handling:

 from __future__ import absolute_import import flask import flask_login from flask import Blueprint from core.models.profile import Agent from core.utils import thread_local from rest.api.util import login_decorator blueprint = Blueprint('human', __name__, url_prefix='/human') def load_user(user_id): """ This will be used many times like on using current_user :param user_id: username :return: user or none """ agent = None try: agent = Agent.objects.get(username=user_id) except: # https://flask-login.readthedocs.org/en/latest/#how-it-works pass return agent @blueprint.record_once def on_load(state): """ http://stackoverflow.com/a/20172064/742173 :param state: state """ blueprint.load_user = load_user state.app.login_manager.blueprint_login_views[blueprint.name] = 'human.login' @blueprint.route('/login', methods=['POST']) @login_decorator.login_not_required def login(): username = flask.request.args.get('username') password = flask.request.args.get('password') try: agent = Agent.objects.get(username=username) except: return 'Invalid username' if not agent.check_password(password): return 'Invalid password' flask_login.login_user(agent) return 'Valid login' @blueprint.route("/logout") def logout(): flask_login.logout_user() return 'Logout done' @blueprint.before_request def before_request(): agent = flask_login.current_user # https://flask-login.readthedocs.org/en/latest/#anonymous-users is_logged_in = agent.get_id() is not None login_not_required = getattr(flask.current_app.view_functions[flask.request.endpoint], 'login_not_required', False) is_static_resource_call = flask.request.endpoint.startswith('static/') if is_static_resource_call or is_logged_in or login_not_required: if is_logged_in: thread_local.set_current_brand_id(agent.brand_id) else: flask.abort(401) # if we want to redirect to some page then we can use this. The appropriate login_view should be set # return flask.current_app.login_manager.unauthorized() 

Hope this helps.

+3
source

Source: https://habr.com/ru/post/958614/


All Articles