angularjs - authenticating a route in angular -


i need routes require authentication, using module:

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

which has $scope.authenticate trying figure out how access $scope/that function $routeprovider. saw example call factory function isn't trying 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 resolve same dependency injection annotation used anywhere else in angular. logic authentication should live in service testability , reusability, injecting , calling method on service precisely should doing:

resolve: {   auth: ['authenticationservice', function (authenticationservice) {     // authentication service injected, call method     return authenticationservice.isauthenticated();   }] } 

or

var checkauth = ['authenticationservice', function (authenticationservice) {   // authentication service injected, call method   return authenticationservice.isauthenticated(); }];  angular.module('app', [   // ... ]). config(['$routeprovider', function($routeprovider) {   // ...   $routeprovider.when('/view', {     templateurl: 'views/view/index.html',     controller: 'view',     resolve: {       auth: checkauth     }   })   // ... }]); 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -