Skip to content

Commit

Permalink
logout user and query server only if user is loggedIn
Browse files Browse the repository at this point in the history
  • Loading branch information
janantala committed Oct 6, 2013
1 parent e67b6cc commit c3133bd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
5 changes: 5 additions & 0 deletions admin/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
</div>
</form>

<h4 ng-show="user.name" ng-click="signout()">
<i class="icon-signout"></i>
<a href="#"><span ng-bind="user.name"></span></a>
</h4>

<a href="http://www.spolocenstvounited.sk" class="nav-logo"></a>

</nav>
Expand Down
29 changes: 21 additions & 8 deletions admin/app/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ angular.module('adminApp', ['ngResource', 'ja.isbn', 'ui.bootstrap'])
templateUrl: 'views/users.html',
controller: 'UsersCtrl',
resolve: {
users: function($q, $route, Users){
users: function($q, $route, Users, Auth){
var deferred = $q.defer();
if (!Auth.isLoggedIn()) { return deferred.reject(); }

Users.query($route.current.params,
function(users){
console.log(users);
Expand All @@ -31,8 +33,9 @@ angular.module('adminApp', ['ngResource', 'ja.isbn', 'ui.bootstrap'])
templateUrl: 'views/books.html',
controller: 'BooksCtrl',
resolve: {
books: function($q, $route, Books){
books: function($q, $route, Books, Auth){
var deferred = $q.defer();
if (!Auth.isLoggedIn()) { return deferred.reject(); }
Books.query($route.current.params,
function(books){
console.log(books);
Expand All @@ -51,8 +54,9 @@ angular.module('adminApp', ['ngResource', 'ja.isbn', 'ui.bootstrap'])
templateUrl: 'views/rents.html',
controller: 'RentsCtrl',
resolve: {
rents: function($q, $route, Rents){
rents: function($q, $route, Rents, Auth){
var deferred = $q.defer();
if (!Auth.isLoggedIn()) { return deferred.reject(); }
Rents.query($route.current.params,
function(rents){
console.log(rents);
Expand All @@ -71,8 +75,9 @@ angular.module('adminApp', ['ngResource', 'ja.isbn', 'ui.bootstrap'])
templateUrl: 'views/books/bookId.html',
controller: 'BooksBookidCtrl',
resolve: {
book: function($q, $route, Books){
book: function($q, $route, Books, Auth){
var deferred = $q.defer();
if (!Auth.isLoggedIn()) { return deferred.reject(); }
Books.get({'bookId': $route.current.params.bookId},
function(book){
console.log(book);
Expand All @@ -91,8 +96,9 @@ angular.module('adminApp', ['ngResource', 'ja.isbn', 'ui.bootstrap'])
templateUrl: 'views/users/userId.html',
controller: 'UsersUseridCtrl',
resolve: {
user: function($q, $route, Users){
user: function($q, $route, Users, Auth){
var deferred = $q.defer();
if (!Auth.isLoggedIn()) { return deferred.reject(); }
Users.get({'userId': $route.current.params.userId},
function(user){
console.log(user);
Expand All @@ -111,8 +117,9 @@ angular.module('adminApp', ['ngResource', 'ja.isbn', 'ui.bootstrap'])
templateUrl: 'views/rents/rentId.html',
controller: 'RentsRentidCtrl',
resolve: {
rent: function($q, $route, Rents){
rent: function($q, $route, Rents, Auth){
var deferred = $q.defer();
if (!Auth.isLoggedIn()) { return deferred.reject(); }
Rents.get({'rentId': $route.current.params.rentId},
function(rent){
console.log(rent);
Expand All @@ -131,8 +138,9 @@ angular.module('adminApp', ['ngResource', 'ja.isbn', 'ui.bootstrap'])
templateUrl: 'views/search.html',
controller: 'SearchCtrl',
resolve: {
books: function($q, $route, Search){
books: function($q, $route, Search, Auth){
var deferred = $q.defer();
if (!Auth.isLoggedIn()) { return deferred.reject(); }
Search.query($route.current.params,
function(books){
console.log(books);
Expand Down Expand Up @@ -161,9 +169,14 @@ angular.module('adminApp', ['ngResource', 'ja.isbn', 'ui.bootstrap'])
$location.url('search?q=' + query);
};

$rootScope.signout = function() {
Auth.logout();
$location.path( '/' );
};

$rootScope.$on('$routeChangeStart', function(event, next, current) {
console.log(next.templateUrl);

console.log(Auth.isLoggedIn());
if ( !Auth.isLoggedIn() ) {
if ( next.templateUrl !== 'views/main.html' ) {
$location.path( '/' );
Expand Down
5 changes: 5 additions & 0 deletions admin/app/scripts/services/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ angular.module('adminApp')
},
isLoggedIn: function() {
return ($rootScope.user.hash && $rootScope.user.hash.length) ? true : false;
},
logout: function() {
$rootScope.user = {};
localStorage.setItem(STORAGE_ID, JSON.stringify('{}'));
return true;
}
};
});
2 changes: 1 addition & 1 deletion admin/app/views/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h3 class="text-center">Prihlásenie</h3>

<div class="control-group">
<div class="controls">
<button type="submit" class="btn btn-success"><i class="icon-upload"></i> Prihlásiť sa</button>
<button type="submit" class="btn btn-success"><i class="icon-signin"></i> Prihlásiť sa</button>
</div>
</div>
</form>
Expand Down

0 comments on commit c3133bd

Please sign in to comment.