Flask in MONGODB implementation flask_login remain logged in

Source: Internet
Author: User
Tags mongoclient

Recently in learning flask, using flask-login, has been unable to complete the status of login, the online example is the use of sqlalchemy, but I use MongoDB.

The online example uses SQLAlchemy when defining the user class when it is written like this:

classUser (usermixin,db. Model):__tablename__='Users'ID= db. Column (db. Integer, primary_key=True) Username= db. Column (db. String (+), Unique=true, index=True) role_id= db. Column (db. Integer, Db. ForeignKey ('roles.id'))    def __repr__(self):return '<user%r>'% Self.username

Then use the following statement to match the query when logging in:

       user = User.query.filter_by (username=form.name.data). First ()

I set the next breakpoint and found this statement to return the following:

As you can see from the diagram, this statement returns the entire user class

And my definition is this:

ClassUser ():Def__init__(self, username, email, password): Self.username =Username Self.email =Email Self.password_hash =Self.set_password (password) self.db =Mongoclient (). Blog. UserDefNew_user (self): Collection ={‘Name‘  ' : Self.email, password ' : Self.password_hash} self.db.insert ( Collection) @property def password (self, password): Span style= "color: #0000ff;" >raise attributeerror ( "password is not a readable Attribute ' ) def Set_password (self, password): return generate_password_hash (password)   

To find a matching statement:

        user = Mongoclient (). Blog. User.find_one ({'email': Form.email.data})

A JSON-formatted document is returned:

So how is flask-login to keep the login on different pages?

hello,{% if current_user.is_authenticated%} {{Current_user.name}}

Flask-login is an account that is imported through load_user and then verified by Is_active and is_anonymous.

We must add these bool values to the information returned by MongoDB in order to complete the validation operation.

My solution is to build a new class, find and export information from MongoDB, recreate a new class, and then pass the newly created class to Load_user.

classTemp (usermixin): Is_active=True is_anonymous=False is_authenticated=Truedef __init__(self, id, username, email, password): Self.id=str (ID) self.name=username Self.email=Email Self.password_hash=Passworddefget_id (self):returnself.iddef __repr__(self):returnSelf.username

@login_manager. User_loaderdefLoad_user (user_id): User= Mongoclient (). Blog. User.find_one ({'_id': ObjectId (user_id)}) returnTemp (Id=user.get ('_id'), Username=user.get ('name'), Email=user.get ('Email'), Password=user.get ('Password'))

Flask in MONGODB implementation flask_login remain logged in

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.