0

On server side i have a next method, which check if token found in database:

def method(token) 
   if (Database.find(token).length == 0)
     not_found()
   else
     success()
end

url as token/:token, for example token/123

I have a state for it:

   $stateProvider.state('token', {
     url: '/token/:token',
     templateUrl: 'success'
   }).state('404', {
      url: '/404',
      templateUrl: 'notfound'
   });

But i do not know, how to in ui router check token, i need some like this

$http.post('/token', {token: $stateParams.token}).success(function(){  
     //ok continue and load `success` template
}).error(function(){
    $state.go('404'); //when error
});

Is it possible with ui router?

5
  • I think you can handle it via resolve method in ui-router, So return false promise based on your condition. Commented Nov 14, 2014 at 13:01
  • How to use it for in my case? Commented Nov 14, 2014 at 13:06
  • Can you please explain more in question about what kind of your token is it used for authentication or something else. what is the purpose of checking the token before routing. Commented Nov 14, 2014 at 13:13
  • token is only number. It might be used for activate account, for example if token exist, i should display info that account activated, otherwise (when token not valid or not found), i should redirect to 404, now it -> display success page and check if exist and then if token not valid -> 404, but i want do it before display success page. Commented Nov 14, 2014 at 14:01
  • Ok, that what I did in my case. Commented Nov 14, 2014 at 14:07

1 Answer 1

1

I also had the same situation, This is what I did

.when('/beacon/:beacon_id',{
        templateUrl : 'components/beacon/beacon.html',
        controller : 'beaconController',
        access : { requiredLogin: true },
        resolve : {
            Beacon : ['$route', 'BeaconService', function($route, BeaconService){
                var beacon = BeaconService.beacon();
                return beacon.get({'beacon_id':$route.current.params.beacon_id}, function(successResponse){
                    console.log(successResponse.result[0]);
                    return successResponse.result[0];
                }, function(errorResponse){
                    $location.path('blablabla'); // This will redirect me to 404. :P
                });
            }] 
        }
    })
.otherwise({templateUrl : '404.html'});

In controller

App.controller('beaconCtrl', ['Beacon', '$scope', function(Beacon, $scope){
    //Get Beacon into controller than get your promise.
    Beacon.$promise.then(function(data){
        $scope.beacon = data.result[0];
    });
}]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.