1

I have a simple Angular app with a login page and a home page, as mentioned in this post Redirecting to a certain route based on condition. Plain logic.

App.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
        $routeProvider
        .when('/home', {
            templateUrl: 'home/main',
            secure : true
        })
        .when('/login', {
            templateUrl: 'home/login'
        })
        .otherwise({redirectTo: '/login'});

}]);

App.run(['$location', '$rootScope', function($location, $rootScope) {
$rootScope.$on("$routeChangeStart", function (event, next, current) {

    if ( $rootScope.loggedUser == null ) {
        // no logged user, we might be going to #login

        if ( next && next.secure) {//send to login.
          $location.path( "/login" );
        } 
    }      
  });
}]);

And the controller is

App.controller('LoginCtrl', ['UserService', '$location','$rootScope' ,
                                   function(UserService, $location,$rootScope){
var self = this;

self.errorMessage='';

self.user = {username: '', password: ''};

self.login = function() {
    UserService.login(self.user)
    .then(
            function(success) {
                $location.url( "/home" );
            },

            function(error) {
                self.errorMessage = error.data.errorMsg;
            }
    );
};

App works. When i start the app ( with URL lh:/myapp/#/ or lh:/myapp/#/login ) , i get the login page which on successful authentication, redirects to home page with proper template (and the url also reflect lh:/myapp/#/home) and then usual business logic of application.

Now the issue i am getting is when i start directly with home page, it goes through the login process and finally changes URL to home, but don't shows the template of home page.

To be more specific : I start app, open browser, and type lh:/myapp/#/home , i get redirected to login (URL changes to lh:/myapp/#/login ), there i provide credentials ,click ok, auth success, i see that my URL gets changed to home , (as expected), but template for home page don;t show up. Still the login page template shown.

Surely some very basic error on my part. Thanks for helping. 

EDIT: below is my spring backend [using velocity templates with AngularJS on FE]

@Controller
@RequestMapping("/home")
public class HomeController {

@RequestMapping(value="/login")
public String getLoginPage() {
        return "template/login_template";   
}

@RequestMapping(value="/main")
public String getMainPage() {
    if(SecurityUtils.getSubject().isAuthenticated()){
        return "template/main_template";
    }
    else{
       return "template/login_template";    
    }
}

}
Community
  • 1
  • 1
Puneetsri
  • 234
  • 2
  • 7
  • Does it make a difference if you use `$location.path()` instead of `$location.url()`? – HankScorpio Apr 08 '15 at 23:04
  • You could also try adding `/` to the start of your template urls, to ensure that it's not a relative pathing issue. – HankScorpio Apr 08 '15 at 23:07
  • Thnks for quick reply. I have already tried that. It didn't help. – Puneetsri Apr 08 '15 at 23:07
  • How about the template urls, are you sure those are correct? It stands out as odd to me that there's no suffix on those filenames (e.g. `.html`) – HankScorpio Apr 08 '15 at 23:08
  • I am using spring REST on backend having a /home mapped controller, that's why the templateURL are like as shown. And as i mentioned, everything is working if i start with login or other inexisting mapping. – Puneetsri Apr 08 '15 at 23:11
  • What does `secure: true` mean? (I guess you use somehow this parameter originally on $routeChangeStart event or something.) – yazaki Apr 08 '15 at 23:29
  • Exactly. I've added that part as well in above post. – Puneetsri Apr 09 '15 at 07:42
  • At your last step when URL gets changed to home(as expected), Is your server logic `getMainPage` function really called and `SecurityUtils.getSubject().isAuthenticated()` return true as expected? – yazaki Apr 09 '15 at 12:57
  • Just verified. No the controller is not called. Only URL gets changed to /home – Puneetsri Apr 09 '15 at 18:54

1 Answers1

0
Problem identified.  It is the redirection to login_template inside getMainPage() which 
is causing problem.

My intention was not to serve html of main page if not authenticated, and so i had put
the security check (to redirect to login) inside getMainPage controller function. But 
what happening is that initial request fired, response of that request arrives (html of 
that response which is login_template html, verified in chrome tool) but in between, the 
redirect triggered (for login in App.run). Once the login is completed, the original request
is released.

Since due to check, the response of original request was HTML of login page, nothing seems
changed on screed (although it is changed from current login template to login template 
of prev request.NASTY ONE.

I have replaced content of getMainPage as mentioned below and now it works.

       @RequestMapping(value="/main")enter code here
       public String getMainPage() {
           return "template/main_template"; 
       }
   
Thanks for the help. This issue can be closed now. I will probably look into 
another approach with data loading on routing to handle template loading
from server only if server check is ok.
Puneetsri
  • 234
  • 2
  • 7