def user_register(prop_id=None): # If the user is already authenticated, redirect if current_user.is_authenticated: return redirect(url_for('index')) # Get the register user form form = RegisterForm() # Validate the form if submitted view post request if request.method == 'POST': email = form.email.data pswd = form.password.data confirm = form.password_confirm.data if form.validate_on_submit(): # Create user and update password user_datastore.create_user(email=email, password=pswd) db.session.commit() 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)) # Check if the user email exists else: if Users.check_user(email): err_msg = 'An Account for this Email Already Exists!' # If the password doesn't match elif pswd != confirm: err_msg = 'Passwords Do Not Match!' # Catch for any other errors else: err_msg = 'Invalid Email!' # Return the template with the correct error message return render_template('user_register.html', prop_id=prop_id, register_user_form=form, err_msg=err_msg) # Render the template return render_template('user_register.html', prop_id=prop_id, register_user_form=form, err_msg=False)
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)