def login_user(): form = LoginForm() if form.validate_on_submit() and request.method == 'POST': username = form.username.data password = form.password.data # Attempt to get user record u = User.query.filter(User.username == username).one_or_none() # Check if user exists if u is not None: # Check if password is correct if bcrypt.check_password_hash(u.pw_hash, password): # Log user in user = flask_login.UserMixin() user.id = u.id flask_login.login_user(user) flash('Successfully logged in {}'.format(username), category='success') return redirect(url_for('front_page'), 303) # Password is not correct, flash message else: flash('Password incorrect') # User does not exist, flash message else: flash('User does not exist') return render_template('login_user.html', form=form)
def is_password_correct(self, plaintext): """Check if user's password is correct.""" return bcrypt.check_password_hash(self._password, plaintext)
def test_password_gets_hashed_when_being_set(): """Testing the library method.""" user = User(username='******', password='******') assert bcrypt.check_password_hash(user.password, '123')