Skip to content

Instantly share code, notes, and snippets.

@suryakun
Created July 26, 2015 15:19
Show Gist options
  • Save suryakun/f5fb51231d110c8f14ab to your computer and use it in GitHub Desktop.
Save suryakun/f5fb51231d110c8f14ab to your computer and use it in GitHub Desktop.
angular.module('App', [
'ngCookies',
'ngResource',
'ngSanitize',
'btford.socket-io',
'ui.router',
'ui.bootstrap',
'angularUtils.directives.dirPagination',
'ncy-angular-breadcrumb',
'ngImgCrop'
])
.config(function ($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider, $breadcrumbProvider) {
$urlRouterProvider
.otherwise('/');
$locationProvider.html5Mode(true);
$httpProvider.interceptors.push('authInterceptor');
$breadcrumbProvider.setOptions({
template: '<ul class="breadcrumb"><li ng-repeat="step in steps"><a href="{{step.ncyBreadcrumbLink}}">{{step.ncyBreadcrumbLabel}}</a></li></ul>'
});
})
.factory('authInterceptor', function ($rootScope, $q, $cookieStore, $location) {
return {
// Add authorization token to headers
request: function (config) {
config.headers = config.headers || {};
if ($cookieStore.get('token')) {
config.headers.Authorization = 'Bearer ' + $cookieStore.get('token');
}
return config;
},
// Intercept 401s and redirect you to login
responseError: function(response) {
if(response.status === 401) {
$location.path('/login');
// remove any stale tokens
$cookieStore.remove('token');
return $q.reject(response);
}
else {
return $q.reject(response);
}
}
};
})
.run(function ($rootScope, $location, Auth) {
// Redirect to login if route requires auth and you're not logged in
$rootScope.$on('$stateChangeStart', function (event, next) {
Auth.isLoggedInAsync(function(loggedIn) {
if (next.authenticate && !loggedIn) {
$location.path('/login');
}
});
});
});
'use strict';
angular.module('App')
.controller('MainCtrl', function ($scope, $http, socket) {
$scope.awesomeThings = [];
$http.get('/api/things').success(function(awesomeThings) {
$scope.awesomeThings = awesomeThings;
socket.syncUpdates('thing', $scope.awesomeThings);
});
$scope.addThing = function() {
if($scope.newThing === '') {
return;
}
$http.post('/api/things', { name: $scope.newThing });
$scope.newThing = '';
};
$scope.deleteThing = function(thing) {
$http.delete('/api/things/' + thing._id);
};
$scope.$on('$destroy', function () {
socket.unsyncUpdates('thing');
});
});
'use strict';
angular.module('App')
.config(function ($stateProvider) {
$stateProvider
.state('main', {
url: '/',
templateUrl: 'app/main/main.html',
controller: 'MainCtrl'
})
.state('gallery', {
url: '/gallery',
templateUrl: 'app/main/gallery/gallery.html',
controller: 'GalleryCtrl',
ncyBreadcrumb: {
label: 'Gallery Screencast'
}
})
.state('list', {
url: '/list/:id',
templateUrl: 'app/main/list/list.html',
controller: 'ListCtrl'
})
.state('show', {
url: '/show/:id',
templateUrl: 'app/main/show/show.html',
controller: 'ShowCtrl',
authenticate: true
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment