JustMaier / angular-spa-security

Angular Security Provider for ASP.Net MVC SPA
MIT License
32 stars 10 forks source link

angular-spa-security

A handy security provider for Angular JS. Works with MVC 5's SPA template without any modifications to the backend code.

Installation

Nuget

install-package AngularJs.SPA.Security

Usage

  1. Include the angular-spa-security.js script provided by this component into your app
  2. add security as a module dependency to your app

Javascript

angular.module('app',['SignalR'])
.config(['securityProvider', function(securityProvider){
    //Modify provider settings
    securityProvider.events.login = function(security, user){
        alert('Hello '+user.userName);
    }
    securityProvider.usePopups = false;
}])
.run(['$rootScope','security', function($rootScope, security){
    $rootScope.security = security; //So you can access security variables and methods anywhere
}])
.controller('SecretPageCtrl', ['security', function(security){
    security.authenticate(); //If user isn't authenticated, will send them to the login page
}])
.controller('LoginCtrl', ['security','$scope', function(security, $scope){
    security.redirectAuthenticated('/'); //If the user is already authenticated, send them to the homepage

    $scope.login = function(user){
        security.login(user).then(function(user){
            //Success
            //Automatically sends them back to the page they were trying to access or the home page
        }, function(errorData){
            //Error
        });
    }
}])

HTML List External Login Buttons

<h4>Or Login With</h4>
<button class="btn btn-default" ng-repeat="login in security.externalLogins" ng-bind="login.name" ng-click="security.loginWithExternal(login)"></button>

Provider Options

Security Variables

Security Methods

Demo

Modified SPA demo

Notes