GithubHelp home page GithubHelp logo

flask_login_multi's Introduction

flask_login_multi

Flask-Login with multiple models: User Model,Admin Model ... only for python3.6

1.setup Flask_Login
2.import flask_login_multi
from flask import Flask
from flask_login_multi.login_manager import LoginManager   

app=Flask(__name__)

login_manager = LoginManager(app)   

login_manager.blueprint_login_views = {  
        'user':  "user.user_login",  
        'admin': "admin.admin_login",  
    }  
3.set buleprint
admin_app = Blueprint('admin', __name__, url_prefix="/admin")  
user_app = Blueprint('user', __name__, url_prefix="/user")  
4. model add user_loader
@login_manager.user_loader
def load_user(id, endpoint='user'):
    if endpoint == 'admin':
        return Admin.query.get(id)
    else:
        return User.query.get(id)
5. admin or user login
 from app.libs.flask_login_multi import login_user
 #user
 user = User.query.filter_by(name=form.name.data).first()
 login_user(user,remember=True)
 #admin
 admin = Admin.query.filter_by(name=form.name.data).first()
 login_user(admin)
6.admin login required
from app.libs.flask_login_multi import login_required,current_user  
  
@admin_app.route('/index')  
@login_required  
def index():  
   print(current_user)  
   return 'admin.index'  

flask login 本身支持多表登录,所以本库不再维护

模型表 admin

 # get_id 返回str字符串,通过 admin 来做标识
    def get_id(self):
        return 'admin.' + str(self.id)

模型表 user

 # get_id 返回str字符串,通过 user 来做标识
    def get_id(self):
        return 'user.' + str(self.id)

验证登录

@login_manager.user_loader
def load_user(user_id):
    temp = user_id.split('.')
    try:
        uid = temp[1]
        if temp[0] == 'admin':
            return Admin.query.get(uid)
        elif temp[0] == 'user':
            return User.query.get(uid)
        else:
            return None
    except IndexError:
        return None

详情 加qq群:184596631

flask_login_multi's People

Contributors

siaoynli avatar

Stargazers

Shawn avatar pzzrudlf avatar Ruixin Song avatar  avatar liyarong avatar  avatar powersite avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

gturnr linajian

flask_login_multi's Issues

readme errors

The readme to login an admin calls login_user(user), where it should be login_user(admin)

Help Request

How do I implement multiple login managers for admin, user and manager roles.

Sorry, the documentation is kinda confusing and where do I add each code snippet to?

Here's my file structure:

├─ flask-app
│ ├─ app
│ │ ├─ init.py
│ │ ├─ app.py
│ │ ├─ instance
│ │ │ └─ database.db
│ │ ├─ models.py
│ │ ├─ routes
│ │ │ ├─ init.py
│ │ │ ├─ admin.py
│ │ │ ├─ customer.py
│ │ │ └─ owner.py
│ │ ├─ static
│ │ │ ├─ home.css
│ │ └─ templates
│ │ ├─ admin
│ │ │ ├─ dashboard.html
│ │ │ ├─ signin.html
│ │ │ └─ signup.html
│ │ ├─ customer
│ │ │ ├─ dashboard.html
│ │ │ ├─ signin.html
│ │ │ ├─ signup.html
│ │ │ └─ verify_otp.html
│ │ └─ owner
│ │ ├─ dashboard.html
│ │ ├─ signin.html
│ │ └─ verify_otp.html

Any help would be appreciated!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.