Skip to content

Instantly share code, notes, and snippets.

@BoLaMN
Created October 1, 2015 13:26
Show Gist options
  • Save BoLaMN/2435f01c1cde467fa4f6 to your computer and use it in GitHub Desktop.
Save BoLaMN/2435f01c1cde467fa4f6 to your computer and use it in GitHub Desktop.
module.factory(
"User",
['LoopBackResource', 'LoopBackAuth', '$injector', function(Resource, LoopBackAuth, $injector) {
var R = Resource(
urlBase + "/Users/:id",
{ 'id': '@id' },
{
"create": {
url: urlBase + "/Users",
method: "POST"
},
"createMany": {
isArray: true,
url: urlBase + "/Users",
method: "POST"
},
"upsert": {
url: urlBase + "/Users",
method: "PUT"
},
"exists": {
url: urlBase + "/Users/:id/exists",
method: "GET"
},
"findById": {
url: urlBase + "/Users/:id",
method: "GET"
},
"find": {
isArray: true,
url: urlBase + "/Users",
method: "GET"
},
"findOne": {
url: urlBase + "/Users/findOne",
method: "GET"
},
"updateAll": {
url: urlBase + "/Users/update",
method: "POST"
},
"deleteById": {
url: urlBase + "/Users/:id",
method: "DELETE"
},
"count": {
url: urlBase + "/Users/count",
method: "GET"
},
"prototype$updateAttributes": {
url: urlBase + "/Users/:id",
method: "PUT"
},
"createChangeStream": {
url: urlBase + "/Users/change-stream",
method: "POST"
},
"login": {
params: {
include: "user"
},
interceptor: {
response: function(response) {
var accessToken = response.data;
LoopBackAuth.setUser(accessToken.id, accessToken.userId, accessToken.user);
LoopBackAuth.rememberMe = response.config.params.rememberMe !== false;
LoopBackAuth.save();
return response.resource;
}
},
url: urlBase + "/Users/login",
method: "POST"
},
"logout": {
interceptor: {
response: function(response) {
LoopBackAuth.clearUser();
LoopBackAuth.clearStorage();
return response.resource;
}
},
url: urlBase + "/Users/logout",
method: "POST"
},
"confirm": {
url: urlBase + "/Users/confirm",
method: "GET"
},
"resetPassword": {
url: urlBase + "/Users/reset",
method: "POST"
},
"getCurrent": {
url: urlBase + "/Users" + "/:id",
method: "GET",
params: {
id: function() {
var id = LoopBackAuth.currentUserId;
if (id == null) id = '__anonymous__';
return id;
},
},
interceptor: {
response: function(response) {
LoopBackAuth.currentUserData = response.data;
return response.resource;
}
},
__isGetCurrentUser__ : true
}
}
);
R["updateOrCreate"] = R["upsert"];
R["update"] = R["updateAll"];
R["destroyById"] = R["deleteById"];
R["removeById"] = R["deleteById"];
R.getCachedCurrent = function() {
var data = LoopBackAuth.currentUserData;
return data ? new R(data) : null;
};
R.isAuthenticated = function() {
return this.getCurrentId() != null;
};
R.getCurrentId = function() {
return LoopBackAuth.currentUserId;
};
R.modelName = "User";
return R;
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment