Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. angular.module("proyectApp",["ngRoute","ngResource","ngCookies"])
  2. .config(function($routeProvider) {
  3. $routeProvider
  4. .when('/',{
  5. controller:'inicioController',
  6. templateUrl:'pages/inicio.html'
  7. })
  8. .when('/login',{
  9. controller:'loginController',
  10. templateUrl:'pages/login.html'
  11. })
  12. .when('/home',{
  13. controller:'homeController',
  14. templateUrl:'pages/home.html'
  15. })
  16. .when('/profile',{
  17. controller:'profileController',
  18. templateUrl:'pages/profile.html'
  19. })
  20. .when('/dashboard',{
  21. controller:'acercaController',
  22. templateUrl:'pages/acerca.html'
  23. })
  24. .when('/error-404',{
  25. controller:'404Controller',
  26. templateUrl:'404.html'
  27. })
  28. .otherwise({
  29. redirectTo:'/error-404'
  30. })
  31. })
  32. .run(function($rootScope, auth)
  33. {
  34. //изменение маршрутов
  35. $rootScope.$on('$routeChangeStart', function()
  36. {
  37. //мы называем checkStatus, который мы определили в auth factoria
  38. //которые мы ввели в действие запуска приложения
  39. auth.checkStatus();
  40. })
  41. })
  42.  
  43. angular.module("proyectApp")
  44. .controller("inicioController",function($scope,$location){
  45. //ng-submit
  46. $scope.dashboard = function(){
  47. $location.path("/home");
  48. }
  49. })
  50. .controller("loginController",function($scope,$http,$routeParams,$route,$cookies,$cookieStore,$location,auth){
  51.  
  52. //ng-submit
  53. $scope.loginUser = function(){
  54. $http({
  55. method: "post",
  56. url: "conexion/sign.php",
  57. data: {
  58. usuario: $scope.user,
  59. contra: $scope.pass
  60. },
  61. headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  62.  
  63. }).success(function(dato){
  64.  
  65. if (dato.trim() == "success") {
  66. console.log("добро пожаловать");
  67. auth.login($scope.user, $scope.pass);
  68. }
  69.  
  70. }).error(function(dato) {
  71. console.log(dato);
  72. });;
  73. }
  74.  
  75. })
  76. .controller("homeController",function($scope,$http,$routeParams,$route,$cookies,$cookieStore,$location,auth){
  77. $scope.profile = function(){
  78. $location.path("/profile");
  79. }
  80. $scope.logout = function()
  81. {
  82. auth.logout();
  83. }
  84. })
  85. .controller("profileController",function($scope,$http,$routeParams,$route,$cookies,$cookieStore,$location,auth){
  86. $scope.logout = function()
  87. {
  88. auth.logout();
  89. }
  90. })
  91.  
  92. angular.module("proyectoApp")
  93. .factory("auth", function($routeParams,$location,$route,$cookies,$cookieStore){
  94. return{
  95. login : function(username, password)
  96. {
  97. //мы создаем файл cookie с именем, которое мы прошли
  98. $cookies.username = username,
  99. $cookies.password = password;
  100. //мы отправляем домой
  101. $location.path("/home");
  102. },
  103. logout : function()
  104. {
  105. //для выхода из системы мы удаляем файл cookie с $ cookieStore.remove
  106. $cookieStore.remove("username"),
  107. $cookieStore.remove("password");
  108. delete $cookies["username"];
  109. delete $cookies["password"];
  110. //мы войдем в систему
  111. $location.path("/login");
  112. },
  113. checkStatus : function()
  114. {
  115.  
  116. //мы создаем массив с маршрутами, которые мы хотим контролировать
  117. var rutasPrivadas = ["/login","/home","/profile","/dashboard","/"];
  118.  
  119. if(this.in_array($location.path(),rutasPrivadas) && typeof($cookies.username) == "undefined")
  120. {
  121. console.log("изменения..1.");
  122. $location.path("/login");
  123. }
  124. //в случае, если вы попытаетесь получить доступ к логину и уже вошли в систему, мы отправляем его на родину
  125. if(this.in_array("/login",rutasPrivadas) && typeof($cookies.username) != "undefined")
  126. {
  127. console.log("изменения..2.");
  128. $location.path("/home");
  129.  
  130. }
  131.  
  132. },
  133. in_array : function(needle, haystack)
  134. {
  135. var key = '';
  136. for(key in haystack)
  137. {
  138. if(haystack[key] == needle)
  139. {
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. }
  146. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement