def account():
    form = UpdateAccountForm()
    if form.validate_on_submit():
        print(form.picture.data)
        if form.picture.data:
            picture_file = save_picture(form.picture.data)
            current_user.image_file = picture_file
        current_user.username = form.username.data
        if form.password.data:
            if bcrypt.check_password_hash(current_user.password, form.oldPassword.data):
                pass
            else:
                flash("Your current password is incorrect", category="danger")
                return redirect(url_for('users.account'))
            hashedPassword = bcrypt.generate_password_hash(
                form.password.data).decode('utf-8')
            current_user.password = hashedPassword
        db.session.commit()
        flash("Your account has been updated", category='success')
        return redirect(url_for('users.account'))
    elif request.method == 'GET':
        form.username.data = current_user.username
        form.email.data = current_user.email
    image_file = url_for('static', filename='images/' +
                         current_user.image_file)
    return render_template('profil.html', title="Account", image_file=image_file, form=form)
def login():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password, form.password.data) and user.active == True:
            login_user(user, remember=form.remember.data)
            nextPage = request.args.get('next')
            flash(
                f"Login successful, welcome {user.username}!", category='success')
            return redirect(nextPage) if nextPage else redirect(url_for('main.home'))
        elif user and bcrypt.check_password_hash(user.password, form.password.data) and user.active == False:
            flash(Markup(
                f"Please activate your account, we have sent an activation code to your email,<a href=\"{url_for('users.sent_activate_token', username=user.username)}\" class=\"ms-1 me-1\">click here</a>to send them again"), "danger")
        else:
            flash("Login Unsuccessful, please check your email and password", "danger")
    return render_template('login.html', title='Login', form=form)
Example #3
0
def test_new_user(new_user):
    """
    GIVEN a User model
    WHEN a new User is created
    THEN check the email, hashed_password, and role fields are defined correctly
    """
    assert new_user.email == "*****@*****.**"
    assert new_user.username == "locationtest"
    assert bcrypt.check_password_hash(new_user.password,
                                      "locationtest123") == True
Example #4
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for("main.home"))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user and bcrypt.check_password_hash(user.password, form.password.data):
            login_user(user, remember=form.remember.data)
            next_page = request.args.get('next')
            now_log = datetime.datetime.now()
            send_log(f'{current_user.username}, logged in, {now_log}', 'authentication')
            return redirect(next_page) if next_page else redirect(url_for('main.home'))
        else:
            flash('Login Unsuccessful. Please check email and password', 'danger')
    return render_template('user/login.html', title='Login', form=form)
Example #5
0
def handle_login():
    form = LoginForm()
    identification = form.identification.data
    if not re.match(r"[^@]+@[^@]+\.[^@]+", identification):
        user = User.query.filter_by(username=identification).first()
    else:
        user = User.query.filter_by(email=identification).first()
    if user and bcrypt.check_password_hash(user.password, form.password.data):
        session.clear()
        login_user(user, remember=form.remember.data)
        next_page = request.args.get("next")
        flash("Login successful!", "success")
        return redirect(next_page) if next_page else redirect(
            url_for("main.home"))
    else:
        flash("Login unsuccessful. Please check email and password.", "danger")
        return redirect(url_for("users.login"))
Example #6
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password,
                                               form.password.data):
            login_user(user, remember=form.remember.data)
            nextPage = request.args.get('next')
            flash(f"Login successful, welcome {user.username}!",
                  category='success')
            return redirect(nextPage) if nextPage else redirect(
                url_for('main.home'))
        else:
            flash("Login Unsuccessful, please check your email and password",
                  "danger")
    return render_template('login.html', title='Login', form=form)