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";
}
}
}