Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Flask-login doesn't actually have a user backend, it just handles the session machinery to help you login and logout users. You have to tell it (by decorating methods), what represents a user and it is also up to you to figure out how to know if a user is "active" or not (since being "active" can mean different things in different applications).</p> <p>You should read the <a href="https://flask-login.readthedocs.org/en/latest/">documentation</a> and be sure what it does <em>and does not do</em>. Here I am only going to concentrate on wiring it up with the db backend.</p> <p>To start off with, define a user object; which represents properties for your users. This object can then query databases, or LDAP, or whatever and it is the hook that connects the login mechanism with your database backend.</p> <p>I will be using the <a href="https://gist.github.com/bkdinoop/6698956">login example</a> script for this purpose.</p> <pre class="lang-py prettyprint-override"><code>class User(UserMixin): def __init__(self, name, id, active=True): self.name = name self.id = id self.active = active def is_active(self): # Here you should write whatever the code is # that checks the database if your user is active return self.active def is_anonymous(self): return False def is_authenticated(self): return True </code></pre> <p>Once you have the user object created, you need to write a method that loads the user (basically, creates an instance of the <code>User</code> class from above). This method is called with the user id.</p> <pre class="lang-py prettyprint-override"><code>@login_manager.user_loader def load_user(id): # 1. Fetch against the database a user by `id` # 2. Create a new object of `User` class and return it. u = DBUsers.query.get(id) return User(u.name,u.id,u.active) </code></pre> <p>Once you have these steps, your login method does this:</p> <ol> <li><p>Checks to see if the username and password match (against your database) - you need to write this code yourself.</p></li> <li><p>If authentication was successful, get the id of the user and pass it to <code>login_user()</code></p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload