Angularjs 如何在身份验证后将用户重定向到原始请求?

Angularjs 如何在身份验证后将用户重定向到原始请求?,angularjs,authentication,yeoman-generator,Angularjs,Authentication,Yeoman Generator,我使用角度全堆栈yeoman生成器作为我项目的基础: 我不知道如何将用户重定向到他们登录后最初请求的链接 我希望发生的事情的示例: 未经验证的用户请求 指示用户登录 用户已成功进行身份验证 用户将自动重定向到 我使用的是样板本地登录和样板OAuth 谢谢你的帮助 在类似的情况下,我所做的是,当我将用户重定向到登录页面时,我将用户试图访问的初始路径附加到url(作为查询参数),例如,path\u to\u login?requested\u url=/videos/video1。因此,当登录

我使用角度全堆栈yeoman生成器作为我项目的基础:

我不知道如何将用户重定向到他们登录后最初请求的链接

我希望发生的事情的示例:

  • 未经验证的用户请求
  • 指示用户登录
  • 用户已成功进行身份验证
  • 用户将自动重定向到
我使用的是样板本地登录和样板OAuth


谢谢你的帮助

在类似的情况下,我所做的是,当我将用户重定向到登录页面时,我将用户试图访问的初始路径附加到url(作为查询参数),例如,
path\u to\u login?requested\u url=/videos/video1
。因此,当登录成功完成时,我只需读取
请求的\u url
查询参数,如果存在该参数,则会将用户重定向到指定的路径

我想出来了,下面是我解决这个问题的步骤。由于某些原因,堆栈溢出没有格式化下面的最后两个代码块。我用下面的代码做了一个要点(注3:需要修改单独的文件)

  • client/app/app.js的
    .run()
    方法中的cookie中存储要返回的url

    .run(function ($rootScope, $location, Auth, $cookieStore) {
      // Redirect to login if route requires auth and you're not logged in
      $rootScope.$on('$stateChangeStart', function (event, next) {
        Auth.isLoggedInAsync(function(loggedIn) {
          if (next.authenticate && !loggedIn) {
    
            // store the requested url if not logged in
            if ($location.url() != '/login')
            {
                $cookieStore.put('returnUrl', $location.url());
            }
            $location.path('/login');
          }
        });
      });
    });
    
    function setTokenCookie(req, res) {
      if (!req.user) { 
          return res.json(404, { message: 'Something went wrong, please try again.'}); 
      }
    
      var token = signToken(req.user._id, req.user.role);
      res.cookie('token', JSON.stringify(token));
    
      // return the user to the request page (oAuth) or homepage
      if (typeof req.cookies.returnUrl != 'undefined')
      {
          res.redirect(req.cookies.returnUrl.replace(/"/g, "") || '/');
      }
      else
      {
          res.redirect('/');
      }
    }
    
    .then( function() {
          // Logged in, redirect to home
          if (typeof $cookieStore.get('returnUrl') != 'undefined' && $cookieStore.get('returnUrl') != '')
          {
              $location.path($cookieStore.get('returnUrl'));
              $cookieStore.remove('returnUrl');
          }
          else
          {
              $location.path('/');
          }
        })
    
  • 对于Oauth,检查此cookie并重定向它是否存在于
    server/auth/auth.service.js

    .run(function ($rootScope, $location, Auth, $cookieStore) {
      // Redirect to login if route requires auth and you're not logged in
      $rootScope.$on('$stateChangeStart', function (event, next) {
        Auth.isLoggedInAsync(function(loggedIn) {
          if (next.authenticate && !loggedIn) {
    
            // store the requested url if not logged in
            if ($location.url() != '/login')
            {
                $cookieStore.put('returnUrl', $location.url());
            }
            $location.path('/login');
          }
        });
      });
    });
    
    function setTokenCookie(req, res) {
      if (!req.user) { 
          return res.json(404, { message: 'Something went wrong, please try again.'}); 
      }
    
      var token = signToken(req.user._id, req.user.role);
      res.cookie('token', JSON.stringify(token));
    
      // return the user to the request page (oAuth) or homepage
      if (typeof req.cookies.returnUrl != 'undefined')
      {
          res.redirect(req.cookies.returnUrl.replace(/"/g, "") || '/');
      }
      else
      {
          res.redirect('/');
      }
    }
    
    .then( function() {
          // Logged in, redirect to home
          if (typeof $cookieStore.get('returnUrl') != 'undefined' && $cookieStore.get('returnUrl') != '')
          {
              $location.path($cookieStore.get('returnUrl'));
              $cookieStore.remove('returnUrl');
          }
          else
          {
              $location.path('/');
          }
        })
    
  • 对于本地登录,请在
    $scope.login()
    .then()
    部分中检查cookie,文件:
    client/app/account/login/login.controller.js

    .run(function ($rootScope, $location, Auth, $cookieStore) {
      // Redirect to login if route requires auth and you're not logged in
      $rootScope.$on('$stateChangeStart', function (event, next) {
        Auth.isLoggedInAsync(function(loggedIn) {
          if (next.authenticate && !loggedIn) {
    
            // store the requested url if not logged in
            if ($location.url() != '/login')
            {
                $cookieStore.put('returnUrl', $location.url());
            }
            $location.path('/login');
          }
        });
      });
    });
    
    function setTokenCookie(req, res) {
      if (!req.user) { 
          return res.json(404, { message: 'Something went wrong, please try again.'}); 
      }
    
      var token = signToken(req.user._id, req.user.role);
      res.cookie('token', JSON.stringify(token));
    
      // return the user to the request page (oAuth) or homepage
      if (typeof req.cookies.returnUrl != 'undefined')
      {
          res.redirect(req.cookies.returnUrl.replace(/"/g, "") || '/');
      }
      else
      {
          res.redirect('/');
      }
    }
    
    .then( function() {
          // Logged in, redirect to home
          if (typeof $cookieStore.get('returnUrl') != 'undefined' && $cookieStore.get('returnUrl') != '')
          {
              $location.path($cookieStore.get('returnUrl'));
              $cookieStore.remove('returnUrl');
          }
          else
          {
              $location.path('/');
          }
        })
    

  • 你的意思是说你无法理解如何实现整个过程,或者仅仅是如何访问最初请求的链接?我无法理解如何访问链接,然后在身份验证后在何处执行重定向。我确信我可以将请求的链接存储在cookie或其他东西中。谢谢gosling,我完全明白你的意思,我无法确定angular fullstack代码库的样板代码中哪里可以获取查询以及从哪里调用查询。谢谢你,我无法想象你为我节省了多少时间!!注意:在我的实现中,我需要
    $window.location.href=$cookieStore.get('returnUrl')
    而不是
    $location.path($cookieStore.get('returnUrl')(步骤3)以使重定向生效。