Angular+RESTful Client-side Communication w/ API for Auth/(re)Routing
This has been covered in a few different questions, and in a few different tutorials, but all of the previous resources I've encountered don't quite hit the nail on the head.
In a nut-shell, I need to
- Login via POST from
http://client.foo
tohttp://api.foo/login
- Have a "logged in" GUI/component state for the user that provides a
logout
route - Be able to "update" the UI when the user logs out / logs out. This has been the most frustrating
- Secure my routes to check for authenticated-state (should they need it) and redirect the user to the login page accordingly
My issues are
- Every time I navigate to a different page, I need to make the call to
api.foo/status
to determine whether or not user is logged in. (ATM I'm using Express for routes) This causes a hiccup as Angular determines things likeng-show="user.is_authenticated"
- When I successfully login/logout, I need to refresh the page (I don't want to have to do this) in order to populate things like
{{user.first_name}}
, or in the case of logging out, empty that value out.
// Sample response from `/status` if successful
{
customer: {...},
is_authenticated: true,
authentication_timeout: 1376959033,
...
}
What I've tried
- http://witoldsz.github.io/angular-http-auth/1
- http://www.frederiknakstad.com/authentication-in-single-page-applications-with-angular-js/2
- https://github.com/mgonto/restangular (For the life of me I could not figure out how to
POST
withpost data
and notquery params
. The docs turned up nothing on the matter.
Why I feel like I'm losing my mind
- It seems as though every tutorial relies on some database (lots of Mongo, Couch, PHP+MySQL, ad infinitum) solution, and none rely purely on communication with a RESTful API to persist logged-in states. Once logged in, additional POSTs/GETs are sent with
withCredentials:true
, so that's not the issue - I cannot find ANY examples/tutorials/repos that do Angular+REST+Auth, sans a backend language.
I'm not too proud
Admittedly, I'm new to Angular, and would not be surprised if I'm approaching this in a ridiculous way; I'd be thrilled if someone suggest an alternative—even if it's soup-to-nuts.
I'm using Express
mostly because I really love Jade
and Stylus
— I'm not married to the Express
' routing and will give it up if what I want to do is only possible with Angular's routing.
Thanks in advance for any help anyone can provide. And please don't ask me to Google it, because I have about 26 pages of purple links. ;-)
1This solution relies on Angular's $httpBackend mock, and it's unclear how to make it talk to a real server.
2This was the closest, but since I have an existing API I need to authenticate with, I could not use passport's 'localStrategy', and it seemed insane to write an OAUTH service...that only I intended to use.