Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- angular.module("proyectApp",["ngRoute","ngResource","ngCookies"])
- .config(function($routeProvider) {
- $routeProvider
- .when('/',{
- controller:'inicioController',
- templateUrl:'pages/inicio.html'
- })
- .when('/login',{
- controller:'loginController',
- templateUrl:'pages/login.html'
- })
- .when('/home',{
- controller:'homeController',
- templateUrl:'pages/home.html'
- })
- .when('/profile',{
- controller:'profileController',
- templateUrl:'pages/profile.html'
- })
- .when('/dashboard',{
- controller:'acercaController',
- templateUrl:'pages/acerca.html'
- })
- .when('/error-404',{
- controller:'404Controller',
- templateUrl:'404.html'
- })
- .otherwise({
- redirectTo:'/error-404'
- })
- })
- .run(function($rootScope, auth)
- {
- //изменение маршрутов
- $rootScope.$on('$routeChangeStart', function()
- {
- //мы называем checkStatus, который мы определили в auth factoria
- //которые мы ввели в действие запуска приложения
- auth.checkStatus();
- })
- })
- angular.module("proyectApp")
- .controller("inicioController",function($scope,$location){
- //ng-submit
- $scope.dashboard = function(){
- $location.path("/home");
- }
- })
- .controller("loginController",function($scope,$http,$routeParams,$route,$cookies,$cookieStore,$location,auth){
- //ng-submit
- $scope.loginUser = function(){
- $http({
- method: "post",
- url: "conexion/sign.php",
- data: {
- usuario: $scope.user,
- contra: $scope.pass
- },
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
- }).success(function(dato){
- if (dato.trim() == "success") {
- console.log("добро пожаловать");
- auth.login($scope.user, $scope.pass);
- }
- }).error(function(dato) {
- console.log(dato);
- });;
- }
- })
- .controller("homeController",function($scope,$http,$routeParams,$route,$cookies,$cookieStore,$location,auth){
- $scope.profile = function(){
- $location.path("/profile");
- }
- $scope.logout = function()
- {
- auth.logout();
- }
- })
- .controller("profileController",function($scope,$http,$routeParams,$route,$cookies,$cookieStore,$location,auth){
- $scope.logout = function()
- {
- auth.logout();
- }
- })
- angular.module("proyectoApp")
- .factory("auth", function($routeParams,$location,$route,$cookies,$cookieStore){
- return{
- login : function(username, password)
- {
- //мы создаем файл cookie с именем, которое мы прошли
- $cookies.username = username,
- $cookies.password = password;
- //мы отправляем домой
- $location.path("/home");
- },
- logout : function()
- {
- //для выхода из системы мы удаляем файл cookie с $ cookieStore.remove
- $cookieStore.remove("username"),
- $cookieStore.remove("password");
- delete $cookies["username"];
- delete $cookies["password"];
- //мы войдем в систему
- $location.path("/login");
- },
- checkStatus : function()
- {
- //мы создаем массив с маршрутами, которые мы хотим контролировать
- var rutasPrivadas = ["/login","/home","/profile","/dashboard","/"];
- if(this.in_array($location.path(),rutasPrivadas) && typeof($cookies.username) == "undefined")
- {
- console.log("изменения..1.");
- $location.path("/login");
- }
- //в случае, если вы попытаетесь получить доступ к логину и уже вошли в систему, мы отправляем его на родину
- if(this.in_array("/login",rutasPrivadas) && typeof($cookies.username) != "undefined")
- {
- console.log("изменения..2.");
- $location.path("/home");
- }
- },
- in_array : function(needle, haystack)
- {
- var key = '';
- for(key in haystack)
- {
- if(haystack[key] == needle)
- {
- return true;
- }
- }
- return false;
- }
- }
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement