重定向用户登录后, [英] Redirect after user has logged in

查看:222
本文介绍了重定向用户登录后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新角,而现在我只是想把我的所有路由设置和工作,我想。

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 pages (/settings for this example) the app should check if there is a user already logged in. If there is continue as usual. Otherwise the user should go to the login page (/login).

我想什么:
用户在成功登录后,他们应该去他们最初试图去( /设置

我的问题:
是否有一个角的方式来记住用户试图去?

My question: Is there an "Angular way" to remember where the user was trying to go to?

相关code:

app.js

  .when('/settings', {
      templateUrl: '/views/auth/settings.html',
      controller: 'SettingsCtrl',
      resolve: {
        currentUser: function($q, $location, Auth) {
          var deferred = $q.defer();

          var noUser = function() {
            //remember where the user was trying to go
            $location.path("/login")
          };

          Auth.checkLogin(function() {
            if (Auth.currentUser()) {
              deferred.resolve(Auth.currentUser());
            } else {
              deferred.reject(noUser());
            }
          });

          return deferred.promise;
        }
      }
    })

login.js

login.js

  $scope.submit = function() {
    if(!$scope.logInForm.$invalid) {
      Auth.login($scope.login, $scope.password, $scope.remember_me)
      //go to the page the user was trying to get to
    }
  };

很多感谢约翰·林德奎斯特为这得到了影片我到这地步。

推荐答案

首先,你不希望将用户重定向到一个登录页面。

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

在一个页面的Web应用程序的理想流程如下:

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


  1. 一个用户访问一个网站。该网站使用的静态资产回复
    在具体的路径(例如/型材/编辑)角的应用程序。

  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).

控制器(用于特定航线)使得使用$ HTTP,$路由到一个API的调用,或其他机构(如:pre-填充在登录用户的详细信息,编辑个人资料表格通过GET帐户/ API / V1 /用户/配置文件)

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)

如果/当客户端从API接收到一个401,显示模式来
登录,并重放API调用。

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

该API调用成功(在这种情况下,用户可以查看pre填充编辑个人资料帐户的形式。)

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

你怎么能做到#3?答案是$ HTTP 响应拦截

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

有关的全球错误处理,认证或任何形式的目的
  接收到的响应的同步或异步preprocessing,它是
  希望能够前拦截HTTP请求的响应
  他们被移交给应用程序code发起这些
  要求。在响应拦截利用承诺的API
  满足这种需要同时支持同步和异步preprocessing。

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?

这里有一个例子:<一href=\"http://witoldsz.github.com/angular-http-auth/\">http://witoldsz.github.com/angular-http-auth/

这个例子是基于这篇文章:

The example is based on this article:

<一个href=\"http://www.espeo.pl/2012/02/26/authentication-in-angularjs-application\">http://www.espeo.pl/2012/02/26/authentication-in-angularjs-application

祝你好运和快乐Angularing!

Good luck and happy Angularing!

这篇关于重定向用户登录后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆