1.4.0 • Published 6 years ago

sanji-auth-ui v1.4.0

Weekly downloads
15
License
MIT
Repository
github
Last release
6 years ago

sanji-auth-ui

Sanji auth service is part of Sanji UI framework and also it is a angular module. It accounts for authenticate and authorize a user based on json web token.

NPM Package Quality Build Status Coverage Status Codacy Badge dependencies devdependencies semantic-release Commitizen friendly

Dependencies

Installation

Sanji auth service is based on es6 + webpack to development and embrace npm to install it.

npm install sanji-auth-ui --save

How to use

You need to include module first.

angular.module('webapp', ['sanji.auth'])

and then use auth and session as DI service. auth service accounts for identifying user and session accounts for keeping login success information.

class AppController {
  constructor($http, auth, session) {
    this.credentials = { username: '', passowrd: '' };
    this.login = (credentials) => {
      // Authenticate a user
      this.auth.login('/auth/local', credentials)
      .then((data) => {
        // Return token data
        return this.$http.get('/users/me');
      })
      .then((res) => {
        // Return authenticated user data and save in session service
        session.setUserData(res.data);
      });
    }
  }
}
AppController.$inject = ['$http', 'auth', 'session'];

Configuration

authProvider

You can define roles to acheive access control. Default includes admin, user and guest.

let app = angular.module('webapp', ['sanji.auth']);
app.config(authProvider => {
  authProvider.configure({
    roles: {
      admin: 'admin',
      guest: 'guest'
    }
  });
});

sessionProvider

You can define token http header. Default is Authorization.

let app = angular.module('webapp', ['sanji.auth']);
app.config(sessionProvider => {
  sessionProvider.configure({
    tokenHeader: 'define-your-own-token-http-header'
  });
});

You also can define token key. Default is token. The key name must match your server response data. For example: token

let app = angular.module('webapp', ['sanji.auth']);
app.config(sessionProvider => {
  sessionProvider.configure({
    tokenKey: 'token'
  });
});

By the way, you can save autheticated user data in session service,

let app = angular.module('webapp', ['sanji.auth']);
app.run(($http, session) => {
  $http.get('/users/me')
  .then(res => {
    session.setUserData(res.data);
  });
});

Contact

Author: Zack Yang © 2015

Support: if you find any problems with this library, open issue on Github

1.4.0

6 years ago

1.3.0

6 years ago

1.2.9

7 years ago

1.2.8

7 years ago

1.2.7

8 years ago

1.2.6

8 years ago

1.2.5

8 years ago

1.2.4

8 years ago

1.2.3

8 years ago

1.2.2

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago