Last active
May 1, 2017 02:47
-
-
Save JBreit/3ed0ee0fb8186e6ad17b0886194a89d9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /*global angular $*/ | |
| (function () { | |
| 'use strict'; | |
| angular.module('innermind.controllers', []) | |
| .controller('IndexCtrl', [ | |
| '$scope', | |
| '$state', | |
| '$http', | |
| function ($scope, $state, $http) { | |
| console.log($state); | |
| $http.get('/api/').then(function (response) { | |
| //console.log(response); | |
| $scope.data = response.data; | |
| }); | |
| } | |
| ]) | |
| .controller('MainCtrl', [ | |
| '$scope', | |
| '$state', | |
| '$http', | |
| '$location', | |
| 'Auth', | |
| function ($scope, $state, $http, $location, Auth) { | |
| console.log($state); | |
| $scope.error = Object.create(null); | |
| $scope.user = Object.create(null); | |
| $http.get('/api/auth/login').then(function (response) { | |
| //console.log(response); | |
| $scope.data = response.data; | |
| }); | |
| $scope.login = function () { | |
| $scope.error = false; | |
| $scope.disabled = true; | |
| $scope.loading = true; | |
| Auth.login($scope.user.username, $scope.user.password) | |
| .success(function (data, status) { | |
| if (status === 200) { | |
| $scope.data = data; | |
| $location.path('/'); | |
| $scope.disabled = false; | |
| $scope.formData = {}; | |
| } | |
| }) | |
| .error(function (data, status) { | |
| console.log(data); | |
| console.log(status); | |
| $scope.error = true; | |
| $scope.message = 'Invalid username or password'; | |
| $scope.disabled = false; | |
| $scope.user = {}; | |
| }); | |
| }; | |
| $scope.logout = function () { | |
| Auth.logout() | |
| .then(function () { | |
| $location.path('/auth/login'); | |
| }); | |
| }; | |
| } | |
| ]) | |
| .controller('UserCtrl', [ | |
| '$scope', | |
| '$state', | |
| function ($scope, $state) { | |
| $scope.user = {}; | |
| console.log($state); | |
| } | |
| ]); | |
| }()); |
This file contains hidden or 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
| /*global angular $*/ | |
| (function () { | |
| 'use strict'; | |
| angular.module('innermind', ['ui.router', 'ngStorage', 'innermind.controllers', 'innermind.services']) | |
| .config([ | |
| '$qProvider', | |
| '$stateProvider', | |
| '$urlRouterProvider', | |
| '$locationProvider', | |
| function ($qProvider, $stateProvider, $urlRouterProvider, $locationProvider) { | |
| $qProvider.errorOnUnhandledRejections(false); | |
| $stateProvider | |
| .state('app', { | |
| abstract: true, | |
| url: '/', | |
| template: '<div ui-view></div>' | |
| }) | |
| .state('app/public', { | |
| abstract: true, | |
| url: '/', | |
| template: '<div ui-view></div>' | |
| }) | |
| .state('app/public/', { | |
| url: '/', | |
| templateUrl: 'innermind/components/public/templates/index.html', | |
| controller: 'IndexCtrl', | |
| access: { | |
| restricted: false | |
| }, | |
| resolve: { | |
| data: function ($http) { | |
| return $http.get('/api/'); | |
| } | |
| } | |
| }) | |
| .state('app/auth', { | |
| abstract: true, | |
| url: '/auth', | |
| template: '<div ui-view></div>' | |
| }) | |
| .state('app/auth/login', { | |
| url: '/auth/login', | |
| templateUrl: 'innermind/components/auth/templates/login.html', | |
| controller: 'MainCtrl', | |
| access: { | |
| restricted: false | |
| }, | |
| resolve: { | |
| data: function ($http) { | |
| return $http.get('/api/auth/login'); | |
| } | |
| } | |
| }) | |
| .state('app/auth/logout', { | |
| url: '/auth/logout', | |
| templateUrl: 'innermind/components/auth/templates/login.html', | |
| controller: 'MainCtrl', | |
| access: { | |
| restricted: false | |
| } | |
| }) | |
| .state('app/auth/register', { | |
| url: '/auth/register', | |
| templateUrl: 'innermind/components/auth/templates/register.html', | |
| controller: 'MainCtrl', | |
| access: { | |
| restricted: false | |
| } | |
| }) | |
| .state('app/auth/', { | |
| url: 'app/auth/', | |
| templateUrl: 'innermind/components/dashboard/templates/dashboard.html', | |
| controller: 'IndexCtrl', | |
| access: { | |
| restricted: true | |
| }, | |
| resolve: { | |
| data: function ($http) { | |
| return $http.get('/api/'); | |
| } | |
| } | |
| }); | |
| $urlRouterProvider.otherwise('app/auth/ '); | |
| $locationProvider.html5Mode(true); | |
| //$locationProvider.hashPrefix('!'); | |
| } | |
| ]) | |
| .run([ | |
| '$rootScope', | |
| '$state', | |
| '$stateParams', | |
| '$location', | |
| 'Auth', | |
| function ($rootScope, $state, $stateParams, $location, Auth) { | |
| var on$stateChangeStateStart = function (event, next, nextParams, previous, previousParams, options) { | |
| console.log('event: ', event); | |
| console.log('next: ', next); | |
| console.log('nextParams: ', nextParams); | |
| console.log('previous: ', previous); | |
| console.log('previousParams: ', previousParams); | |
| console.log('options: ', options); | |
| Auth.getUserStatus() | |
| .then(function () { | |
| if (next.access.restricted && !Auth.isAuthenticated()) { | |
| $location.path('/auth/login'); | |
| $state.reload(); | |
| } | |
| }); | |
| }; | |
| var on$stateChangeStateSuccess = function (event, toState, toParams, fromState, fromParams) { | |
| /*console.log('event: ', event); | |
| console.log('toState: ', toState); | |
| console.log('toParams: ', toParams); | |
| console.log('fromState: ', fromState); | |
| console.log('fromParams: ', fromParams);*/ | |
| $('.sidebar').find('[ui-sref="' + toState.name + '"]').addClass('selected'); | |
| $('.sidebar').find('[ui-sref="' + fromState.name + '"]').removeClass('selected'); | |
| }; | |
| /* | |
| * It's very handy to add references to $state and $stateParams to the $rootScope | |
| * so that you can access them from any scope within your applications.For example, | |
| * <li ng-class="{ active: $state.includes('contacts.list') }"> will set the <li> | |
| * to active whenever 'contacts.list' or one of its decendents is active. | |
| */ | |
| $rootScope.$state = $state; | |
| $rootScope.$stateParams = $stateParams; | |
| $rootScope.$on('$stateChangeStart', on$stateChangeStateStart); | |
| $rootScope.$on('$stateChangeSuccess', on$stateChangeStateSuccess); | |
| } | |
| ]); | |
| }()); |
This file contains hidden or 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
| /*global angular $*/ | |
| (function () { | |
| 'use strict'; | |
| angular.module('innermind.services', []) | |
| .factory('User', function ($http) { | |
| return { | |
| getAll: function () { | |
| return $http.get('/api/users'); | |
| } | |
| }; | |
| }) | |
| .factory('Auth', function AuthenticationService($q, $timeout, $http, $localStorage) { | |
| var service = Object.create(null), | |
| user = Object.create(null); | |
| service.isAuthenticated = function () { | |
| if (user) { | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| }; | |
| service.getUserStatus = function () { | |
| var deferred = $q.defer(); | |
| $http.get('/api/') | |
| .then(function (data, status) { | |
| $timeout(function () { | |
| if (status === 200 && data.status) { | |
| user = true; | |
| deferred.resolve(data); | |
| } else { | |
| user = false; | |
| deferred.reject(data); | |
| } | |
| }, 500); | |
| }) | |
| .catch(function (data) { | |
| user = false; | |
| deferred.reject(data); | |
| }); | |
| return deferred.promise; | |
| }; | |
| service.login = function (username, password) { | |
| var deferred = $q.defer(); | |
| $http.post('/auth/login', {username: username, password: password}) | |
| .success(function (data, status) { | |
| $timeout(function () { | |
| console.log(data); | |
| if (status === 200) { | |
| user = true; | |
| deferred.resolve(); | |
| } else { | |
| user = false; | |
| deferred.reject(); | |
| } | |
| }, 500); | |
| }) | |
| .error(function (data) { | |
| console.log(data); | |
| user = false; | |
| deferred.reject(); | |
| }); | |
| return deferred.promise(); | |
| }; | |
| service.logout = function () { | |
| var deferred = $q.defer(); | |
| $http.get('/auth/logout') | |
| .then(function (data, status) { | |
| $timeout(function () { | |
| console.log(data); | |
| console.log(status); | |
| user = false; | |
| deferred.resolve(); | |
| }, 500); | |
| }) | |
| .catch(function (data) { | |
| console.log(data); | |
| user = false; | |
| deferred.reject(); | |
| }); | |
| return deferred.promise(); | |
| /*delete $localStorage.currentUser; | |
| $http.defaults.headers.common.Authorization = '';*/ | |
| }; | |
| service.register = function (username, password) { | |
| var deferred = $q.defer(); | |
| $http.post('/auth/register', {username: username, password: password}) | |
| .success(function (data, status) { | |
| $timeout(function () { | |
| if (status === 200 && data.status) { | |
| deferred.resolve(); | |
| } else { | |
| deferred.reject(); | |
| } | |
| }, 500); | |
| }) | |
| .error(function (data) { | |
| console.log(data); | |
| deferred.reject(); | |
| }); | |
| }; | |
| return service; | |
| }); | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment