2015-01-08 3 views
0

AngularJS에서 새로 생겼습니다. 사용자가 로그인하면 내 페이지를 대시 보드로 리디렉션하고 싶습니다. 로그인 후에 쿠키에 저장하고있는 액세스 토큰을 얻고 있습니다. 내가 스택 오버플로의 솔루션을 확인한 다음 내 문제가 해결되지 않습니다.사용자가 AngularJS로 로그인 한 경우 대시 보드로 리디렉션하는 방법은 무엇입니까?

여기 내 코드입니다 :

(function(window){ 

var app= angular.module('customersApp',['ngRoute','ngCookies','ui.bootstrap']); 

app.config(['$routeProvider', 
    function ($routeProvider) { 
     $routeProvider. 
     when('/login', { 
      title: 'Login', 
      controller: 'loginController', 
       templateUrl: 'app/views/loginuser.html' 
     }) 
      .when('/logout', { 
       title: 'Logout', 
       templateUrl: 'partials/login.html', 
       controller: 'loginController' 
      }) 

      .when('/dashboard', { 
       title: 'Dashboard', 
       templateUrl: 'app/views/dynamic_table.html', 
       controller: 'dashboard' 
      }) 
      .when('/verified_artists', { 
       title: 'Verified Artists', 
       templateUrl: 'app/views/verified_artists.html', 
       controller: 'artistController' 
      }) 
      .when('/new_artists', { 
       title: 'New Request Artists', 
       templateUrl: 'app/views/new_artists.html', 
       controller: 'artistController' 
      }) 

      .otherwise({ 
       redirectTo: '/login' 
      }); 
    }]); 
    window.app = app; 

}(window)); 

loginController.js

app.controller('loginController', function ($scope,$http,$cookies,$cookieStore) { 


    //initially set those objects to null to avoid undefined error 
    $scope.login = {}; 
    $scope.signup = {}; 
    $scope.doLogin = function (customer) { 


    $.post("websiteurl.com/admin_login", 
    { 

    user_email : $scope.login.email, 
     password : $scope.login.password 

    }, 


    function(data,status){ 

     data = JSON.parse(data); 
     console.log(data); 

    var someSessionObj = { 'accesstoken' : data.access_token}; 

    $cookies.dotobject = someSessionObj; 
    $scope.usingCookies = { 'cookies.dotobject' : $cookies.dotobject, "cookieStore.get" : $cookieStore.get('dotobject') }; 

    $cookieStore.put('obj', someSessionObj); 
    $scope.usingCookieStore = { "cookieStore.get" : $cookieStore.get('obj'), 'cookies.dotobject' : $cookies.obj, }; 

    console.log($cookieStore.get('obj').accesstoken); 

    if(data.flag==10) 
     { 
      alert(data.error); 
     } 
     else 
     { 
     window.location.href = "#/dashboard"; 

     } 


    }) 


    }; 


}); 
+0

로그인 한 사용자가 웹 페이지를 다시 열면 대시 보드로 리디렉션하고 싶습니다. 내 이해가 맞습니까? 또한 위의 코드가 작동합니까? – CodingNinja

+0

예, 사용자가 로그인 한 경우 로그 아웃 버튼을 클릭하지 않을 때까지 로그인 페이지로 돌아 가지 않습니다. – Prince

+0

사용 app.run ([ '$ cookieStore', function ($ cookieStore) { // 쿠키가 있으면 대시 보드로 리디렉션 }})' – CodingNinja

답변

0

시도 경로 변경 이벤트를 찾아 쿠키 저장소를 사용하여 쿠키에 대한 확인을 app.js ...

$rootScope.$on("$routeChangeStart", function(event, next, current) { 
 
    // check for the cookie 
 
    // if present 
 
    // check next route 
 
     // if its login/signup page 
 
     // event.preventDefault(); (don't make the route change, redirect to dashboard) 
 
     // $location.path("/dashboard"); 
 
     
 
     // if it is some other route 
 
     // allow it. 
 
    
 
    // if cookie not present 
 
    // $location.path("/login"); 
 
});

관련 문제