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)
- {
- //al cambiar de rutas
- $rootScope.$on('$routeChangeStart', function()
- {
- //llamamos a checkStatus, el cual lo hemos definido en la factoria auth
- //la cuál hemos inyectado en la acción run de la aplicación
- 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("Bienvenido");
- 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)
- {
- //creamos la cookie con el nombre que nos han pasado
- $cookies.username = username,
- $cookies.password = password;
- //mandamos a la home
- $location.path("/home");
- },
- logout : function()
- {
- //al hacer logout eliminamos la cookie con $cookieStore.remove
- $cookieStore.remove("username"),
- $cookieStore.remove("password");
- delete $cookies["username"];
- delete $cookies["password"];
- //mandamos al login
- $location.path("/login");
- },
- checkStatus : function()
- {
- //creamos un array con las rutas que queremos controlar
- var rutasPrivadas = ["/login","/home","/profile","/dashboard","/"];
- if(this.in_array($location.path(),rutasPrivadas) && typeof($cookies.username) == "undefined")
- {
- console.log("Cambiando..1.");
- $location.path("/login");
- }
- //en el caso de que intente acceder al login y ya haya iniciado sesión lo mandamos a la home
- if(this.in_array("/login",rutasPrivadas) && typeof($cookies.username) != "undefined")
- {
- console.log("Cambiando..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