Ejemplo n.º 1
0
def login():
    """User login function"""
    data = dict()
    if request.is_json:
        data = request.get_json()
    else:
        return jsonify({
            "status": 0,
            "message": "invalid format json type"
        }), 400

    if not data or not data.get('password') or not data.get('email'):
        return jsonify({
            "status": 0,
            "message": "email and password is required!"
        }), 401

    user = User.get_user_by_id(data['email'])
    print("user role: ", user.role)

    if user and sha256_crypt.verify(data['password'], user.password):
        return jsonify({
            "jwt": encode({
                "id": user.id,
                'role': user.role
            }),
            "status": 1
        }), 200
    else:
        return jsonify({
            "status": 0,
            "message": "email or password is incorrect!"
        }), 400