AuthenticationExpander

Expands:

$applicationRoot.directory$/native/ionic/src/app/services/authentication.service.ts

if:

true
property value
element type ApplicationInstance
qualified name net.democritus.expander.ionic.applicationinstance.service.AuthenticationExpander
layer ROOT
technology COMMON
sourceType JS
phase expansion
features  

Example

Some example of the content generated by the expander:

import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
import { Platform } from '@ionic/angular';
import { BehaviorSubject } from 'rxjs';

import { BackendConnectorService } from './backend-connector.service';

@Injectable({
  providedIn: 'root'
})

export class AuthenticationService {

  authenticationState = new BehaviorSubject(false);

  constructor(
    private storage: Storage,
    private platform: Platform,
    private backendConnectorService: BackendConnectorService,
  ) {
    this.platform.ready().then(() => { this.checkToken(); });
  }

  checkToken() {
    return new Promise( (resolve, reject) => {
      this.storage.get("euRent_USER_INFO").then(response => {
        if (response) { this.authenticationState.next(true); }
        resolve(response);
      })
      .catch( error => reject(error) );
    });
  }

  login(username, password) {
    const accessToken = btoa(username + ':' + password);
    const token = 'Basic ' + accessToken;
    const user = { name: username, pass: password };

    return new Promise((resolve, reject) => {
      this.backendConnectorService.login(user).subscribe(
        response => {
          if (response.success) {
            this.storage.set("euRent_USER_INFO", {user: response.list[0], token})
            .then(() => {
              this.authenticationState.next(true);
              resolve();
            })
            .catch(() => reject());
          }
        },
        () => reject()
      );
    });
  }

  logout() {
    return this.storage.remove("euRent_USER_INFO").then(() => {
      this.authenticationState.next(false);
    });
  }

  isAuthenticated() {
    return new Promise( (resolve, reject) => {
      this.checkToken()
      .then( response => { resolve(this.authenticationState.value); })
      .catch( error => { reject(error); });
    });
  }
}