Ejemplo n.º 1
0
def create():
    req_data = request.get_json()
    data, error = user_schema.load(req_data)

    if error:
        return custom_response(error, 400)

    user_in_db = User.get_by_email(data.get('email'))
    if user_in_db:
        message = {'error': 'User already exists'}
        return custom_response(message, 400)

    user = User(
        name=data.get('name'),
        email=data.get('email'),
    )

    user.set_password(password=data.get('password'))
    user.save()
    serialized_data = user_schema.dump(user).data
    token = Auth.generate_token(serialized_data.get('uid'))
    return custom_response(
        {
            'jwt_token': token,
            'uid': serialized_data.get('uid'),
        }, 201)
Ejemplo n.º 2
0
def login():
    req_data = request.get_json()

    data, error = user_schema.load(req_data, partial=True)
    if error:
        return custom_response(error, 400)
    if not data.get('email') or not data.get('password'):
        return custom_response(
            {'error': 'you need email and password to sign in'}, 400)
    user = User.get_by_email(data.get('email'))
    if not user:
        return custom_response({'error': 'invalid credentials'}, 400)
    if not user.check_password(data.get('password')):
        return custom_response({'error': 'invalid credentials'}, 400)
    serialized_data = user_schema.dump(user).data
    token = Auth.generate_token(serialized_data.get('uid'))
    return custom_response(
        {
            'jwt_token': token,
            'uid': serialized_data.get('uid'),
        }, 200)