Skip to content

AlmogBaku/ngAuth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Angular Authorization layer

This module allows you to add authorization layer to your angular project, and to filter pages for "anonymous" and "authenticated" users.

Installation

** You can assist the example on AuthExample.js of using ngAuth with ng-facebook module **

  1. Download using one of the following options:
    1. npm: npm install ngauth
    2. bower: bower install ngAuth
    3. git
  2. Add the module to your dependencies and include its scripts
  3. Create your own authentication service by implementing the AuthBase abstract:
    angular.module('myApp', ['ngAuthBase'])
        .factory('Auth', ['$facebook', 'AuthBaseUI', '$rootScope',
            function($facebook, AuthBase, $rootScope) {
                var Auth = angular.extend(AuthBase,  {});
    
                return Auth;
            }])
        .run(['Auth', function(Auth) {}])
    ;
    *** use AuthBase dependency for regular ng-route, and AuthBaseUI for router-ui ***
  4. Implement the following methods: 2.1. setIsLoggedIn() should check if the user is logged-in: true - logged-in user false - anonymous user null - information not available yet(waiting to response)

Usage

Defining routes

  1. add anonymous: true to every route which allowed only for anonymous users
  2. add authenticated: true to every route which allowed only for anonymous users

Example:

$stateProvider
    .state('login', {
        url: '/login',
        controller: 'loginCtrl',
        anonymous: true,
        templateUrl: 'src/app/views/login.html'
    })
;

Authentication status change handler

You can attach handler for every time the authentication status is changed and ready, by listening to Auth.status:

Example:

$rootScope.$on("Auth.status", function(event, status) {
    if(status) {
        console.log("Logged In!");
    } else {
        console.log("Logged out!")
    }
});

Add login/logout methods to your auth service

It's recommended to add your login/logout method on your auth service.