Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @santech/common with all npm packages installed. Try it out:

require("tslib/package.json"); // tslib is a peer dependency. var common = require("@santech/common")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@santech/common v7.0.0-beta.4

Common module for santech studio

@santech/common Dependency Status devDependency Status ========

@santech/common is a npm module that exports common models of the santech studio

Prerequisites

You need to have globally installed:

  • node 9.x.x
  • npm 5.x.x
  • yarn 1.x.x

Development

Install all the dependencies

yarn

Launch tests

npm test

Build the package

yarn build

Publish the package

npm publish

Require package in your project

npm i @santech/common -S

Import module

import { Authenticator } from '@santech/common';
// or
var Authenticator = require('@santech/common').Authenticator;
// or
var Authenticator = Santech.Common.Authenticator;

Examples

Angular 1

<script type="text/javascript" src="./node_modules/@santech/core/dist/umd/index.js"></script>
<script type="text/javascript" src="./node_modules/@santech/common/dist/umd/index.js"></script>
class MyCtrl {
  constructor(auth) {
    this._auth = auth;

    auth.waitForLogin
      .then((err) => {
        if (err) {
          // error during session recovery
        }

        if (auth.isLogged()) {
          // already logged in
        }
        // not logged in
      });
  }

  login() {
    this._auth.authenticate('login', 'password')
      .then(() => {
        // logged in
      })
      .catch(() => {
        // error
      });
  }
}

angular.module('santech', [])
  .constant('santech-authenticator-endpoints', {
    authenticateEndPoint: 'your/endpoint/to/gateway/authenticate';
    endPoint: 'your/endpoint/to/gateway';
    renewEndPoint: 'your/endpoint/to/gateway/renew';
  })
  .factory('santech-http', () => new Santech.Core.Http(fetch, Headers))
  .factory('santech-storage', () => new Santech.Core.TokenStorage(localStorage))
  .factory('santech-jwt', () => new Santech.Core.Jwt(atob))
  .service('santech-authenticator', ['santech-http', 'santech-storage', 'santech-jwt', 'santech-authenticator-endpoints', Santech.Common.Authenticator])
  .controller('MyCtrl', ['santech-authenticator', MyCtrl])
  .run(['santech-http', '$rootScope', function (http, $rootScope) {
    http.addResponseInterceptor(() => {
      $rootScope.$applyAsync();
    })
  }]);

Angular 2

import { Component, InjectionToken, Optional } from '@angular/core';
import { Authenticator, IAuthenticatorEndPoints } from '@santech/common';
import { Http, Jwt, TokenStorage } from '@santech/core';

export const AUTHENTICATOR_END_POINTS = new InjectionToken<IAuthenticatorEndPoints>('authenticatorEndPoints');

@Component({
  providers: [
    {
      provide: Http,
      useFactory: () => new Http(fetch, Headers),
    },
    {
      provide: Jwt,
      useFactory: () => new Jwt(atob),
    },
    {
      provide: TokenStorage,
      useFactory: () => new TokenStorage(localStorage),
    },
    {
      provide: Authenticator,
      useClass: Authenticator,
      deps: [Http, TokenStorage, Jwt, AUTHENTICATOR_END_POINTS],
    },
    {
      provide: AUTHENTICATOR_END_POINTS,
      useValue: {
        authenticateEndPoint: 'your/endpoint/to/gateway/authenticate';
        endPoint: 'your/endpoint/to/gateway';
        renewEndPoint: 'your/endpoint/to/gateway/renew';
      },
    },
  ],
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  private _auth: Authenticator;

  constructor(auth: Authenticator) {
    this._auth = auth;

    auth.waitForLogin
      .then((err) => {
        if (err) {
          // error during session recovery
        }

        if (auth.isLogged()) {
          // already logged in
        }
        // not logged in
      });
  }

  public login() {
    this._auth.authenticate('login', 'password')
      .then(() => {
        // logged in
      })
      .catch(() => {
        // error
      });
  }
}

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free