验证在角路线 [英] authenticating a route in angular

查看:97
本文介绍了验证在角路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一定的路线来要求验证,我使用这个模块:

https://github.com/enginous/angular-oauth

其中有$ scope.authenticate但我试图找出如何从$ routeProvider访问$范围/该功能。我看到了一个例子调用一个工厂函数,但是这不正是我想要做的。

 使用严格的;angular.module('应用',[
  ngRoute',
  angularOauth',
  googleOauth',
  app.global',
  app.home',
  app.view
])。
配置(['$ routeProvider',函数($ routeProvider){
  $ routeProvider.when('/',{
    templateUrl:意见的/ home / index.html的,
    控制器:家
  });
  $ routeProvider.when('/视图',{
    templateUrl:意见/浏览/ index.html的,
    控制器:'观',
    解析:{
      工厂:checkAuth
    }
  })否则({redirectTo:'/'});
  $ routeProvider.otherwise({redirectTo:'/'});
}]);VAR checkAuth =功能(){
};


解决方案

为解决注解是在角其他任何地方使用相同的依赖注入注解。您的验证逻辑应该生活在可测性和可重用性服务,所以注射和调用服务的方法是precisely你应该做的事情:

 解析:{
  AUTH:['的AuthenticationService',函数(的AuthenticationService){
    //验证服务注入,打电话给你的方法
    返回AuthenticationService.isAuthenticated();
  }]
}

  VAR checkAuth = ['的AuthenticationService',函数(的AuthenticationService){
  //验证服务注入,打电话给你的方法
  返回AuthenticationService.isAuthenticated();
}];angular.module('应用',[
  // ...
])。
配置(['$ routeProvider',函数($ routeProvider){
  // ...
  $ routeProvider.when('/视图',{
    templateUrl:意见/浏览/ index.html的,
    控制器:'观',
    解析:{
      AUTH:checkAuth
    }
  })
  // ...
}]);

I need certain routes to require authentication, I am using this module:

https://github.com/enginous/angular-oauth

Which has $scope.authenticate but I am trying to figure out how to access $scope/that function from the $routeProvider. I saw an example call a factory function but that isn't exactly what I am trying to do.

'use strict';

angular.module('app', [
  'ngRoute',
  'angularOauth',
  'googleOauth',
  'app.global',
  'app.home',
  'app.view'
]).
config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/', {
    templateUrl: 'views/home/index.html',
    controller: 'home'
  });
  $routeProvider.when('/view', {
    templateUrl: 'views/view/index.html',
    controller: 'view',
    resolve: {
      factory: checkAuth
    }
  }).otherwise({redirectTo: '/'});
  $routeProvider.otherwise({redirectTo: '/'});
}]);

var checkAuth = function() {
};

解决方案

The annotation for a resolve is the same dependency injection annotation used anywhere else in Angular. Your logic for authentication should live in a service for testability and reusability, so injecting and calling a method on your service is precisely what you should be doing:

resolve: {
  auth: ['AuthenticationService', function (AuthenticationService) {
    // Authentication service injected, call your method
    return AuthenticationService.isAuthenticated();
  }]
}

or

var checkAuth = ['AuthenticationService', function (AuthenticationService) {
  // Authentication service injected, call your method
  return AuthenticationService.isAuthenticated();
}];

angular.module('app', [
  // ...
]).
config(['$routeProvider', function($routeProvider) {
  // ...
  $routeProvider.when('/view', {
    templateUrl: 'views/view/index.html',
    controller: 'view',
    resolve: {
      auth: checkAuth
    }
  })
  // ...
}]);

这篇关于验证在角路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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