0

I'm working on a node.js application, using Angular.js, passport and connect-ensure-login.

Only on refresh (F5), does the redirect in ensureLoggedIn seem to work and I get to the login page, otherwise I think the routeProvider for the one-page app just takes over for normal click and I get to localhost:3000/users still with the 500 error from '/api/users'.

How can I get connect-ensure-login (ensureLoggedIn) working for both direct path and click-through?

app.get('/users', ensureLoggedIn('/login'))

For APIs, I'm using

app.get('/api/users', 
    passport.authenticate('bearer'),
    ...

routeProvider snippet for the application:

.config(function($routeProvider, $locationProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'views/home.html'
            })
            ...

References:

https://www.npmjs.org/package/connect-ensure-login

http://passportjs.org/

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
bcm
  • 5,470
  • 10
  • 59
  • 92

1 Answers1

1

You may want to go about solving this issue by making use of the $httpProvider.interceptors.

In your case you could catch the 500 error coming from /api/users (though probably better to return a 401 or 403) and have an interceptor kick in and take the user to /login.

Here's the official doc:

http://docs.angularjs.org/api/ng/service/$http#interceptors

Here's a more specific example:

http://blog.thesparktree.com/post/75952317665/angularjs-interceptors-globally-handle-401-and-other

And here's a similar answer.

https://stackoverflow.com/a/18764801/115015

Community
  • 1
  • 1
brianmarco
  • 343
  • 1
  • 8
  • Looks like I should just chuck connect-ensure-login... ? – bcm Apr 21 '14 at 03:38
  • I guess it would depend on what you're ultimately after. For my app I keep it around as I need it to cover the case for full page reloads for parts of my app which don't make use of Angular routing. If your app is completely governed by Angular routing then you may want to just stick with your server side returning HTTP error codes that your routing + interceptors can handle. – brianmarco Apr 21 '14 at 04:17
  • Thanks Brian, eventually I just went for catching the error codes and using the http interceptor. – bcm Apr 21 '14 at 04:58