def reset_token(token):
    # Make sure user is logged out to view this
    # Check if user is already logged in with current_user variable from flask login module downloaded
    if current_user.is_authenticated:
        return redirect('home')

    # Use the method from the User class created in models
    # Method checks if the toekn is valid and returns user_id from db if valid
    user = User.verify_reset_token(token)

    # Invalid token message
    if not user:
        flash('That is an invalid or expired token', 'warning')
        return redirect(url_for('users.reset_request'))

    form = ResetPasswordForm()

    # WTForms checks if request is POST
    if form.validate_on_submit():
        # Hash users password from form data
        hashed_pw = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')

        # Update to new password to the database using SQLAlchemy
        user.password = hashed_pw
        db.session.commit()

        # Show user successful password change message
        flash(f'Your account has been updated! You are now able to log in.',
              'success')
        return redirect(url_for('users.login'))

    return render_template('users/reset_token.html',
                           title='Reset Password',
                           form=form)
Example #2
0
def register():
    # if user is already loged in and authenticated they will redirect to home page
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))

    form = RegistrationForm()
    if form.validate_on_submit():

        # hashing the password that user enterd
        hashed_pw = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')

        # creting the user with given details and hashed password
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_pw)

        # adding user to db and commiting changes
        db.session.add(user)
        db.session.commit()

        flash(f"Account has been created! you are now able to log in",
              "success")
        return redirect(url_for('users.login'))

    return render_template('register.html', title='Register Now', form=form)
def register():
    form = RegistrationForm()
    # Check if user is already logged in with current_user variable from flask login module downloaded
    if current_user.is_authenticated:
        return redirect('home')

    # WTForms checks if request is POST
    if form.validate_on_submit():
        # Hash users password
        hashed_pw = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')

        # Create new instance of user with User class declared in models
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_pw)

        # Add user to the database using SQLAlchemy
        db.session.add(user)
        db.session.commit()

        # Show user successful registration message
        flash(f'Your account has been created! You are now able to log in.',
              'success')
        return redirect(url_for('users.login'))

    # If method is GET return register form template
    return render_template('users/register.html', title='Register', form=form)
Example #4
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))

    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_pw = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(username=form.username.data,
                    password=hashed_pw,
                    email=form.email.data)
        db.session.add(user)
        db.session.commit()
        flash('Your account has been created, you are now able to log in!',
              'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Example #5
0
def reset_token(token):
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    user = User.verify_reset_token(token)
    if user is None:
        flash("Invalid or expired token", 'warning')
        return redirect(url_for('users.reset_request'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(form.password.data)
        user.password = hashed_password
        db.session.commit()
        flash("Your password has been updated!", 'success')
        return redirect(url_for('users.login'))
    return render_template('reset_token.html',
                           form=form,
                           title='Reset Password')
Example #6
0
def reset_token(token):
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    user = User.verify_auth_token(token)
    if user is None:
        flash('Invalid or Expired token', 'danger')
        return redirect(url_for('users.reset_request'))

    form = PasswordResetForm()
    if form.validate_on_submit():
        hashed_pw = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user.password = hashed_pw
        db.session.commit()
        flash(f'Your Password has been updated. Now you can login', 'success')
        return redirect(url_for('users.login'))
    return render_template('reset_password.html',
                           title='Update Password',
                           form=form)
Example #7
0
def registration():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))

    reg_form = RegistrationForm()

    if reg_form.validate_on_submit():
        password_hash = generate_password_hash(reg_form.password.data)
        user = User(username=reg_form.username.data,
                    email=reg_form.email.data,
                    password=password_hash)
        db.session.add(user)
        db.session.commit()
        flash(f'Account created for { reg_form.username.data } !', 'success')
        return redirect(url_for('users.login'))
    else:
        return render_template('users/registration.html',
                               title='Registration',
                               form=reg_form)