Giter Club home page Giter Club logo

ng-session's Introduction


No longer maintained.


ng-session Build Status

Session handler for AngularJS

Installation

Using bower, install with this command:

bower install --save ng-session

Then add either the dist/ng-session.js for development or the dist/ng-session.min.js for production to your application scripts.

And finally, add the ngSession module to your AngularJS application dependencies.

Usage

This module defines a session object into the root scope, so you can access the values directly with $rootScope.session from your controllers or directives or with $root.session from your templates.

Configuration

Ideally, the default URLs should do the job but you can configure the URLs during your application's config phase.

The default URLs are as follows:

{
  signOutUrl: '/api/users/sign-out',
  signInUrl: '/api/users/sign-in',
  updateUrl: '/api/session',
  cache: false // reload on each route change
}

Provider

Use the provider to change the default URLs.

angular.module('MyApp').config([
  'ngSessionProvider',

  function (ngSessionProvider) {
    ngSessionProvider.configure({
      signOutUrl: '/my/url/for/users/sign-out',
      signInUrl: '/my/url/for/users/sign-in',
      updateUrl: '/my/url/for/session/update',
      cache: 1000 * 60 * 60 // 1h (`true` means for all the app life cycle)
    });
  }
]);

Service

The ngSession service exposes various methods:

Method Arguments Description
reload config:Object Reloads the session via PUT request to the update url. The config object is optional and must be a valid AngularJS HTTP config object.
update config:Object Updates the session via GET request to the update url. The config object is optional and must be a valid AngularJS HTTP config object.
signIn data:Object, config:Object Signs a user in via POST request to the sign in url and updates the session with the user's data. The data object must contain the POST data to send to the server in order to sign the user in. The config object is optional and must be a valid AngularJS HTTP config object.
signOut data:Object, config:Object Signs a user out via POST request to the sign out url. The data object can be used to send POST data. The config object is optional and must be a valid AngularJS HTTP config object.
user prop:String Retrieves a property from the session.user object if any. If no argument is passed it will return the whole object.
hasRole prop:String | String[] Checks if the current user has any or all of the provided roles.
set prop:String, value:Mixed Sets a value into the session object. Argument prop must be a property name to assign the value to. Argument value must be the value to assign.
get prop:String Obtains a value from the session object. Argument prop must be the property name to retrieve the value from.
del prop:String Deletes a property from the session object. Argument prop must be the property name to delete.

Example usage

angular.module('MyApp').controller('MyController', [
  '$scope', 'ngSession',

  function ($scope, $session) {
    // ...

    $scope.signingIn = true;

    function onSignInSuccess(res) {
      // res.status === 200
      // Yay! user signed in!

      $session.set('happiness', 100);

      $scope.userName = $session.user('name');
      $scope.userId = $session.user('id');
    }

    function onSignInError(res) {
      // res.status === 4xx
      // Couldn't sign user in
      $session.set('happiness', -100);
    }

    function afterSignIn() {
      $scope.happiness = $session.get('happiness');
      $scope.signingIn = false;
    }

    ngSession.signIn($scope.data)
      .then(onSignInSuccess, onSignInError)
      .finally(afterSignIn);

    $scope.$on('$destroy', function () {
      $session.del('happiness');
    });
  }
]);

Documentation

To learn more please view the API Docs.

ng-session's People

Watchers

 avatar  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.