> For the complete documentation index, see [llms.txt](https://eleven-fifty-academy.gitbook.io/python-301-flask/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://eleven-fifty-academy.gitbook.io/python-301-flask/07-uniformed-users.md).

# 07 - Uniformed Users

## Create User

Add the following code to `user_view.py`

```python
from flask import request, json, Response, Blueprint
from ..models.user import UserModel, UserSchema
from ..shared.authentication import Auth  # we haven't built this yet

user_api = Blueprint('users', __name__)
user_schema = UserSchema()

@user_api.route('/', methods=['POST'])
def create():
  req_data = request.get_json()
  data, error = user_schema.load(req_data)

  if error:
    return custom_response(error, 400)

  # check if user already exist in the db
  user_in_db = UserModel.get_user_by_email(data.get('email'))
  if user_in_db:
    message = {'error': 'User already exist, please supply another email address'}
    return custom_response(message, 400)

  user = UserModel(data)
  user.save()

  ser_data = user_schema.dump(user).data

  token = Auth.generate_token(ser_data.get('id'))

  return custom_response({'jwt_token': token}, 201)


def custom_response(res, status_code):
  return Response(
    mimetype="application/json",
    response=json.dumps(res),
    status=status_code
  )
```

Ok, we just added a lot so let's break it down

1. We created a **blueprint** object that defines what this collection of functions will be grouped under
2. when we use the decorator `@user_api.route()` , we are adding a route to the `user_api` blueprint. This is how we will define all of our routes.&#x20;
3. This way of adding routes makes it really easy to add routes to our app as all we need to do is add the blueprints
4. At the beginning of the create function, we load the data from the `request` argument (this is handled on Flask's side, it will be there when we run it) into the `user_schema`. This will automatically, map our attributes to the model and check for errors in the request
5. If there are errors, we will send back a custom response using the `custom_response` function we created, to send back a 400 (bad request) to the client
6. If the request body comes back clean from the schema and the email isn't used yet in our database, we are going to save the user and generate a token for the user
7. This token will be used to log in the user, we will implement this in the future
8. Then, the token will be sent back to client so they have access to it

We still don't have the authentication class yet. We will build this out in the next module
