Skip to content

Commit

Permalink
angularjs + user.space example
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasjm committed Dec 27, 2016
1 parent 50d46b4 commit 43091d5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
1 change: 0 additions & 1 deletion CNAME

This file was deleted.

4 changes: 4 additions & 0 deletions examples/angularjs/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ angular.module('todomvc', ['ngRoute', 'ngResource'])
}
};

This comment has been minimized.

Copy link
@sebasjm

sebasjm Jan 16, 2017

Author

Here is how you integrate https://user.space with your open source and single page application. First you need to solve the login part. You can do that by copy-pasting the next three lines at the start of your application. With this you will be sure that the user is logged in.

var token = (window.location.search.match(/[\?&]token=(.*)[#&]?/) || []).pop() || localStorage.id_token

This comment has been minimized.

Copy link
@sebasjm

sebasjm Jan 16, 2017

Author

look for the token in the url (if the user just logged in) or localStorage (if the user has a previous session)

if (!token) window.location = `https://gateway.user.space/sign/${btoa(window.location.origin + window.location.pathname)}`

This comment has been minimized.

Copy link
@sebasjm

sebasjm Jan 16, 2017

Author

if the user it's not logged, go to the login page with the return url (thats also the application id)

localStorage.id_token = token

This comment has been minimized.

Copy link
@sebasjm

sebasjm Jan 16, 2017

Author

save the token for later use


$routeProvider
.when('/', routeConfig)
.when('/:status', routeConfig)
Expand Down
28 changes: 15 additions & 13 deletions examples/angularjs/js/services/todoStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ angular.module('todomvc')

// Detect if an API backend is present. If so, return the API module, else
// hand off the localStorage adapter
return $http.get('/api')
return $http.get('https://gateway.user.space/main/classes/ToDo', {headers: {'Authorization': `Bearer ${localStorage.id_token}`}})
.then(function () {
return $injector.get('api');
}, function () {
Expand All @@ -27,32 +27,34 @@ angular.module('todomvc')
var store = {
todos: [],

api: $resource('/api/todos/:id', null,
api: $resource('https://gateway.user.space/main/classes/ToDo/:id', null,

This comment has been minimized.

Copy link
@sebasjm

sebasjm Jan 16, 2017

Author

Finally, you should point all your request to https://gateway.user.space/main/classes/<entity>

{
update: { method:'PUT' }
save: {headers: {'Authorization': `Bearer ${localStorage.id_token}`}, method: 'POST'},

This comment has been minimized.

Copy link
@sebasjm

sebasjm Jan 16, 2017

Author

...and send the token as your Authorization header.

query: {headers: {'Authorization': `Bearer ${localStorage.id_token}`}},
update: {headers: {'Authorization': `Bearer ${localStorage.id_token}`}, method: 'POST', transformRequest : function(data) {
// remove attributes that we dont want to update
return angular.toJson( Object.assign({}, data, {'_method' : 'PUT', createdAt: undefined, updatedAt: undefined, objectId: undefined}) );
}},
delete: {headers: {'Authorization': `Bearer ${localStorage.id_token}`}, method: 'DELETE'}

This comment has been minimized.

Copy link
@sebasjm

sebasjm Jan 16, 2017

Author

That's it! No setup, no configuration... more documentation here

}
),

clearCompleted: function () {
var originalTodos = store.todos.slice(0);

var incompleteTodos = store.todos.filter(function (todo) {
if (todo.completed) store.api.delete({id : todo.objectId})
return !todo.completed;
});

angular.copy(incompleteTodos, store.todos);

return store.api.delete(function () {
}, function error() {
angular.copy(originalTodos, store.todos);
});
return angular.copy(incompleteTodos, store.todos);
},

delete: function (todo) {
var originalTodos = store.todos.slice(0);

store.todos.splice(store.todos.indexOf(todo), 1);
return store.api.delete({ id: todo.id },
return store.api.delete({ id: todo.objectId },
function () {
}, function error() {
angular.copy(originalTodos, store.todos);
Expand All @@ -61,7 +63,7 @@ angular.module('todomvc')

get: function () {
return store.api.query(function (resp) {
angular.copy(resp, store.todos);
angular.copy(resp.results, store.todos);
});
},

Expand All @@ -70,7 +72,7 @@ angular.module('todomvc')

return store.api.save(todo,
function success(resp) {
todo.id = resp.id;
todo.objectId = resp.objectId;
store.todos.push(todo);
}, function error() {
angular.copy(originalTodos, store.todos);
Expand All @@ -79,7 +81,7 @@ angular.module('todomvc')
},

put: function (todo) {
return store.api.update({ id: todo.id }, todo)
return store.api.update({ id: todo.objectId }, todo)
.$promise;
}
};
Expand Down

0 comments on commit 43091d5

Please sign in to comment.