Example #1
0
def register_user():
    username = request.form.get('username')
    email = request.form.get('email')
    password = request.form.get('password')
    avatar = request.files['avatar']

    hashed_password = bcrypt.hashpw(
        str(password).encode('utf-8'), bcrypt.gensalt())

    user = User(username=username, email=email, password=hashed_password)

    filename = secure_filename(avatar.filename)
    avatar.save(filename)

    s3_client.upload_file(Bucket='jwalls-fun-bucket',
                          Filename=filename,
                          Key=f'profile-pics/{filename}',
                          ExtraArgs={'ACL': 'public-read'})

    os.remove(filename)

    user.avatar_url = f'https://jwalls-fun-bucket.s3.amazonaws.com/profile-pics/{filename}'

    db.session.add(user)
    db.session.commit()

    user = db.session.query(User).filter_by(username=username).first()

    return {'user': user.to_dict()}
Example #2
0
def create_user():
    """
    Create new user.

    Args:
        username (request.json): The username of the new user.
        email (request.json): The email of the new user.
        password (request.json): The password of the new user.

    Returns:
        (dict): {
            'created': (bool) created,
            'user': {
                'id': (int) user id,
                'username': (str) user username,
                'email': (str) user email,
                'authenticated': (bool) user authenticated,
                'last_login': (datetime) user last login,
                'created_on': (datetime) user created on
            }
        }
    """

    # encode the password and hash
    password = str(request.get_json()['password']).encode('utf-8')
    hashed_password = bcrypt.hashpw(password, bcrypt.gensalt())

    user = User(
        username=request.get_json()['username'],
        password=hashed_password,
        email=request.get_json()['email']
    )

    db.session.add(user)
    db.session.commit()

    user = db.session.query(User).get(user.id)

    return {'user': user.to_dict()}