Exemplo n.º 1
0
def api_register():
    data = request.get_json()
    try:
        username = data['username'].strip()
        email = data['email'].strip()
        password = data['password']
        errors = dict()
        input_validator.validUsername(username, errors)
        input_validator.validEmail(email, errors)
        input_validator.validPassword(password, errors)
        if len(errors) > 0:
            return jsonify(errors)
        user = db.session.query(User).filter(
            db.func.lower(User.username) == db.func.lower(username)).first()
        if not user:
            hash_password = bcrypt.generate_password_hash(password).decode(
                'utf-8')
            user = User(username=username, email=email, password=hash_password)
            db.session.add(user)
            db.session.commit()
            result = user_schema.dump(user)
            return jsonify({'user': result})
        else:
            return jsonify({'error': {'username': '******'}}), 401
    except:
        return jsonify({'message': 'Invalid request'}), 400
    return jsonify({'message': 'Invalid request'}), 400
Exemplo n.º 2
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_passwd = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_passwd)
        db.session.add(user)
        db.session.commit()
        flash('Your account is created! Please login.', 'success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)
Exemplo n.º 3
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hash_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hash_password)
        db.session.add(user)
        db.session.commit()
        return redirect(url_for('main.home'))
    searchForm = SearchForm()
    return render_template('register.html',
                           form=form,
                           searchForm=searchForm,
                           title="Register")
Exemplo n.º 4
0
def reset_token(token):
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    user = User.verify_reset_token(token)
    if user is None:
        flash('That is an invalid or expired token', 'warning')
        return redirect(url_for('reset_request'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user.password = hashed_password
        db.session.commit()
        flash('Your password has been updated! You are now able to log in',
              'success')
        return redirect(url_for('login'))
    return render_template('reset_token.html',
                           title='Reset Password',
                           form=form)
Exemplo n.º 5
0
def change_password_form():
    form = ChangePasswordForm()
    image_file = url_for('static',
                         filename='display_pics/' +
                         current_user.display_picture)
    if form.validate_on_submit():
        hash_password = bcrypt.generate_password_hash(
            form.new_password.data).decode('utf-8')
        current_user.password = hash_password
        db.session.commit()
        return redirect(url_for('main.home'))
    follow = User.query.filter_by(id=current_user.id).first().follow.all(
    ) if current_user.is_authenticated else None
    searchForm = SearchForm()
    return render_template('change_password.html',
                           form=form,
                           searchForm=searchForm,
                           follow=follow,
                           image_file=image_file,
                           title="Change Password")
Exemplo n.º 6
0
def api_change_password(c_user):
    data = request.get_json()
    try:
        old_password = data['old_password']
        new_password = data['new_password']
        errors = dict()
        input_validator.validPassword(new_password, errors)
        if len(errors) > 0:
            return jsonify(errors)
        if bcrypt.check_password_hash(c_user.password, old_password):
            hash_new_pass = bcrypt.generate_password_hash(new_password).decode(
                'utf-8')
            c_user.password = hash_new_pass
            db.session.commit()
            return jsonify({'message': 'Account password has been changed'})
        else:
            return jsonify({'error': {
                'password': '******'
            }}), 401
    except:
        return jsonify({'message': 'Invalid request'}), 400
Exemplo n.º 7
0
def register():
    if(current_user.is_authenticated):
        return redirect(url_for('home'))
    form = RegisterForm()
    if(form.validate_on_submit()):
        user = User.query.filter_by(username=form.username.data).first()
        if(user):
            flash('That username is taken. Choose another one.', 'danger')
            return redirect(url_for('register'))
        user2 = User.query.filter_by(email=form.email.data).first()
        if(user2):
            flash('That email is taken. Choose another one.', 'danger')
            return redirect(url_for('register'))

        hashed_pass = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
        user = User(username=form.username.data, email=form.email.data, password=hashed_pass)
        db.session.add(user)
        db.session.commit()
        flash(f'Created account for {form.username.data}. You can now log in.', 'success')
        return redirect(url_for('login'))
    return render_template('register.html', title="Register", form=form)