Пример #1
0
def create():
    content = request.json

    username = content['username']
    user_exists = UsersModel.find_user_by_username(username)
    if user_exists:
        return jsonify(success=False, msg='Username already exists'), 409

    email = content['email']
    email_exists = UsersModel.find_user_by_email(email)
    if email_exists:
        return jsonify(success=False, msg='Email already used'), 409

    password = content['password']
    display_name = content['displayName']

    new_user = UsersModel(username,
                          hashlib.sha256(password.encode("utf-8")).hexdigest(),
                          email)
    new_user.save_to_db()

    new_location = LocationsModel(new_user, 0.0, 0.0)
    new_location.save_to_db()

    premade = '[{"key":"InitialGreeting","title":"Greetings","text":"Hi, this is ' + display_name + '"}]'
    new_pass = PassesModel(new_user, display_name, premade)
    new_pass.save_to_db()

    return jsonify(success=True), 200
Пример #2
0
def signin():
    content = request.json
    username = content['username']
    password = content['password']

    user_exists = UsersModel.find_user_by_username(username)
    if user_exists and user_exists.password == hashlib.sha256(
            password.encode("utf-8")).hexdigest():
        access_token = create_access_token(identity=user_exists.id)
        display_name = PassesModel.get_display_name_by_user_id(user_exists.id)
        entries = PassesModel.get_string_pass_by_user_id(user_exists.id)
        favorites = user_exists.favorites
        return jsonify(success=True,
                       token=access_token,
                       displayName=display_name,
                       entries=entries,
                       favorites=favorites), 200
    else:
        return jsonify(success=False, msg='Invalid credentials'), 401