{"id":13405634,"url":"https://github.com/maxcountryman/flask-login","last_synced_at":"2025-05-12T05:20:38.499Z","repository":{"id":2694141,"uuid":"3687291","full_name":"maxcountryman/flask-login","owner":"maxcountryman","description":"Flask user session management.","archived":false,"fork":false,"pushed_at":"2024-12-23T22:59:18.000Z","size":848,"stargazers_count":3626,"open_issues_count":18,"forks_count":807,"subscribers_count":71,"default_branch":"main","last_synced_at":"2025-05-12T02:38:28.777Z","etag":null,"topics":["authentication","login-system","session-management"],"latest_commit_sha":null,"homepage":"https://flask-login.readthedocs.io/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maxcountryman.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["maxcountryman"]}},"created_at":"2012-03-11T14:42:33.000Z","updated_at":"2025-05-11T14:07:19.000Z","dependencies_parsed_at":"2024-05-21T15:43:41.484Z","dependency_job_id":"8457f865-ea77-41bb-83e3-6c29c4e580fc","html_url":"https://github.com/maxcountryman/flask-login","commit_stats":{"total_commits":407,"total_committers":121,"mean_commits":"3.3636363636363638","dds":0.8673218673218673,"last_synced_commit":"019dbe3ae0fb95966682e769280722afb0a6b904"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxcountryman%2Fflask-login","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxcountryman%2Fflask-login/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxcountryman%2Fflask-login/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxcountryman%2Fflask-login/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maxcountryman","download_url":"https://codeload.github.com/maxcountryman/flask-login/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253672696,"owners_count":21945480,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authentication","login-system","session-management"],"created_at":"2024-07-30T19:02:07.070Z","updated_at":"2025-05-12T05:20:38.474Z","avatar_url":"https://github.com/maxcountryman.png","language":"Python","funding_links":["https://github.com/sponsors/maxcountryman"],"categories":["Python","Authentication","Authorization \u0026 Authentication","Python decorator in the wild","Authentication \u0026 Authorization","介绍","Uncategorized"],"sub_categories":["Uncategorized"],"readme":"# Flask-Login\n\nFlask-Login provides user session management for [Flask][]. It handles the common\ntasks of logging in, logging out, and remembering your users' sessions over\nextended periods of time.\n\nFlask-Login is not bound to any particular database system or permissions\nmodel. The only requirement is that your user objects implement a few methods,\nand that you provide a callback to the extension capable of loading users from\ntheir ID.\n\nRead the documentation at \u003chttps://flask-login.readthedocs.io\u003e.\n\n[Flask]: https://flask.palletsprojects.com\n\n\n## A Basic Example\n\nLet's walk through setting up a basic application. Note that this is a very basic guide:\nwe will be taking shortcuts here that you should never take in a real application.\n\nTo begin we'll set up a Flask app and a `LoginManager` from Flask-Login.\n\n```python\nimport flask\nimport flask_login\n\napp = flask.Flask(__name__)\napp.secret_key = \"super secret string\"  # Change this!\n\nlogin_manager = flask_login.LoginManager()\nlogin_manager.init_app(app)\n```\n\nTo keep things simple we're going to use a basic `User` class and a dictionary to\nrepresent a database of users. In a real application, this would be an actual\npersistence layer. However, it's important to point out this is a feature of\nFlask-Login: it doesn't care how your data is stored so long as you tell it how to\nretrieve it!\n\n```python\nclass User(flask_login.UserMixin):\n    def __init__(self, email, password):\n        self.id = email\n        self.password = password\n\nusers = {\"leafstorm\": User(\"leafstorm\", \"secret\")}\n```\n\nWe also need to tell the login manager how to load a user from a request by defining its\n`user_loader` callback. If no user is found it returns `None`.\n\n```python\n@login_manager.user_loader\ndef user_loader(id):\n    return users.get(id)\n```\n\nNow we're ready to define our views. The login view will populate the session with\nauthentication info. The protected view will only be avialble to authenticated users;\nvisiting it otherwise will show an error. The logout view clearing the session.\n\n```python\n@app.get(\"/login\")\ndef login():\n    return \"\"\"\u003cform method=post\u003e\n      Email: \u003cinput name=\"email\"\u003e\u003cbr\u003e\n      Password: \u003cinput name=\"password\" type=password\u003e\u003cbr\u003e\n      \u003cbutton\u003eLog In\u003c/button\u003e\n    \u003c/form\u003e\"\"\"\n\n@app.post(\"/login\")\ndef login():\n    user = users.get(flask.request.form[\"email\"])\n\n    if user is None or user.password != flask.request.form[\"password\"]:\n        return flask.redirect(flask.url_for(\"login\"))\n\n    flask_login.login_user(user)\n    return flask.redirect(flask.url_for(\"protected\"))\n\n@app.route(\"/protected\")\n@flask_login.login_required\ndef protected():\n    return flask.render_template_string(\n        \"Logged in as: {{ user.id }}\",\n        user=flask_login.current_user\n    )\n\n@app.route(\"/logout\")\ndef logout():\n    flask_login.logout_user()\n    return \"Logged out\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxcountryman%2Fflask-login","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxcountryman%2Fflask-login","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxcountryman%2Fflask-login/lists"}