Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can emit/broadcast events on rootScope and subscribe to them in your login directive. </p> <p>Here is little clue <a href="http://www.espeo.pl/2012/02/26/authentication-in-angularjs-application" rel="nofollow">http://www.espeo.pl/2012/02/26/authentication-in-angularjs-application</a></p> <p>it uses interceptors to catch 401 </p> <pre><code>myapp.config(function($httpProvider) { var interceptor = ['$rootScope','$q', function(scope, $q) { function success(response) { return response; } function error(response) { var status = response.status; if (status == 401) { var deferred = $q.defer(); var req = { config: response.config, deferred: deferred } scope.requests401.push(req); scope.$broadcast('event:loginRequired'); return deferred.promise; } // otherwise return $q.reject(response); } return function(promise) { return promise.then(success, error); } }]; $httpProvider.responseInterceptors.push(interceptor); }); </code></pre> <p>And simplest directive could be this</p> <pre><code>myapp.directive("loginForm",function($http){ return function(scope,element,attrs){ element.hide(); scope.$root.$on('event:loginRequired', function(event) { element.show(); }); scope.login=function(){ // You can set controller for this directive, but I skiped that part for sake of simplicity var payload = $.param({username: scope.username, password: scope.password}); var config = { headers: {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'} } $http.post('/some/login/url', payload, config).success(function(data) { if (data === 'AUTHENTICATION_SUCCESS') { element.hide(); }else{ alert("username or password was wrong, please try again"); elements.find("form").reset(); // reset form, or you coud reset just password field } }); }; }; }); </code></pre> <p>Now, directive in action</p> <pre><code>&lt;div login-form&gt; &lt;form ng-submit="login()"&gt; &lt;label for="username"&gt;Username&lt;/label&gt; &lt;input type="text" id="username" ng-model="username"/&gt; &lt;br /&gt; &lt;label for="password"&gt;Password&lt;/label&gt; &lt;input type="password" id="password" ng-model="password" /&gt; &lt;hr/&gt; &lt;input type="submit" value="Login" /&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p>Please <strong>note</strong> code above is not tested, probably there is some misspell or something. But let me know if you have trouble implementing this, I will try to take some time and effort to make it work. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload