Beispiel #1
0
def register():
    if request.method == 'POST':
        name = request.form["name"]
        username = request.form["username"]
        password = request.form["password"]
        email = request.form["email"]
        phone = request.form["phone"]
        user = User(name, username, generate_password_hash(password, 'sha1'),
                    email, phone)
        user.save_to_mongo()
        return render_template("register.html",
                               registered="{} has been created".format(name))
    return render_template("register.html")
Beispiel #2
0
def delete_user(id):
    user = User.find_one_class_id(id)
    if user:
        user.delete()
        return jsonify(
            {"message": "{} has been deleted".format(user.username)})
    else:
        return jsonify({"message": "User not found"})
Beispiel #3
0
def update_user(id):
    r = request.json
    user = User.find_one_class_id(id)
    if user:
        user.update_one(r)
        return jsonify(
            {"message": "{} has been updated".format(user.username)})
    else:
        return jsonify({"message": "User not found"})
Beispiel #4
0
def login():
    r = request.json
    user = User.find_one_username(r['username'])
    user['exp'] = datetime.datetime.utcnow() + datetime.timedelta(minutes=50)
    if check_password_hash(user['password'], r['password']):
        token = jwt.encode(
            user,
            app.config['SECRET_KEY'],
        )
        return jsonify({"token": token.decode('UTF-8')})
    else:
        return jsonify({"message": "login failed"})
Beispiel #5
0
def login():
    # try, except block
    # if user does not exist
    if request.method == 'POST':
        username = request.form["username"]
        password = request.form["password"]
        user = User.find_one_username(username)
        docs = Document.find_by_author(user['_id'])
        user['exp'] = datetime.datetime.utcnow() + datetime.timedelta(
            minutes=50)
        if check_password_hash(user['password'], password):
            token = jwt.encode(user, app.config['SECRET_KEY'])
            session['token'] = token
            return render_template("profile.html",
                                   user=user['username'],
                                   docs=docs)
        else:
            return render_template(
                "login.html",
                no_login="******")

    return render_template("login.html")
Beispiel #6
0
def post_user():
    r = request.json
    user = User(username=r['username'],
                password=generate_password_hash(r['password'], 'sha1'))
    user.save_to_mongo()
    return jsonify({"message": "{} has been created".format(user.username)})
Beispiel #7
0
def get_one_user_by_id(id):
    user = User.find_one_id(id)
    if user:
        return jsonify(user)
    else:
        return jsonify({"message": "User does not exist"})
Beispiel #8
0
def get_users():
    users = User.find()
    if users:
        return jsonify({"users": users})
    else:
        return jsonify({"message": "No users to display"})