We have used what Beyers described before and it works well for most apps, and I use it frequently.
In our current application we are working on the premise that separation of concern should apply to route management.
Normal lifecycle:
- User goes to www.server.com
- Server sends down index.html
- Client makes request for minified assets (.js, .css., etc.)
- Angular loads -- a directive removes the loading class from the body (revealing the login section)
- The Angular LoginCtrl makes an autologin attempt. (Login and Autologin in an Angular service).
- The server returns a HTTP 401
- The login screen remains visible.
- User successfully logs in ( server gives the browser a authToken cookie; angular does not know or care)
- Angular sets some isAuthenticated variables in the BodyCtrl and LoginCtrl
- The login section receives a class of .hidden and the content recieves a class of .visible (insert ng-hide/show animations for fun)
- User starts filling out a form, but takes an obligitory, 30 minute phone call from relative.
- Server has expired his session 10 minutes ago
- User finishes and submits form but the server return unauthorized (401)
- http-auth-interceptor intercepts the 401 from the server, caches the submit call and publishes a "login-required' event.
- The BodyCtrl listens and sets isAuthenticated = false and then the ng-class and ng-show/hide do there work on the login and content sections.
- User re-signs in and 'login-confirmed' event is published
- http-auth-interceptor posts cached call.
- User is happy
- (the content section can also display some public views as our rest api has some routes that are made public -- displaying the public views is handled by a simple function similar to isAuthenticated)
Angular Ctrl structure:
index.html
We could get a little more creative on how display the public views/routes but you get the idea. We only have a few public routes and they are mainly for registration, password resets, etc.
Disclaimer: I have yet to integrate with and oauth/external authentication services. Hopefully this setup will still hold water.
Any critique of this process is welcome.