def user_login(prop_id=None): # If the user is already authenticated, redirect if current_user.is_authenticated: return redirect(url_for('index')) # Get the login user form form = LoginForm() # Validate the form if submitted via post request if request.method == 'POST': if form.validate_on_submit(): email = form.email.data user = Users.check_user(email) # Complete the login and redirect to correct page login_user(user) if not prop_id: return redirect(url_for('index')) # Should be account page else: return redirect(url_for('prop', prop_id=prop_id)) # Return the failure message if form not validated else: err_msg = 'Invalid Email or Password!' return render_template('user_login.html', prop_id=prop_id, login_user_form=form, err_msg=err_msg) # Render the template return render_template('user_login.html', prop_id=prop_id, login_user_form=form, err_msg=False)
def post(self): login_form = LoginForm(MultiDict(request.get_json())) if login_form.validate_on_submit(): login_user(login_form.user, remember=login_form.remember.data) after_this_request(_commit) return jsonify({'data': user_schema.dump(current_user).data}) # login failed login_error = BadRequest(LOGIN_ERROR_MESSAGE) return Response(json.dumps({"errors": login_form.errors, "_error": LOGIN_ERROR_MESSAGE}), login_error.code, login_error.get_headers())
def login(): """View function for login view""" form = LoginForm(request.form, csrf_enabled=not app.testing) user, msg, confirm_url = None, None, None if request.json: form = LoginForm(MultiDict(request.json), csrf_enabled=not app.testing) if form.validate_on_submit(): try: user = _security.auth_provider.authenticate(form) except ConfirmationError, e: msg = str(e) confirm_url = url_for('send_confirmation', email=e.user.email) except BadCredentialsError, e: msg = str(e) form.password.errors.append(msg)