GithubHelp home page GithubHelp logo

angular-http-auth's Introduction

HTTP Auth for AngularJS

AngularJS service for handling HTTP authentication and request queuing.


This library allows Angular applications to connect to APIs which which expose their resources via HTTP authentication.

It does this by waiting for failed HTTP requests, and notifying subscribed scopes that authentication is required. Scopes can prompt users for credentials, which are then submitted to the auth service. When the queue of failed requests is flushed, the requests are re-tried with credentials.

Example

/**
 * Define application module with `ur.http.auth` dependency.
 */
var app = angular.module("app", ["ur.http.auth", "ngResource"]).run(function($resource) {

  /**
   * Query a resource that is protected by authentication.
   */
   var Item = $resource("https://api.myservice.com/items/:id", { id: "@id" });

  $rootScope.items = Item.query();

}).controller("LoginController", function($scope, $dialog, requestQueue, httpAuth) {

  /**
   * Set up the controller to handle the UI for the authentication credentials. The
   * `login()` method should be bound to a login button pressed by the user.
   */
  angular.extend($scope, {
    credentials: {
      username: "",
      password: ""
    },

    /**
     * After the user enters their credentials, submit them to the `httpAuth` service,
     * then flush the failed requests queue.
     */
    login: function() {
      httpAuth.basic(this.credentials);
      requestQueue.flush();
    },

    show: function() {
      /* Show login UI */
    }
  });

  /**
   * Wait for failed requests and prompt for login.
   */
  $scope.$on("requestFailed", function(event) {
    $scope.show();
  });

});

angular-http-auth's People

Contributors

jorgegamba avatar nateabele avatar nfang avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.