Using login checkbox with postgresql

I am working on a flash application that requires authentication. I connected the login checkbox, but it does not look very elegant.

To enter the flash drive, you need to make sure that the user exists:

@login_manager.user_loader def load_user(id): return User.query.get(id) 

But you also need to use "login_user" to create a custom object

 # Some code above user = User.query.filter_by(email = form.email.data, password = form.password.data).first() user.login_status = 1 db.session.commit() login_user(objects.SignedInUser(user.id, user.email, user.login_status == LoginStatus.Active)) # Some code below 

In the above code, β€œUser” is the model for postgres, and SignedInUser is just the object that will be used to enter the flags.

Does anyone have an example of flash input used with postgres?

+8
flask postgresql
source share
1 answer

It seems that you are mistaken, understand what the checkbox does. It should track everything about the user's session after you report that the authentication was successful (by calling login_user .) The user_loader tells only how to reload the object for a user who has already authenticated, for example, when someone reconnects to Remember Me session. The documents are not particularly clear.

No need to store a flag in the database for user login status. In addition, the code you included will raise an AttributeError if the credentials are incorrect (user = None).

Here is an example from the Flask-SQLAlchemy application. It uses an external authentication source and shell for the UserAllchemy User object, but the process is basically the same.

user_loader callback:

 @login_manager.user_loader def load_user(user_id): user = User.query.get(user_id) if user: return DbUser(user) else: return None 

Custom class (wrapper for SQLAlchemy object):

 # User class class DbUser(object): """Wraps User object for Flask-Login""" def __init__(self, user): self._user = user def get_id(self): return unicode(self._user.id) def is_active(self): return self._user.enabled def is_anonymous(self): return False def is_authenticated(self): return True 

Login Handler:

 @app.route('/login', methods=['GET', 'POST']) def login(): error = None next = request.args.get('next') if request.method == 'POST': username = request.form['username'] password = request.form['password'] if authenticate(app.config['AUTH_SERVER'], username, password): user = User.query.filter_by(username=username).first() if user: if login_user(DbUser(user)): # do stuff flash("You have logged in") return redirect(next or url_for('index', error=error)) error = "Login failed" return render_template('login.html', login=True, next=next, error=error) 

Please note that the login fails if:

  • external authentication failure
  • user request returns None (user does not exist)
  • login_user returns False ( user.is_active() == False )

Exit

 @app.route('/logout') @login_required def logout(): logout_user() flash('You have logged out') return(redirect(url_for('login'))) 
+35
source share

All Articles