tenaz3.comp
tenaz3.comp

Reputation: 512

How to redirect with $state.go

I'm trying to redirect my user to Dashboard after Third Party Login. But when success callback is fired, the application still on Login Page, nothing happened. If I refresh the browser my interceptor catch already login and change to Dashboard... My Login Controller looks like this:

ThirdParty.login(function(result){
callbackSUCCESS(result);
},function(){});


function callbackSUCCESS(result){
          AuthenticationService.login(result).then(
                            callbackServerSUCCESS(), function(reject) {
                                callbackServerERROR(reject);
                            });

    }
function callbackServerSUCCESS() {

                $scope.$apply(function() {
                    $state.go('dashboard');
                });
            }

My route in app.js

$stateProvider
    .state('dashboard', {
        url: '/dashboard',
        views: {
            '': {
                templateUrl: 'views/dashboard/dashboard.html',
                controller: 'DashboardCtrl'
            }
        }
    });

My Header Controller

.controller('HeaderCtrl', ['$scope', 'AuthenticationService', '$state',
        function($scope, AuthenticationService, $state) {

            $scope.logout = function() {
                AuthenticationService.logout().then(callbackServer(), callbackServer());
            };

            function callbackServer() {
                $state.go('login');
            }
        }
    ]);

Authentication Controller Angular Factory

 var headersConfig = {   
     'Cache-Control': 'no-cache',
     'Pragma': 'no-cache'
 };

 return {
     login: function(credentials) {
         var deferred = $q.defer();

         $http.post('/api/users/sign_in', sanitizeCredentials(credentials), {
             headers: headersConfig,
             timeout: deferred.promise
         }).then(function(result) {
             if (result.status === 200) {
                 UserSessionService.cacheSession(result.data);
                 deferred.resolve();
             } else {
                 deferred.reject();
             }
         }, function(reject) {
             UserSessionService.clean();
             deferred.reject(reject);
         });

         $timeout(function() {
             deferred.resolve();
         }, 15000);

         return deferred.promise;
     }
 };

Upvotes: 2

Views: 3170

Answers (2)

tenaz3.comp
tenaz3.comp

Reputation: 512

Get rid of both () in

then(callbackServer(), callbackServer())

Upvotes: 0

Matti Virkkunen
Matti Virkkunen

Reputation: 65166

I can't remember the exact semantics of $state.go, but usually you need to use $scope.$apply in some manner when responding to events Angular isn't aware of, to ensure a digest cycle occurs. I.e. you could try:

ThirdParty.login(function(result) {
    $scope.$apply(function() {
        $state.go('dashboard');
    });
}, function() {});

You need to get a reference to a scope from somewhere, but one shouldn't be hard to find in an Angular app.

Upvotes: 1

Related Questions