I am currently using AngularJS, Django Rest Framework, and Django in my project. I want to prevent anonymous users from accessing a specific view in AngularJS. While the rest framework restricts anon user actions in the AngularJS view, it still allows access, which I want to avoid. Essentially, I need to implement login_required functionality similar to Django in my AngularJS routes.
var app = angular.module('mostAwesomeApp', ['restangular', 'ngRoute'])
app.config(function($routeProvider){
var base_dir = STATIC_URL + 'js/ng-coolest-app-ever/templates/';
$routeProvider.when('/',
{
templateUrl: base_dir + 'list.html',
controller: 'listViewCtrl'
})
.when('/edit/:pk',{
templateUrl: base_dir + 'update.html',
controller: 'editCtrl'
})
})
// Any user can access this controller/view
app.controller('listViewCtrl', function($scope){
$scope.doSomethingAwesome = function(){//doing something awesome}
})
// Only authenticated users should be able to access this controller and its view
app.controller('editCtrl', function($scope){
$scope.onlyAuthedUsersCanDoAndSee = function(){
// doing something awesome for authed peeps}
}
})
<div ng-app='mostAwesomeApp'>
// The problem lies here. If using ng-view, I cannot simply use Django's if user.auth... statements to control what an anonymous user can or cannot see
<ng-view></ng-view>
</div>
I have been considering passing the user status into a JavaScript variable and making it global for all my apps. Then, I would just check this variable for any application that requires special treatment. It works, but doesn't seem like the most elegant solution.