1

I'm really new on Angular JS development and I'm trying to implement the following authentication module : https://github.com/witoldsz/angular-http-auth (more info at http://www.espeo.pl/2012/02/26/authentication-in-angularjs-application), to my project .

THE MODULE

The module has been thought to allow the following scenario :

  • user asks for: something.com/secured/formXyz,
  • server sends a login form,
  • user logs in, fills a long and complicated form, but they are doing it so long that theirs session expires,
  • user submits a form, but since the session is not valid anymore, login screen appears,
  • once user logs in, server can process the submitted form, **no need to re-enter everything again**.

The solution to do such a thing is :

server side behavior :

for every /resources/* call, if user is not authorized, response a 401 status. Otherwise, when user is authorized or when not a /resources/* request, send what client asked for.

client side behavior :

  • capture 401 response,
  • save the request parameters, so in the future we can reconstruct original request,
  • create and return new object representing server’s future answer (instead of returning the original failed response),
  • broadcast that login is required, so application can react, in particular login form can appear,
  • listen to login successful events, so we can gather all the saved request parameters, resend them again and trigger all the ‘future’ objects (returned previously).

MY PROJECT

My project is a basic one, which uses the $route service. When I try to add the directive which catch the events :

scripts/directives/login-directive.js

angular.module('myApp', ['http-auth-interceptor','content-mocks'])
/**
* This directive will find itself inside HTML as a class,
* It is responsible for showing/hiding login form.
*/
.directive('authDirective', function() {
return {
  restrict: 'C',
  link: function(scope, elem, attrs) {  
    
    var login = elem.find('#login');
    var main = elem.find('#main');
    
    login.hide();
    main.show();
    
    scope.$on('event:auth-loginRequired', function() {
        login.show();
        main.hide();
    });
    scope.$on('event:auth-loginConfirmed', function() {
        main.show();
        login.hide();
    });
  }
}
});

to my index.html :

<body ng-app='myApp' class='auth-directive'>
    
    <!--[if lt IE 7]>
    <p class="chromeframe">You are using an outdated browser. <a href="http://browsehappy.com/">Upgrade your browser today</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to better experience this site.</p>
    <![endif]-->
    <!--[if lt IE 9]>
    <script src="components/es5-shim/es5-shim.js"></script>
    <script src="components/json3/lib/json3.min.js"></script>
    <![endif]-->
    
    <div id="login" name="login">
        <p>login</p>
    </div>

    <div id="main" name="main">
        <p>main</p>
        <div class="container" ng-view></div>
    </div>
    
    <script src="components/angular/angular.js"></script>
    <script src="components/angular-resource/angular-resource.js"></script>
    <script src="components/angular-cookies/angular-cookies.js"></script>
    <script src="components/angular-sanitize/angular-sanitize.js"></script>
    <script src="components/angular-http-auth/angular-mocks-1.0.1.js" type="text/javascript"></script>
    <script src="components/angular-http-auth/http-auth-interceptor.js" type="text/javascript"></script>
    <script src="scripts/app.js"></script>
    <script src="scripts/controllers/main.js"></script>
    <script src="scripts/directives/login-directive.js"></script>
    <script src="scripts/mocks/content-mocks.js" type="text/javascript"></script>

</body>

The directive ngView doesn't work anymore. Nothing appears on console log. Is there any incompatibility between the two directives ?

1 Answer 1

2

By the look of your login-directive files it seems that you are (improperly) reinitialising you Angular app by passing the dependencies array as a second parameter to your angular.module call:

angular.module('myApp', ['http-auth-interceptor','content-mocks'])

Dependencies should be defines only once, when app is initialised (probably in app.js), and when you want to reference your application in another file you should call the angular.module without the second parameter:

angular.module('myApp')
   .directive('authDirective', function() { ...
Sign up to request clarification or add additional context in comments.

1 Comment

It actually works :). From now, I'll remember to define this dependencies only once, at the initialization. Thanks a lot Stewie!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.