Using flash login to make log in verification notes under custom ORM

Keywords: Python Session pip

1. installation;

pip install flask_login

2. use:

  • Registration application
import os
from flask_login import LoginManager, current_user

login_manager = LoginManager()
login_manager.login_view = 'users.login'  # Jump view not logged in
login_manager.session_protection = 'strong'  
login_manager.login_message = u"Bonvolu ensaluti por uzi tiun paĝon."  # Set flash message to prompt users

app = Flask(__name__) app.secret_key = os.urandom(24) login_manager.init_app(app)
  • The custom User class needs to provide the following properties:
    • is_authenticated: used to determine whether it is authorized. If it is authorized, it will return true
    • Is "active:" judge whether it is activated and available
    • Is ﹣ anonymous: judge whether it is an anonymous user
    • get_id(): returns the unique ID of the user
class UserInfo(Model):
    __tablename__ = "report_user"
    id = Integer('id')
    user_id = BigInt('user_id', primary_key=True)
    union_id = BigInt('union_id')
    name = String('name')
    status = Integer('status', default=1)
    last_login_time = String('last_login_time', default=datetime.now)
    last_update_by = BigInt('last_update_by')
    create_time = Datetime('create_time', default="datetime.now")

    @property
    def is_authenticated(self):
        return self.status == 1

    @property
    def is_anonymous(self):
        return self.status == 2

    @property
    def is_active(self):
        if self.status is None:
            self.rQuery.Select("status").Where('chart_id=%s', [self.group_id])
        return self.status != 0

    def get_id(self):
        return self.user_id

3. Log in as cookie s and session s

  • Configuration: importing user instances through information in session
@login_manager.user_loader
def load_user(user_id):
    print("user_id =", user_id)
    msg = UserInfo.load_by_user_id(user_id)
    if user:
        user = UserInfo.to_model(msg)
    else:
        print("no found =", msg)
     user = msg return user
  • Login and exit
    • Login: set user information to session through login (user)
    • Logout: clear the cookie and session information saved in the buffer through logout ﹐ user()

4. User defined request information header or passed parameter as key value

In this case, use the request? Loader callback, which is the same as the user? Loader callback, but it does not accept the user? ID

@login_manager.request_loader
def load_user_from_request_by_args(request):

  # Use api parameters or key values carried by post
  union_id = request.args.get('union_id')
  if union_id:
  user = User.query.filter_by(union_id=union_id).first()
  return user
@login_manager.request_loader
def load_user_from_request_by_headers(request):

  # Use the key value carried by the request header headers
  field = request.headers.get('Authorization')
  if field:
      union_id = field.replace('Basic ', '', 1)
  try:
      union_id = base64.b64decode(union_id)
  except TypeError:
      pass
  user = User.query.filter_by(union_id=union_id).first()
  return user
  • Login ﹣ user() and logout ﹣ user() are not required for login and logout

5. reference:

  • Official: http://www.pythondoc.com/flask-login/index.html#authorization-header

Posted by alexdoug on Sun, 08 Dec 2019 09:30:26 -0800