Esempio n. 1
0
def login():
    """Login a user."""
    form = LoginForm()
    if form.validate_on_submit():
        user = db_session.query(User).filter_by(
            username=form.username.data).first()

        if user is None or user.password != form.password.data:
            user = db_session.query(User).filter_by(
                email=form.username.data).first()

            if user is None or user.password != form.password.data:
                return jsonify({"non_field_errors": bad_credentials}), 401

        login_user(user)

        return jsonify({})

    return jsonify(form.errors), 400
Esempio n. 2
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for("arcticsun_index", device_type="arctic"))

    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(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("arcticsun_index", device_type="arctic")))

        else:
            flash("Login Unsuccessful. Please check email and password.",
                  "danger")

    return render_template("authentication/login.html", form=form)