Beispiel #1
0
def register():
    """
    verify user's phone number and
    authenticate with a new token
    :return:
    """

    json = request.json
    schema = Schema(
        {
            "first_name": str,
            "username": str,
            "token": str,
            "phone": str,
            Optional('last_name'): str
        },
        ignore_extra_keys=True)
    try:
        schema.validate(request.json)
    except SchemaError as err:
        return response(err.code)

    last_name = None
    if 'last_name' in json:
        last_name = json['last_name']

    first_name = json['first_name']
    username = json['username']
    token = json['token']
    phone = json['phone']

    if User.find(username=username):
        return error_response("username already exists")

    if not TextAPI.verify_auth(phone, token):
        return error_response('invalid phone authentication')

    user = User.new(first_name, last_name, username, phone)
    session = Session.new(user.id)
    return response({'user': user.make_json(), 'session': session.make_json()})
Beispiel #2
0
def login():
    """
    verify user's phone number and login
    the user
    """
    json = request.json
    schema = Schema({"token": str, "phone": str}, ignore_extra_keys=True)
    try:
        schema.validate(request.json)
    except SchemaError as err:
        return response(err.code)

    token = json['token']
    phone = json['phone']

    if not TextAPI.verify_auth(phone, token):
        return error_response('invalid phone authentication')

    user = User.find(phone_num=phone)
    if not user:
        return error_response('user is not registered')

    session = Session.new(user.id)
    return response({'user': user.make_json(), 'session': session.make_json()})