wathmal
wathmal

Reputation: 371

Destroy a "User" from parse sdk with javascript

i need to delete a user form parse sdk with javascript, i tried loading the user query and then calling the destroy() but it gives me:

[HTTP/1.1 400 Bad Request]

my code is here

var query = new Parse.Query("User");
query.equalTo("email", '[email protected]');
query.find().then(function(results) {
console.log(results[0]);
results[0].destroy();
});

this won't destroy the user. can anybody help?

Upvotes: 1

Views: 1157

Answers (1)

wathmal
wathmal

Reputation: 371

with the help of Bjorn's answer i figured out a way doing it. i had to use REST api of parse sdk and generate a DELETE request with a proper session key of the user.

var CurrentUser = Parse.User.current();
console.log(CurrentUser);

var sessiontoken;


Parse.User.logIn(CurrentUser.attributes.username, document.getElementById("curpassword").value, {
    success: function (user) {
        user.set("StayLoggedIn", "false");

        console.log(user._sessionToken);
        sessiontoken = user._sessionToken;
        user.save();



        $.ajax({
            url: 'https://api.parse.com/1/users/' + user.id,
            type: 'DELETE',
            headers: {'X-Parse-Application-Id': APP_ID, 'X-Parse-REST-API-Key': REST_KEY, 'X-Parse-Session-Token': sessiontoken},
            success: function (result) {
                // Do something with the result
                alert("you have successfully deleted your account.");
                Parse.User.logOut();
                window.location.href = "index.html";
            }
        });
        //  location.reload();
    },
    error: function (user, error) {
        //alert(error);
        alert("incorrect username or password");
    }
});

Upvotes: 1

Related Questions