Example #1
0
def register_user():
    post_data = request.get_json()
    response_object = {'status': 'fail', 'message': 'Invalid payload'}
    if not post_data:
        return jsonify(response_object), 400

    username = post_data.get('username')
    email = post_data.get('email')
    password = post_data.get('password')

    try:
        user = Users.query.filter(
            or_(Users.username == username, Users.email == email)).first()
        if not user:
            # add new user to db
            new_user = Users(username=username, email=email, password=password)
            db.session.add(new_user)
            db.session.commit()

            # generate auth token
            auth_token = new_user.encode_auth_token(new_user.id)
            response_object['status'] = 'success'
            response_object['message'] = 'Successfully registered.'
            response_object['auth_token'] = auth_token.decode()
            return jsonify(response_object), 200
        else:
            response_object['message'] = 'Sorry. That user already exists.'
            return jsonify(response_object), 400
    except (exc.IntegrityError, ValueError):
        db.session.rollback()
        return jsonify(response_object), 400
def seed_db():
    """Seeds the database."""
    db.session.add(
        Users(username='******',
              email='*****@*****.**',
              password='******'))
    db.session.commit()
Example #3
0
    def post(self):
        post_data = api.payload
        response_object = {'status': 'fail', 'message': 'Invalid payload'}
        if not post_data:
            return response_object, HTTPStatus.BAD_REQUEST

        first_name = post_data.get('firstName')
        last_name = post_data.get('lastName')
        email = post_data.get('email')
        password = post_data.get('password')

        try:
            user = Users.query.filter_by(email=email).first()
            if not user:
                # add new user to db
                new_user = Users(first_name=first_name,
                                 last_name=last_name,
                                 email=email,
                                 password=password)
                db.session.add(new_user)
                db.session.commit()

                confirmation_token = self.generate_activation_token(
                    new_user.email)
                return response_object, HTTPStatus.CREATED
            else:
                response_object['message'] = 'Sorry. That user already exists.'
                return response_object, HTTPStatus.BAD_REQUEST
        except Exception as e:
            db.session.rollback()
            api.logger.error(e)
            response_object = {"message": "Successfully"}
            return response_object, HTTPStatus.CREATED
Example #4
0
def seed_db():
    """Seeds the database."""
    db.session.add(
        Users(username='******',
              password='******',
              email='*****@*****.**',
              admin=True))
    db.session.commit()
    print('Admin user added.')
Example #5
0
 def decorated_function(*args, **kwargs):
     response_object = {
         'status': 'fail',
         'message': 'Provide a valid auth token.'
     }
     auth_header = request.headers.get('Authorization')
     if not auth_header:
         return response_object, HTTPStatus.FORBIDDEN
     auth_token = auth_header.split(" ")[1]
     resp = Users.decode_auth_token(auth_token)
     if isinstance(resp, str):
         response_object['message'] = resp
         return response_object, HTTPStatus.UNAUTHORIZED
     user = Users.query.filter_by(id=resp).first()
     if not user or not user.active:
         return response_object, HTTPStatus.UNAUTHORIZED
     return f(user, *args, **kwargs)