2016-03-08 26 views
2

角度驗證ng-token-auth。它沒有顯示退出按鈕,之後註冊。但它適用於登錄註冊后角度顯示退出

我不會將它重定向到任何地方。此外,isLoggedIn(), signOut()功能正常工作。

我究竟在哪裏做錯了?謝謝 !

導航欄控制器

function($rootScope, $scope, $location, $auth, currentUser) { 

    $scope.isLoggedIn = function() { 
    return ($scope.user.id) ? true : false; 
    } 

    $scope.signOut = function() { 
    currentUser.signOut(); 
    }; 

驗證控制器

$scope.submitRegistration = function() { 
    $auth.submitRegistration($scope.registrationForm) 
     .then(function(res) { 
     currentUser.set(res.data.data); 
     $scope.close(); 
     }) 
     .catch(function(res) { 
     $scope.errors = res.data.errors.full_messages.join(', '); 
     }) 
    }; 

    $scope.submitLogin = function() { 
    $auth.submitLogin($scope.loginForm) 
     .then(function(resp) { 
     currentUser.set(resp); 
     $scope.close(); 
     }) 
     .catch(function(resp) { 
     $scope.errors = "Email or Password invalid..."; 
     }); 
    }; 

導航欄HTML

<li ng-controller="AuthModalCtrl" ng-if='!isLoggedIn()'> 
     <div ng-click="openModal(signin)">Sign In</div> 
    </li> 
    <li ng-controller="AuthModalCtrl" ng-if='!isLoggedIn()'> 
     <div class="bubble-btn sign-up" ng-click="openModal('register')">Sign Up</div> 
    </li> 
    <li ng-controller="NavCtrl" ng-show='isLoggedIn()'> 
     <div ng-click="signOut()">Sign Out</div> 
    </li> 

回答

2

如果I U理解正確,用戶可以正確登錄,但您想在註冊後自動登錄。

根據ng-token-auth documentation,$ auth.submitRegistration不會自動登錄用戶,它只註冊用戶。您需要在submitRegistration的成功回調中顯式登錄用戶。

它看起來像success callback爲您提供了一個響應,其中包含您在註冊時發送的參數。你需要這樣做:

$scope.submitRegistration = function() { 
    $auth.submitRegistration($scope.registrationForm) 
     .then(function(res) { 

     //todo: use res to build out your login object 

     // You could either call the ng-token-auth function submitLogin   
     // here and provide another callback, or you could call your 
     // $scope.submitLogin and leverage that existing functionality 
     $auth.submitLogin(...submit your login params...) 

     $scope.close(); 
     }) 
     .catch(function(res) { 
     $scope.errors = res.data.errors.full_messages.join(', '); 
     }) 
    }; 
+0

很好的捕獲,我檢查它。 – 7urkm3n