def login(): signUpForm = SignUpForm if request.method == 'POST' and signUpForm.validate_on_submit(): user = db.session.query(User).filter_by( username=signUpForm.username).first() if bcrypt.check_password_hash(user.password, signUpForm.password): login_user(user) return redirect('main.homepage') return render_template('login.html', signUpForm=signUpForm)
def login(): form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(username=form.username.data).first() if user and bcrypt.check_password_hash(user.password, form.password.data): login_user(user, remember=True) next_page = request.args.get('next') return redirect(next_page if next_page else url_for('main.homepage')) return render_template('login.html', form=form)
def login(): form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(username=form.username.data).first() if user and bcrypt.check_password_hash(user.password, form.password.data): login_user(user, remember=True) # The next_page variable determines where to send the user to # If the user tries to visit a page while logged out, they will be redirected to the login page. next_page = request.args.get('next') return redirect(next_page if next_page else url_for('main.homepage')) return render_template('login.html', form=form)
def test_signup(self): # - Make a POST request to /signup, sending a username & password post_data = { 'username': '******', 'password': '******', } response = self.app.post('/signup', data=post_data) self.assertEqual(response.status_code, 302) self.assertIn('/login', response.location) # - Check that the user now exists in the database created_user = User.query.filter_by(username='******').one() self.assertIsNotNone(created_user) self.assertEqual(created_user.username, 'newuser') self.assertTrue( bcrypt.check_password_hash(created_user.password, 'newpass'))
def validate_password(self, password): user = User.query.filter_by(username=self.username.data).first() if user and not bcrypt.check_password_hash(user.password, password.data): raise ValidationError('Password doesn\'t match. Please try again.')