Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular.js - Prevent Direct Access to Partial Views

Tags:

angularjs

I want the users to not be able to view the partials directly when accessing their url in the browser. For example if my partial view is located in:

/partials/myPartial.html

I can still directly access it and see the odd markup. Is there a way to prevent it and make the partial views only available through angular? Thanks.

like image 227
Randal Cunanan Avatar asked Feb 23 '13 14:02

Randal Cunanan


2 Answers

I put a <script> at the top of every view partial that checks if the variable containing the main app Module exists, if not redirect to index.html

If in app.js the appModule is defined in this way:

var appModule = angular.module('myApp',["ngRoute","ngResource"]);`

At the top of the partial insert:

<script type="text/javascript">
  if(typeof appModule === 'undefined'){
    document.location.href="index.html";
  }
</script>
like image 61
Punkman Avatar answered Oct 22 '22 06:10

Punkman


Well, not really.

At the end of a day a browser needs to be able to access a partial to download it. If a browser can do this your users will be able to do so as well. You might eventually make it a bit harder for them to hit partials directly (for example to by configuring your server so it only responds to request with a certain header and configure $http to send this header on each XHR request).

The other possibility is to pre-load partials up-front as described here and not expose them via HTTP at all (actually this is a good practice for production deployments anyway).

Otherwise it is hard to propose a meaningful solution without knowing what is the exact problem (functionally speaking) that you are trying to solve.

like image 10
pkozlowski.opensource Avatar answered Oct 22 '22 06:10

pkozlowski.opensource