Reputation: 6321
Angular authentication with ng-token-auth
. Its not showing Sign Out button, after Sign Up. But it works for Sign In.
I am not redirecting it anywhere. Also, isLoggedIn(), signOut()
functions work fine.
Where exactly am i doing wrong ? Thank You !
Navbar Controller
function($rootScope, $scope, $location, $auth, currentUser) {
$scope.isLoggedIn = function() {
return ($scope.user.id) ? true : false;
}
$scope.signOut = function() {
currentUser.signOut();
};
Auth Controller
$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...";
});
};
Navbar 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>
Upvotes: 2
Views: 166
Reputation: 318
If I understand correctly, users are able to log in correctly but you want to log them in automatically after they register.
According to the ng-token-auth documentation, $auth.submitRegistration doesn't log the user in automatically, it only registers the user. You would need to explicitly log the user in in the success callback from submitRegistration.
It looks like the success callback gives you a response that has the parameters that you sent with the registration. You would need to do something like this:
$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(', ');
})
};
Upvotes: 2