Redirect after user has logged in

て烟熏妆下的殇ゞ 提交于 2019-12-02 21:50:54

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!