Skip to content

Instantly share code, notes, and snippets.

@ericbaranowski
Created September 3, 2018 17:50
Show Gist options
  • Select an option

  • Save ericbaranowski/ecae8d025f97a8603104c71d8bbd36be to your computer and use it in GitHub Desktop.

Select an option

Save ericbaranowski/ecae8d025f97a8603104c71d8bbd36be to your computer and use it in GitHub Desktop.
# form is submitted via a POST request to the /register route
@app.route('/register', methods=['GET', 'POST'])
def register():
# this is using a class created using WTForms
form = RegForm()
if request.method == 'POST':
# form is validated via WTForms .validate() method
if form.validate():
# checks if submitted email has already been registered
existing_user = User.objects(email=form.email.data).first()
if existing_user is None:
# the password is hashed using generate_password_hash function from werkzeug.security.
hashpass = generate_password_hash(form.password.data, method='sha256')
# users credentials are saved to database
hey = User(form.email.data,hashpass).save()
# login_user function from flask_login logs the user in
login_user(hey)
# user is then redirected to the dashboard route
return redirect(url_for('dashboard'))
return render_template('register.html', form=form)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment