def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        # db.create_all()
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        #CREATING USER

        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash(f'Your account has been created, you can now login!', 'success')
        return redirect(url_for('users.signin'))
    return render_template('register.html', title='Register', form=form)
Ejemplo n.º 2
0
def register():

    # If user is already logged in, return to home page
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))

    # Set form variable to registration form
    form = RegistrationForm()

    # Only run following code if form response passes checks (in users/forms.py)
    if form.validate_on_submit():

        # Remove old user info if exists (and unvalidated)
        olduser = User.query.filter(
            User.username.ilike(
                f'%{form.username.data}%')).first()  # check if username exists
        if olduser and olduser.confirm_account is False:  # remove username
            db.session.delete(olduser)  # delete row entry
            db.session.commit()  # save changes
        oldemail = User.query.filter(User.email.ilike(
            f'%{form.email.data}%')).first()  # check for email in db
        if oldemail and oldemail.confirm_account is False:  # remove email
            db.session.delete(oldemail)  # delete row entry
            db.session.commit()  # save changes

        # Add user to db
        hashed_pw = bcrypt.generate_password_hash(form.password.data).decode(
            'utf-8')  # encrypt password
        newuser = User(username=form.username.data,
                       email=form.email.data,
                       password=hashed_pw,
                       date_register=datetime.now())  # noqa
        db.session.add(newuser)  # add row entry
        db.session.commit()  # save changes
        sendemail_auth(newuser)  # send authentication email

        # Inform user that email authentication is required
        flash(f'Email verification request sent to {form.email.data}!',
              'success')
        return redirect(url_for('users.login'))

    return render_template('register.html', title='Register',
                           form=form)  # key variables for .html