Validate subdomain API in Angularjs

448 views Asked by At

I have created a small angular app where I have subdomain wildcards. Every subdomain is an account. Then when someone is going to the angular app the first thing I would like is to validate whether this account exists with an API call. I have created a RestFull API call that validates certain subdomains but now I don't know where I have to validate this in angular. I would like this to be the first thing it does and if it is not valid it should be then redirected to 404 error page.

Now the first thing I am doing is

$stateProvider
.state('default', {
  abstract: true,
  templateUrl: CORE.THEME_DIR+'modules/core/views/layouts/default.html',
  resolve: {
    authenticated: function($q, $location, $auth) {
      var deferred = $q.defer();
      if (!$auth.isAuthenticated()) {
        $location.path('/auth/login');
      } else {
        deferred.resolve();
      }
    return deferred.promise;
    }
  }
})
.state('minimal', {
  abstract: true,
  url: '',
  templateUrl: CORE.THEME_DIR+'modules/core/views/layouts/minimal.html'
});

At this point what it does is checking whether the user has logged in otherwise he is redirected to the login page. Before this I would like to validate the subdomain. How can I do that?

Maybe:

app.run(function ($rootScope,$location){
  $rootScope.$on('$routeChangeStart',function(event,next,current){
    VALIDATE HERE???
  });
});

Thanks

1

There are 1 answers

2
Antony Smith On

One method would be to use a factory which can then be injected into controllers as required. Either way this shows you how you can get the host name and ultimately subdomain using $location.

.factory('subdomain', ['$location', function ($location) {
    var host = $location.host();
    if (host.indexOf('.') < 0) 
        return null;
    else
        return host.split('.')[0];
}])

I'm not too sure how the rest of your app is put together to be able to provide help on actually validating this.