Exemplo n.º 1
0
def login():
    form = LoginForm(request.values)

    if request.method == 'POST':
        if form.validate():
            user = User(**form.data)
            if user.pre_login():
                the_user = User.query.filter_by(email=user.email).first()
                login_user(the_user)
                identity_changed.send(current_app._get_current_object(),
                                  identity=Identity(the_user.id))
                flash("Logged in successfully.")
                return redirect(request.args.get("next") or\
                                url_for("landing.index"))

    return render_template("user/login.html", form=form)
Exemplo n.º 2
0
def api_login():
    data = request.json
    user = User(**data)

    if user.pre_login():
        the_user = User.query.filter_by(email=user.email).first()
        login_user(the_user)
        #Tell Flask-Principal the identity changed
        identity_changed.send(current_app._get_current_object(),
                                  identity=Identity(the_user.id))
        resp = Response(json.dumps({'id': the_user.id}), status=200,
            content_type="application/json")
    else:
        resp = Response(json.dumps({'msg': 'GTFO'}), status=400,
            content_type=JSON_HEADER)

    return resp