Authentication and routing with Express / Passport, AngularJS, and LoggedIn support do not work on URL "/"

I have a MEAN application application generated by Yeoman Generator Angular Fullstack . You should only have access to the site by logging in.

Repo for providing logged in here

During logout, if I try to go to '/ products', I am redirected to '/ login' just fine. However, I have a problem redirecting users who are not logged in when the url is "/" or even "localhost: 9000" without a slash.

If I have not logged in and on the login screen and I change '/ login' to just '/' or '', I go to the 'main' in AngularJS and it is treated as registered (I 'm because it recognizes session?) and can click on links to "/ products" or "/ users".

My current .js routes are as follows:

/**
 * Main application routes
 */

'use strict';

var errors = require('./components/errors');
var auth = require('./controllers/auth');
var passport = require('passport');
var ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn;
module.exports = function(app) {

// Insert routes below
// All undefined asset or api routes should return a 404
    app.route('/:url(api|auth|components|app|bower_components|assets)/*').get(errors[404]);
    app.route('/login').get(auth.login).post(auth.loginUser);
    app.route('/logout').get(auth.logout);
    // All other routes should redirect to the index.html
    app.all('*', ensureLoggedIn('/login'));
    app.route('/*').get(function(req, res) {
        res.sendfile(app.get('appPath') + '/index.html');
    });
};

I also tried this with routes:

app.route('/*').get(function(req, res) {
    res.sendfile(app.get('appPath') + '/index.html');
});

Which seems to have the same behavior as enabling LoggedIn support in app.all.

Here is a snippet of my routing on the Angular side that uses ui-router:

.config ($stateProvider, $urlRouterProvider, $locationProvider) ->
    $httpProvider.interceptors.push('httpInterceptor')

    $urlRouterProvider
        .otherwise '/'

    $stateProvider
        .state 'main',
            url: '/'
            templateUrl: 'app/views/main/main.html'
            controller: 'MainCtrl'
        .state 'users',
            url: '/users'
            templateUrl: 'app/views/users/index.html'
            controller: 'UsersController'

, '/users'. , auth. Auth , , '/'.

login.jade , . 404.jade, ui-router.

, - . , . .

EDIT:

, , app.route('login'):

app.route('/')
    .get(function(req, res) {
        res.render('login');
    });

uri-router url main '/' '/main.

index.html Angular , . res.redirect routes.js, .

+4
1

, . , , . , , , . .

 $routeProvider
    .when("/login", { templateUrl: "/view/account/login.html", controller: Login })
    .when("/forgottenpassword", { templateUrl: "/view/account/forgottenpassword.html", controller: ForgottenPassword })
    .otherwise({ redirectTo: "login" });

. - , . .

, angularjs. , $routeProvider , $routeProvider , .

$routeProvider, angular, .

  var routes = {};

  this.ClearRoutes = function ()
  {
      routes = {};
  }

$routeProvider.ClearRoutes();

$routeProvider
    .when("/home", { templateUrl: "/view/home.html", controller: Home })
    .when("/logoff", { templateUrl: "/view/account/logoff.html", controller: Logoff })
    .otherwise({ redirectTo: "home" });
0

All Articles