Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update</strong></p> <p>Most of this is outdated now with the progression of angularFire. Check out this module for an updated version: <a href="https://github.com/firebase/angularFire-seed/blob/master/app/js/module.simpleLoginTools.js" rel="nofollow">https://github.com/firebase/angularFire-seed/blob/master/app/js/module.simpleLoginTools.js</a></p> <p>It decorates <code>ng-cloak</code> to work with simple login and provides some optimized helper methods as well as working with recent versions.</p> <p><strong>Old Post</strong></p> <p>Authenticating requires a round trip to the server. In an SPA, you don't generally reload the page during normal usage so this is really superfluous in most cases.</p> <p>However, it's not difficult to solve with a moderate understanding of Angular. In fact, I taught this exact scenario as an example for writing directives in a recent workshop. <a href="https://gist.github.com/katowulf/7328023" rel="nofollow">Here's the gist I used</a>, and <a href="https://github.com/katowulf/codecamp-nov2013/tree/step3/app" rel="nofollow">here's an example app</a> that uses the gist.</p> <p>Note that if you try to clone the GitHub repo, you need to grab that particular tag (<code>git checkout step3</code>) as the course was taught in steps.</p> <p>For compliance with SO format, here's the contents of the gist. First the directive:</p> <pre><code>angular.module('waitForAuth', []) /** * A service that returns a promise object, which is resolved once angularFireAuth * is initialized (i.e. it returns login, logout, or error) */ .service('waitForAuth', function($rootScope, $q, $timeout) { var def = $q.defer(), subs = []; subs.push($rootScope.$on('angularFireAuth:login', fn)); subs.push($rootScope.$on('angularFireAuth:logout', fn)); subs.push($rootScope.$on('angularFireAuth:error', fn)); function fn() { for(var i=0; i &lt; subs.length; i++) { subs[i](); } $timeout(function() { // force $scope.$apply to be re-run after login resolves def.resolve(); }); } return def.promise; }) /** * A directive that hides the element from view until waitForAuth resolves */ .directive('ngCloakAuth', function(waitForAuth) { return { restrict: 'A', compile: function(el) { console.log('waiting'); el.addClass('hide'); waitForAuth.then(function() { el.removeClass('hide'); }) } } }) /** * A directive that shows elements only when the given authentication state is in effect */ .directive('ngShowAuth', function($rootScope) { var loginState; return { restrict: 'A', compile: function(el, attr) { var expState = attr.ngShowAuth; function fn(newState) { loginState = newState; el.toggleClass('hide', loginState !== expState ); } fn(null); $rootScope.$on("angularFireAuth:login", function() { fn('login') }); $rootScope.$on("angularFireAuth:logout", function() { fn('logout') }); $rootScope.$on("angularFireAuth:error", function() { fn('error') }); } } }); </code></pre> <p>And some example usages:</p> <pre><code>&lt;style&gt; .hide { display: none; } &lt;/style&gt; &lt;script&gt; // include the waitForAuth module as a dependency angular.module('myApp', ['waitForAuth']) // you can use waitForAuth directly from your scripts .controller('myController', function(waitForAuth) { waitForAuth.then(function() { /* do something after auth completes */ }) }) &lt;/script&gt; &lt;!-- and you can use the directives in your views --&gt; &lt;div ng-cloak-auth&gt;Authentication has resolved.&lt;/div&gt; &lt;div ng-show-auth="login"&gt;{{user.id}} is logged in&lt;/div&gt; &lt;div ng-show-auth="logout"&gt;Logged out&lt;/div&gt; &lt;div ng-show-auth="error"&gt;An error occurred during authentication&lt;/div&gt; </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    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