def login():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.objects(email = form.email.data).first()
        if user and bcrypt.check_password_hash(user.password, form.password.data):
            login_user(user, remember = form.remember.data)
            next_page = request.args.get('next')
            return redirect(next_page) if next_page else redirect(url_for('home'))
        else:
            flash('Login Unsuccessful', 'danger')
    return render_template('login.html', title="Login", form=form)
 def validate_email(self, email):
     user = User.objects(email=email.data).first()
     if user:
         raise ValidationError('Email already used. Please use another one')
 def validate_username(self, username):
     user = User.objects(username=username.data).first()
     if user:
         raise ValidationError(
             'Username already taken. Please take another one')