3.13.0 • Published 9 months ago

oct-mwa-common v3.13.0

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

OCT Modern Web App Common

Libreria de servicios npm con typescript

https://www.npmjs.com/package/oct-mwa-common

Usage

//...

import { JwtModule, JWT_OPTIONS } from '@auth0/angular-jwt';

import {
  BaseErrorTrackerService,
  BaseJwtHelperService,
  BaseNotificationService,
  BaseStorageService,
  BaseAuthBackendCallerService,
  BaseLoaderService,
  TokenStoreService,
  OCT_MWA_COMMON_SERVICES,
} from 'oct-mwa-common';

//...

// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
export function jwtOptionsFactory(tokenStoreService: TokenStoreService) {
    return {
      tokenGetter: () => tokenStoreService.accessToken,
      //No se agrega accessToken
      disallowedRoutes: [
        // /api/auth
        /.*\/auth.*/,
      ],
      headerName: 'Authorization',
      authScheme: 'Bearer ',
      //Si el token esta vencido, se envia igual
      skipWhenExpired: false
    };
}

@NgModule({
  declarations: [AppComponent],
  entryComponents: [
    //...
  ],
  imports: [
    //...    
    JwtModule.forRoot({
      jwtOptionsProvider: {
        provide: JWT_OPTIONS,
        useFactory: jwtOptionsFactory,
        deps: [TokenStoreService]
      }
    }),
  ],
  providers: [
    //...    
    ...OCT_MWA_COMMON_SERVICES,
    {
      provide: BaseAuthBackendCallerService,
      useClass: MyAuthBackendCallerService,//Implementación local
      multi: false
    },
    {
      provide: BaseJwtHelperService,
      useClass: MyJwtHelperService,//Implementación local
      multi: false
    },
    {
      provide: BaseStorageService,
      useClass: MyStorageService,//Implementación local
      multi: false,
    },
    {
      provide: BaseNotificationService,
      useClass: MyNotificationService,//Implementación local
      multi: false,
    },
    {
      provide: BaseErrorTrackerService,
      useClass: MyErrorTrackerService,//Implementación local
      multi: false,
    },
    {
      provide: BaseLoaderService,
      useClass: MyLoaderService,//Implementación local
      multi: false,
    }
  ],
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  bootstrap: [AppComponent],
})
export class AppModule {}

Al hacer peticiones

this.http.post(`/api/mi-endpoint`, data, { headers })

La librería @auth0/angular-jwt se encargará de agregar el header de autenticación

Cómo obtener usuario?

Cada opción será mejor según el caso

Opcion 1: Subscription (Reactivo)

En este caso, el código reaccionará ante cada cambio (si se cierra sesión o si el usuario cambia)

import { Component, OnInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { AuthService } from 'oct-mwa-common';
import { MyJWTParserService } from './services';

@Component({
  selector: 'app-help',
  templateUrl: './help.page.html',
  styleUrls: ['./help.page.scss'],
})
export class HelpPage implements OnInit {

  text: string = 'No hay usuario';
  getAccessTokenSubscription: Subscription;

  constructor(private authService: AuthService,
              private myJWTParserService: MyJWTParserService) 
              { }

  ngOnInit() {
    this.getAccessTokenSubscription = this.authService.getAccessToken().subscribe(
      accessToken => {
        if(!!accessToken){
          const username = this.myJWTParserService.decodeAccessToken(accessToken).nameIdentifier;
          this.text = `El usuario es ${username}`;
        }
        else {
          this.text = 'No hay usuario';
        }
      }
    );
  }

  ngOnDestroy() {
    this.getAccessTokenSubscription.unsubscribe();
  }

}
<ion-header>
  <ion-toolbar>
    <ion-title>help</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>

<div>
  {{ text }}
</div>  

</ion-content>

Opcion 2: Async pipe (Reactivo)

En este caso, el código reaccionará ante cada cambio (si se cierra sesión o si el usuario cambia)

import { Component, OnInit } from '@angular/core';
import { Observable, Subscription } from 'rxjs';
import { map } from 'rxjs/operators';
import { AuthService } from 'oct-mwa-common';
import { MyJWTParserService } from './services';

@Component({
  selector: 'app-help',
  templateUrl: './help.page.html',
  styleUrls: ['./help.page.scss'],
})
export class HelpPage implements OnInit {

  parsedAccessToken$ : Observable<string | null>;

  constructor(private authService: AuthService,
              private myJWTParserService: MyJWTParserService) 
              { }

  ngOnInit() {
    this.parsedAccessToken$ = this.authService.getAccessToken()
      .pipe(
        map(rawAccessToken => {
            return this.myJWTParserService.decodeAccessToken(accessToken);
        })
      );
  }

}
<ion-header>
  <ion-toolbar>
    <ion-title>help</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>

<div *ngIf="parsedAccessToken$ | async as pat">
  El usuario es {{ pat.nameIdentifier }}
</div>  

</ion-content>

Opción 3: Propiedad authUser (No reactivo)

El código no reaccionará ante los cambios. Esta manera es para obtener el usuario una única vez

import { Component, OnInit } from '@angular/core';
import { AuthService } from 'oct-mwa-common';
import { MyJWTParserService } from './services';

@Component({
  selector: 'app-help',
  templateUrl: './help.page.html',
  styleUrls: ['./help.page.scss'],
})
export class HelpPage implements OnInit {

  parsedAccessToken: any;

  constructor(private authService: AuthService,
              private myJWTParserService: MyJWTParserService) { }

  async ngOnInit() {
    this.parsedAccessToken = this.myJWTParserService.decodeAccessToken(this.authService.accessToken);
  }

}
<ion-header>
  <ion-toolbar>
    <ion-title>help</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>

<div>
  El usuario es {{ parsedAccessToken.nameIdentifier }}
</div>  

</ion-content>

Opcion 4: Take + Promise (No reactivo)

Esta opción no se recomienda, pero está a modo de ejemplo. En todo caso se recomienda usar la opción anterior

El código no reaccionará ante los cambios. Esta manera es para obtener el usuario una única vez

import { Component, OnInit } from '@angular/core';
import { map, take } from 'rxjs/operators';
import { AuthService } from 'oct-mwa-common';
import { MyJWTParserService } from './services';

@Component({
  selector: 'app-help',
  templateUrl: './help.page.html',
  styleUrls: ['./help.page.scss'],
})
export class HelpPage implements OnInit {

  text: string = 'No hay usuario';
  parsedAccessToken: any;

  constructor(private authService: AuthService,
              private myJWTParserService: MyJWTParserService) { }

  async ngOnInit() {
    this.parsedAccessToken = await this.authService.getAccessToken()
      .pipe(
        map(rawAccessToken => {
            return this.myJWTParserService.decodeAccessToken(accessToken);
        }),
        take(1)
      ).toPromise();
  }

}
<ion-header>
  <ion-toolbar>
    <ion-title>help</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>

<div>
  El usuario es {{ parsedAccessToken.nameIdentifier }}
</div>  

</ion-content>
3.12.0

9 months ago

3.13.0

9 months ago

3.11.0

9 months ago

3.2.0

2 years ago

3.6.2

2 years ago

3.6.1

2 years ago

3.6.0

2 years ago

3.6.3

2 years ago

3.1.0

2 years ago

3.5.0

2 years ago

3.9.1

2 years ago

3.9.0

2 years ago

3.4.0

2 years ago

3.8.0

2 years ago

3.0.0

2 years ago

3.8.1

2 years ago

3.3.0

2 years ago

3.7.0

2 years ago

2.6.6

2 years ago

2.4.0

2 years ago

2.3.0

2 years ago

2.3.2

2 years ago

2.3.1

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.2.2

2 years ago

2.6.1

2 years ago

2.6.0

2 years ago

2.6.3

2 years ago

2.6.2

2 years ago

2.1.1

2 years ago

2.5.0

2 years ago

2.5.1

2 years ago

2.6.5

2 years ago

2.6.4

2 years ago

1.2.0

2 years ago

1.4.6

2 years ago

1.4.5

2 years ago

1.4.4

2 years ago

1.4.3

2 years ago

1.6.0

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

1.0.61

3 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.5.0

2 years ago

1.3.0

2 years ago

2.1.0

2 years ago

1.0.60

3 years ago

1.0.59

3 years ago

1.0.58

3 years ago

1.0.57

3 years ago

1.0.55

3 years ago

1.0.54

3 years ago

1.0.56

3 years ago

1.0.51

3 years ago

1.0.53

3 years ago

1.0.52

3 years ago

1.0.44

3 years ago

1.0.48

3 years ago

1.0.47

3 years ago

1.0.46

3 years ago

1.0.45

3 years ago

1.0.49

3 years ago

1.0.50

3 years ago

1.0.43

3 years ago

1.0.42

3 years ago

1.0.41

3 years ago

1.0.39

3 years ago

1.0.38

3 years ago

1.0.40

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.34

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.27

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.23

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago