Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Session

An abstraction layer to securely store and manage platform credentials.

The Session is a singleton, so you may access the authentication state at any time, in any context, getting its current instance. It is also an observable, so it can be watched for changes:

import { Observer } from 'bitcapital-core-sdk';

// Gets the current Session instance
const session = bitcapital.session();

// Shows the current user instance, if any
console.log(session.current);

// Prepare a new session observer (typescript notation)
const observer: Observer = {
update(event: string, data: User) {
if(event === Session.EVENT_SESSION_CHANGED) {
console.log('User instance has changed in Session', { user: data });
}
}
};

// Start listening to Session changes, such as credentials
// expiration or a refreshed access token.
session.subscribe(observer);

// ...

// Eventually, you can also stop listening to its changes
session.unsubscribe(observer);

Hierarchy

  • Session

Index

Constructors

constructor

Properties

Private Optional _fetchPromise

_fetchPromise: Promise<User>

Private _interceptors

_interceptors: HttpInterceptor[] = []

Optional current

current: User

oauthWebService

oauthWebService: OAuthWebService

observable

observable: Observable

options

storage

storage: StorageUtil

userWebService

userWebService: UserWebService

Static EVENT_SESSION_CHANGED

EVENT_SESSION_CHANGED: string = "SESSION_CHANGED"

Static Protected instance

instance: Session

Methods

clientCredentials

  • clientCredentials(): Promise<User>
  • Perform a "client_credentials" authentication using the OAuth 2.0 server and registers it in current Session.

    Returns Promise<User>

destroy

  • destroy(): Promise<User>
  • Destroy the Session and clears the storage.

    Returns Promise<User>

Protected fetch

  • fetch(): Promise<User>
  • Fetch the currently stored Session from local storage.

    Returns Promise<User>

interceptors

  • interceptors(): HttpInterceptor[]
  • Get the Session interceptors for authorized calls and auto Session destruction.

    Returns HttpInterceptor[]

onFetch

  • onFetch(): Promise<User>
  • Returns a promise to await fetching completion.

    Returns Promise<User>

password

  • Perform a "password" authentication using the OAuth 2.0 server and registers it in current Session.

    Parameters

    Returns Promise<User>

refreshToken

  • Performs a "refresh_token" authentication using the OAuth 2.0 server and registers it in current session. This method is automatically called on requests that return 401

    Parameters

    Returns Promise<User>

register

  • register(user: User, options?: object): Promise<User>
  • Register a new User in session, notifying all observers.

    Parameters

    • user: User

      The User instance.

    • Default value options: object = { notify: true }

      The operation options.

      • notify: boolean

    Returns Promise<User>

reload

  • reload(): Promise<User>
  • Reload the current User using the remote server.

    Returns Promise<User>

subscribe

  • subscribe(observable: Observer): void
  • Subscribe for updates.

    Parameters

    • observable: Observer

      The instace to be notified.

    Returns void

unsubscribe

  • unsubscribe(observable: Observer): void
  • Unsubscribe from updates.

    Parameters

    • observable: Observer

      The instance to be removed from listeners.

    Returns void

Static getInstance

  • Get the Session singleton instance.

    Returns Session

Static initialize

Generated using TypeDoc