Redirect after user has logged in

后端 未结 1 419
死守一世寂寞
死守一世寂寞 2021-02-02 14:24

I\'m pretty new to Angular, and right now I\'m just trying to get all my routes set up and working as I\'d like.

Setup: When a user navigates to certain

1条回答
  •  -上瘾入骨i
    2021-02-02 14:39

    First off, you do not want to redirect the user to a login page.

    An ideal flow in a single page web app is as follows:

    1. A user visits a web site. The web site replies with the static assets for the angular app at the specific route (e.g. /profile/edit).

    2. The controller (for the given route) makes a call to an API using $http, $route, or other mechanism (e.g. to pre-fill the Edit Profile form with details from the logged in user's account via a GET to /api/v1/users/profile)

    3. If/while the client receives a 401 from the API, show a modal to login, and replay the API call.

    4. The API call succeeds (in this case, the user can view a pre-filled Edit Profile form for their account.)

    How can you do #3? The answer is $http Response Interceptors.

    For purposes of global error handling, authentication or any kind of synchronous or asynchronous preprocessing of received responses, it is desirable to be able to intercept responses for http requests before they are handed over to the application code that initiated these requests. The response interceptors leverage the promise apis to fulfil this need for both synchronous and asynchronous preprocessing.

    http://docs.angularjs.org/api/ng.$http

    Now that we know what the ideal user experience should be, how do we do it?

    There is an example here: http://witoldsz.github.com/angular-http-auth/

    The example is based on this article:

    http://www.espeo.pl/2012/02/26/authentication-in-angularjs-application

    Good luck and happy Angularing!

    0 讨论(0)
提交回复
热议问题