2016-03-07 35 views
0

我在AngularJS新手,我讀了關於登錄和認證具有角JS教程,但我仍然搞不清楚我的代碼點多,現在我已經到了登錄和存儲在瀏覽器中的一個令牌會話,但我不能重定向到主頁在洛後, 這裏的MyService:認證機制

function authenticationSvc ($http, $q, $window, auth_uri) { 

    var userInfo; 

    function login(username, password) { 

    var deferred = $q.defer(); 

    $http.post(auth_uri, { 
     username: username, 
     password: password 
    }).then(function(result) { 

     userInfo = { 
     accessToken: result.data.token 
     }; 

     $window.sessionStorage["pallas_token"] = result.data.token; 

     deferred.resolve(userInfo); 
    }, 
    function(error) { 
     deferred.reject(error); 
    }); 

    return deferred.promise; 
    } 

    function getUserInfo() { 
    return userInfo; 
    } 

    return { 
    login: login, 
    getUserInfo: getUserInfo 
    }; 

}; 

,這是我的狀態配置

.state('dashboard', { 
    url:'/dashboard', 
    controller: 'HomeController', 
    templateUrl: 'partials/dashboard/main.html', 
    resolve:{ 
    auth: function($q, authenticationSvc) { 
     var userInfo = authenticationSvc.getUserInfo();      
     if (userInfo) {      
     return $q.when(userInfo); 

     } else { 
     return $q.reject({ authenticated: false }); 
     } 
    } 
    } 
} 

最後這個我.RUN塊:

angular 
    .module ('mainApp') 
    .run (function ($rootScope, $state, $location) { 

    $rootScope.$on('$stateChangeSuccess', function(userInfo) { 
     console.log(userInfo);    
    }); 

    $rootScope.$on('$stateChangeError', function(evt, toState, toParams, fromState, fromParams, error) { 
     if (error.authenticated == false) {  
     $state.transitionTo("login"); 
     } 
    }); 
    }); 

請幫我解決這個問題,我需要幫助我的朋友們:(

我對失蹤後我登錄控制器對不起,有:

function LoginController($scope, $state, authenticationSvc){ 
    $scope.submit = function(credentials){ 
    authenticationSvc.login(credentials.username, credentials.password); 
    }; 
}; 
+1

請還顯示您的登錄 – manzapanza

+0

對不起,我已經添加的頁面的控制器還myController的。 –

回答

0

你的登錄方法返回一個當用戶通過認證時成功承諾。所以..你可以用這種方式編輯控制器:

function LoginController($scope, $state, authenticationSvc){ 
    $scope.submit = function(credentials){ 
    authenticationSvc.login(credentials.username, credentials.password).then(function(){ 
     $state.go('dashboard'); 
     console.log('User logged in!'); 
    }).catch(function(){ 
     console.log('User NOT logged in!'); 
    }); 
    }; 
}; 

UPDATE

要將頁面後保持狀態刷新你需要從sessionStorage恢復userInfo對象。我還添加了註銷邏輯!請看:

function authenticationSvc ($http, $q, $window, auth_uri) { 

    var userInfo; 

    function login(username, password) { 
    ... 
    } 

    function logout() { 
    $window.sessionStorage.removeItem("pallas_token"); 
    userInfo = null; 
    } 

    function getUserInfo() { 
    return userInfo; 
    } 

    function init(){ 
    if ($window.sessionStorage["pallas_token"]){ 
     userInfo = { 
     accessToken: $window.sessionStorage["pallas_token"] 
     };  
    } 
    } 

    init(); 

    return { 
    login: login, 
    logout: logout, 
    getUserInfo: getUserInfo 
    }; 

}; 

註銷:

function LoginController($scope, $state, authenticationSvc){ 
    $scope.submit = function(credentials){ 
    ... 
    }; 

    $scope.logout = function(){ 
    authenticationSvc.logout(); 
    $state.go('login'); 
    console.log('User logged out!'); 
    }; 
}; 

享受!

+0

非常感謝manzapanza這種幫助,爲我工作, –

+0

好!如果安澤人是正確的,你應該考慮接受它。爲什麼? http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – manzapanza

+0

我如何處理頁面刷新,以防止我的服務的loseof狀態? –