Authentication and Routing with Express/Passport, AngularJS and ensureLoggedIn not working on "/" url
This is the code I use for handling authentication. It is a hack but I didn't find a better way when I needed to code it. Also the routes defined in the system varied user to user so I couldn't define them in the normal config stage. This may help with your issue though.
$routeProvider .when("/login", { templateUrl: "/view/account/login.html", controller: Login }) .when("/forgottenpassword", { templateUrl: "/view/account/forgottenpassword.html", controller: ForgottenPassword }) .otherwise({ redirectTo: "login" });
This basically only allows access to 2 views. Once someone authenticates successfully I rebuild the routing table with the new valid views. Any invalid navigation goes to the login view.
I do this through a hack though so it might not be the best implementation angularjs wise. I do this by keeping a reference to $routeProvider
on the window object then use $routeProvider
as normal when you have a successful logon.
The original $routeProvider provided in angular also needs a public method to clear the existing routes before adding new ones.
After
var routes = {};
Add
this.ClearRoutes = function () { routes = {}; }
Example usage after successful logon
$routeProvider.ClearRoutes();$routeProvider .when("/home", { templateUrl: "/view/home.html", controller: Home }) .when("/logoff", { templateUrl: "/view/account/logoff.html", controller: Logoff }) .otherwise({ redirectTo: "home" });