Skip to content

Instantly share code, notes, and snippets.

@nk9
Last active January 13, 2024 15:17
Show Gist options
  • Save nk9/81a1e3674b9efe6314e07d715d06cd4c to your computer and use it in GitHub Desktop.
Save nk9/81a1e3674b9efe6314e07d715d06cd4c to your computer and use it in GitHub Desktop.
Flask-Security registration with pending role
from flask_security.decorators import anonymous_user_required
from flask_security.utils import encrypt_password
from flask_security.confirmable import send_confirmation_instructions
@bp.route('/register/', methods=['GET', 'POST'])
@anonymous_user_required
def register():
form = ExtendedRegistrationForm(request.form)
if form.validate_on_submit():
form_data = form.to_dict()
form_data['password'] = encrypt_password(form_data['password'])
user = security.datastore.create_user(**form_data)
# Do stuff to register the user
pending_role = security.datastore.find_role('pending')
security.datastore.add_role_to_user(user, pending_role)
security.datastore.commit()
send_confirmation_instructions(user)
flash('Thank you for registering. Please check your inbox for a confirmation email and click the link inside.')
return redirect(url_for('security.login'))
return render_template('register_user.html', title='Register', register_user_form=form)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment