Advertisement
Guest User

Untitled

a guest
Sep 17th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. (function() {
  2.  
  3. 'use strict';
  4. angular
  5. .module('authApp', ['ui.router', 'satellizer','ngFileUpload'])
  6. .config(function($stateProvider, $urlRouterProvider, $authProvider, $httpProvider, $provide) {
  7.  
  8. function redirectWhenLoggedOut($q, $injector) {
  9.  
  10. return {
  11.  
  12. responseError: function(rejection) {
  13.  
  14. // Need to use $injector.get to bring in $state or else we get
  15. // a circular dependency error
  16. var $state = $injector.get('$state');
  17.  
  18. // Instead of checking for a status code of 400 which might be used
  19. // for other reasons in Laravel, we check for the specific rejection
  20. // reasons to tell us if we need to redirect to the login state
  21. var rejectionReasons = ['token_not_provided', 'token_expired', 'token_absent', 'token_invalid'];
  22.  
  23. // Loop through each rejection reason and redirect to the login
  24. // state if one is encountered
  25. angular.forEach(rejectionReasons, function(value, key) {
  26.  
  27. if(rejection.data.error === value) {
  28.  
  29. // If we get a rejection corresponding to one of the reasons
  30. // in our array, we know we need to authenticate the user so
  31. // we can remove the current user from local storage
  32. localStorage.removeItem('user');
  33.  
  34. // Send the user to the auth state so they can login
  35. $state.go('auth');
  36. }
  37. });
  38.  
  39. return $q.reject(rejection);
  40. }
  41. }
  42. }
  43.  
  44. // Setup for the $httpInterceptor
  45. $provide.factory('redirectWhenLoggedOut', redirectWhenLoggedOut);
  46.  
  47. // Push the new factory onto the $http interceptor array
  48. $httpProvider.interceptors.push('redirectWhenLoggedOut');
  49.  
  50. $authProvider.loginUrl = '/api/authenticate';
  51.  
  52. $urlRouterProvider.otherwise('/auth');
  53.  
  54. $stateProvider
  55. .state('auth', {
  56. url: '/auth',
  57. templateUrl : 'views/login.html',
  58. controller: 'AuthController as auth',
  59. })
  60.  
  61. .state('profile', {
  62. url: '/profile',
  63. templateUrl : 'views/edit-profile.html',
  64. controller: 'AuthController as auth',
  65.  
  66.  
  67. })
  68.  
  69. // route for the signup page
  70. .state('signup', {
  71. url: '/signup',
  72. templateUrl : '../views/register.html'
  73.  
  74. })
  75. .state('home', {
  76. url: '/home',
  77. templateUrl: '../views/home.html',
  78. controller: 'AuthController as auth'
  79. });
  80.  
  81. })
  82. .run(function($rootScope, $state) {
  83.  
  84. // $stateChangeStart is fired whenever the state changes. We can use some parameters
  85. // such as toState to hook into details about the state as it is changing
  86. $rootScope.$on('$stateChangeStart', function(event, toState) {
  87.  
  88. // Grab the user from local storage and parse it to an object
  89. var user = JSON.parse(localStorage.getItem('user'));
  90.  
  91. // If there is any user data in local storage then the user is quite
  92. // likely authenticated. If their token is expired, or if they are
  93. // otherwise not actually authenticated, they will be redirected to
  94. // the auth state because of the rejected request anyway
  95. if(user) {
  96.  
  97. // The user's authenticated state gets flipped to
  98. // true so we can now show parts of the UI that rely
  99. // on the user being logged in
  100. $rootScope.authenticated = true;
  101.  
  102. // Putting the user's data on $rootScope allows
  103. // us to access it anywhere across the app. Here
  104. // we are grabbing what is in local storage
  105. $rootScope.currentUser = user;
  106.  
  107. // If the user is logged in and we hit the auth route we don't need
  108. // to stay there and can send the user to the main state
  109. if(toState.name === "auth") {
  110.  
  111. // Preventing the default behavior allows us to use $state.go
  112. // to change states
  113. event.preventDefault();
  114.  
  115. // go to the "main" state which in our case is users
  116. $state.go('profile');
  117. }
  118. }
  119. });
  120. });
  121. })();
  122.  
  123. (function() {
  124.  
  125. 'use strict';
  126.  
  127. angular
  128. .module('authApp')
  129. .controller('AuthController', AuthController);
  130.  
  131.  
  132. function AuthController($auth, $state, $http, $rootScope,$scope,Upload) {
  133.  
  134. var vm = this;
  135. vm.loginError = false;
  136. vm.loginErrorText;
  137. vm.login = function() {
  138.  
  139. var credentials = {
  140. email: vm.email,
  141. password: vm.password
  142. }
  143.  
  144. // Use Satellizer's $auth service to login
  145. $auth.login(credentials).then(function(data) {
  146.  
  147. return $http.get('api/user').then(function(response) {
  148.  
  149. // Stringify the returned data to prepare it
  150. // to go into local storage
  151. var user = JSON.stringify(response.data.user);
  152.  
  153. // Set the stringified user data into local storage
  154. localStorage.setItem('user', user);
  155.  
  156. // The user's authenticated state gets flipped to
  157. // true so we can now show parts of the UI that rely
  158. // on the user being logged in
  159. $rootScope.authenticated = true;
  160. $rootScope.username=user.name;
  161.  
  162.  
  163.  
  164. // Putting the user's data on $rootScope allows
  165. // us to access it anywhere across the app
  166. $rootScope.currentUser = response.data.user;
  167. console.log($rootScope.currentUser.profile_picture);
  168. // Everything worked out so we can now redirect to
  169. // the users state to view the data
  170. $state.go('home');
  171.  
  172. });
  173.  
  174. // If login is successful, redirect to the users state
  175. $state.go('users');
  176. }, function(error) {
  177. console.log(error);
  178. });
  179. }
  180.  
  181. vm.logout = function() {
  182.  
  183. $auth.logout().then(function() {
  184.  
  185. // Remove the authenticated user from local storage
  186. localStorage.removeItem('user');
  187.  
  188. // Flip authenticated to false so that we no longer
  189. // show UI elements dependant on the user being logged in
  190. $rootScope.authenticated = false;
  191.  
  192. // Remove the current user info from rootscope
  193. $rootScope.currentUser = null;
  194.  
  195. });
  196. }
  197. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement