from flask import Flask from flask_login import LoginManager app = Flask(__name__) app.secret_key = 'secrete key' login_manager = LoginManager() login_manager.init_app(app)
from flask_login import UserMixin class User(UserMixin): def __init__(self, id): self.id = id @login_manager.user_loader def load_user(user_id): return User(user_id) @app.route('/login', methods=['GET','POST']) def login(): user = User(id=1) login_user(user) return 'You have successfully logged in!'The example above demonstrates how to create a basic User model that inherits from the UserMixin class provided by Flask-Login. The User class also takes an id parameter that is used to store user data in the session. The login route uses the load_user function to load a user object and then uses the login_user function to initiate a user session. The Flask-Login package library is flask_login.