【问题标题】:Angular.js authentication redirect not working with Yeoman Angular-FullstackAngular.js 身份验证重定向不适用于 Yeoman Angular-Fullstack
【发布时间】:2014-12-30 22:29:03
【问题描述】:

我正在使用 ngRouter,但下面的代码存在问题:

# Add Video
.when '/videos/:action',
  templateUrl: 'app/videos/videos-edit/videos-form.html'
  controller: 'VideosEditCtrl'
  authenticate: true

# Edit video
.when '/videos/:action/:year/:month/:slug',
  templateUrl: 'app/videos/videos-edit/videos-form.html'
  controller: 'VideosEditCtrl'
  authenticate: true

如果我去上面的编辑视频页面并且我没有登录,它会重定向到我想要的登录页面。但是,如果我对添加视频页面执行相同操作,浏览器中的 url 会更改为登录页面,但仍会显示添加视频的视图。

我可以从添加视频控制器中删除 authenticate:true 并且它似乎以某种方式仍然使用编辑视频身份验证工作,但我不知道为什么或如何。

我想使用一个控制器来添加和编辑视频,因为代码非常相似。我的路由有问题吗?我应该拆分控制器吗?

【问题讨论】:

    标签: angularjs coffeescript yeoman yeoman-generator-angular


    【解决方案1】:

    在 app.js 中添加“preventDefault()”。

    //before
    .run(function ($rootScope, $location, Auth) {
    // 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) {
          $location.path('/login');
        }
      });
    });
    
    //after
    .run(function ($rootScope, $location, $state, Auth) {
    // 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) {
            event.preventDefault();
            $location.path('/login');
        }
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 2014-11-20
      • 2017-06-06
      相关资源
      最近更新 更多