Beispiel #1
0
def register():
    """Register a new user.

    Validates that the username is not already taken. Hashes the
    password for security.
    """
    if request.method == "POST":
        db = get_db()
        error = None

        json = request.get_json()
        username = json['username']
        password = json['password']

        if not username:
            error = "Username is required."
        elif not password:
            error = "Password is required."
        elif get_user_by_username(db, username) is not None:
            error = "User {0} is already registered.".format(username)

        if error is None:
            # the name is available, store it in the database and go to
            # the login page
            create_user(db, username, password)
            return redirect(url_for("auth.login"))

        flash(error)

    # return render_template("auth/register.html")
    return jsonify(username=g.user.username, password=g.user.password)
Beispiel #2
0
def register():
    """Register a new user.

    Validates that the username is not already taken. Hashes the
    password for security.
    """

    if request.method == "POST":
        db = get_db()
        error = None

        json = request.get_json()
        username = json['username']
        password = json['password']

        if not username:
            error = "Username is required."
        elif not password:
            error = "Password is required."
        elif get_user_by_username(db, username) is not None:
            error = "User {0} is already registered.".format(username)

        if not error:
            create_user(db, username, password)
            return Response("200 user was created", status=200)

        return Response("400 %s" % error, status=400)

    return Response("405 need POST method", status=405)
Beispiel #3
0
def register():
    """Register a new user.

    Validates that the username is not already taken. Hashes the
    password for security.
    """
    db = get_db()
    error = None

    data = request.get_json()
    username = data.get('username', '')
    password = data.get('password', '')

    if not username:
        error = "Username is required."
    elif not password:
        error = "Password is required."
    elif get_user_by_username(db, username) is not None:
        error = "User {0} is already registered.".format(username)

    if error:
        error = json.dumps({"error": error})
        return Response(error, status=400, mimetype="application/json")

    create_user(db, username, password)
    user = get_user_by_username(db, username)
    data = {"user_id": user["id"], "username": user["username"]}
    data = json.dumps(data)
    return Response(data, status=200, mimetype="application/json")
Beispiel #4
0
def register():
    """Register a new user.
    Validates that the username is not already taken. Hashes the
    password for security.
    """
    db = get_db()
    error = None

    json_data = request.get_json()
    username = json_data['username']
    password = json_data['password']

    if not username:
        error = "Username is required."
    elif not password:
        error = "Password is required."
    elif get_user_by_username(db, username) is not None:
        error = "User {0} is already registered.".format(username)

    if error is None:
        create_user(db, username, password)
        user = get_user_by_username(db, username)
        register = {"user_id": user['id'], "username": user['username']}
        data = json.dumps(register)
        return Response(data, status=200)

    error = json.dumps({'error': error})
    return Response(error, status=400)
def register():
    """Register a new user"""
    json = request.get_json()

    if json.get('username') and json.get('password'):
        username, password = json['username'], json['password']

        if get_user_by_username(get_db(), username) is not None:
            abort(409, description=f"User {username} is already registered.")

        create_user(get_db(), username, password)
        return Response("Success: user was registered", 200)

    abort(400, description="Error: Username and Password is required.")
Beispiel #6
0
def new_user():

    db = get_db()

    username = request.json.get('username')
    password = request.json.get('password')

    if username is None or password is None:
        abort(400)

    if get_user_by_username(db, username) is not None:
        abort(400, 'User with the same username already exists')

    create_user(db, username, password)
    user = get_user_by_username(db, username)

    return jsonify({'username': user['username']}), 201