2016-03-08 44 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