def login(): form = LoginForm(request.form) if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user and bcrypt.check_password_hash(user.password, request.form['password']): login_user(user) flash('You are logged in. Welcome!', 'success') return redirect(url_for('user.members')) else: flash('Invalid email and/or password.', 'danger') return render_template('user/login.html', form=form) return render_template('user/login.html', title='Please Login', form=form)
def login(): form = LoginForm(request.form) if form.validate_on_submit(): try: user = models.User.objects.get(username=form.username.data) except: user = None if user and bcrypt.check_password_hash(user.password, request.form['password']): login_user(user) flash('You are logged in. Welcome!', 'success') return redirect(url_for('user.main')) else: flash('Invalid email and/or password.', 'danger') return render_template('user/login.html', form=form) return render_template('user/login.html', form=form)
def test_validate_invalid_email_format(self): # Ensure invalid email format throws error. form = LoginForm(email='unknown', password='******') self.assertFalse(form.validate())
def test_validate_success_login_form(self): # Ensure correct data validates. form = LoginForm(email='*****@*****.**', password='******') self.assertTrue(form.validate())