def register(): data = request.get_json(force=True) if data: if is_valid(data['first_name']) or \ is_valid(data['last_name']): return jsonify({'message': 'Name contains special characters'}), 200 if data['email'] == "" or data['password'] == "" or \ data['first_name'] == "" or data['last_name'] == "": return jsonify({'message': 'All fields must be filled'}), 200 if not validate_email(data['email']): return jsonify({'Error': 'Invalid Email'}), 200 if len(data['password']) < 6: return jsonify({'Error': 'Password is too short'}), 200 hashed_password = generate_password_hash(data['password'], method='sha256') if User.query.filter_by(email=data['email']).first(): return jsonify({'message': 'User already exists'}), 202 new_user = User(public_id=str(uuid.uuid4()), email=data['email'], password=hashed_password, first_name=data['first_name'], last_name=data['last_name']) new_user.save() response = jsonify('message', 'New user created!'), 201 else: response = jsonify('message', 'New user not created!') return response
def create_user(): """Creates a test user""" password = bcrypt.generate_password_hash("password").decode("utf-8") user = User(username="******", password=password) db.session.add(user) db.session.commit()
def register(): if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm(request.form) if request.method == "POST" and form.validate(): check_username = User.query.filter_by( username=form.username.data).first() check_email = User.query.filter_by(username=form.email.data).first() print(check_email) if check_username: flash(f'That username is taken. Please choose a different one.', 'danger') return render_template('register.html', form=form) elif check_email: flash(f'That email is taken. Please choose a different one.', 'danger') return render_template('register.html', form=form) else: hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(user) db.session.commit() flash(f'Account created for {form.username.data}!', 'success') return redirect(url_for('login')) return render_template('register.html', form=form)
def reset_password(token): if current_user.is_authenticated: return redirect(url_for('core.index')) user = User.verify_reset_password_token(token) if not user: return redirect(url_for('core.index')) form = ResetPasswordForm() if form.validate_on_submit(): user.set_password(form.password.data) db.session.commit() flash('Your password has been reset.') return redirect(url_for('users.login')) return render_template('reset_password.html', form=form)
def get_users(): users = User.get_all() user_list = [] for user in users: user_data = {} user_data['id'] = user.id user_data['public_id'] = user.public_id user_data['email'] = user.email user_data['first_name'] = user.first_name user_data['last_name'] = user.last_name user_list.append(user_data) response = jsonify({'users': user_list}), 200 return response
def signup(): """Creates a new User Model provided informaiton from the signup form""" form = SignUpForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User( username=form.username.data, password=hashed_password ) db.session.add(user) db.session.commit() flash('You Created Your Account!') return redirect(url_for('auth.login')) return render_template('signup.html', form=form)
def register(): form = RegistrationForm() if form.validate_on_submit(): try: user = User(email=form.email.data, username=form.username.data, password=form.password.data) db.session.add(user) db.session.commit() flash(f'Account created for {form.username.data}!', 'success') return redirect(url_for('users.login')) except IntegrityError: db.session.rollback() flash( f'either email {form.email.data} or username {form.username.data} already taken : !', 'danger') return render_template('register.html', form=form)
def test_password_hashing(self): u = User(username='******', email="*****@*****.**", password="******") u.set_password('cat') self.assertFalse(u.check_password('dog')) self.assertTrue(u.check_password('cat'))