# ng-session

> Session handler for AngularJS

Latest version **1.4.0** (published 2018-12-26) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install ng-session
pnpm add ng-session
yarn add ng-session
bun add ng-session
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.4.0 |
| Published | 2018-12-26 |
| First published | 2018-12-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 61 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Santiago G. Marín |
| Maintainers | stgogm |
| Keywords | angular, session, handler, service |

## Links

- npm: https://www.npmjs.com/package/ng-session
- Repository: https://github.com/FinalDevStudio/ng-session
- Homepage: https://github.com/FinalDevStudio/ng-session#readme
- Issues: https://github.com/FinalDevStudio/ng-session/issues
- npm.io page: https://npm.io/package/ng-session

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads

## Recent versions

- 1.4.0 (latest) — 2018-12-26
- 1.3.3 — 2018-12-26
- 1.3.2 — 2018-12-26
- 1.3.1 — 2018-12-26

## README

# ng-session [![Build Status](https://travis-ci.org/FinalDevStudio/ng-session.svg?branch=master)](https://travis-ci.org/FinalDevStudio/ng-session)

Session handler for AngularJS

## Installation

Using bower, install with this command:

```sh
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:

```javascript
{
  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.

```javascript
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

```javascript
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](docs/ng-session.md).

---
_Source: https://npm.io/package/ng-session · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
