Created
October 23, 2012 13:07
-
-
Save taylorSando/3938642 to your computer and use it in GitHub Desktop.
Altering backbone to go to /login and /logout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sync: function(method, model, options) { | |
if (method == "update" || method == "create") { | |
var params = {type: "POST", dataType: "json", "url": ___ROOT_URL___ + "/login", | |
contentType: 'application/json', data: JSON.stringify(model.toJSON()), | |
processData: false | |
}; | |
return $.ajax(_.extend(params, options)); | |
} else { | |
Backbone.sync(method, model, options); | |
} | |
}, | |
login: function(creds) { | |
// this.save(creds); | |
var model = this; | |
$.ajax({type: 'POST', | |
url: ___ROOT_URL___ + "/login", | |
data: {email: "[email protected]", password: "password"}, | |
success: function(data) { model.set("userID", data); }, | |
error: function(data) { console.log(data);} | |
}); | |
}, | |
logout: function() { | |
// Do a DELETE to /session and clear the clientside data | |
var that = this; | |
this.destroy({ | |
success: function (model, resp) { | |
model.clear() | |
// Set auth to false to trigger a change:auth event | |
// The server also returns a new csrf token so that | |
// the user can relogin without refreshing the page | |
that.set({auth: false, _csrf: resp._csrf}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment