How to get a session token once for multiple requests?

Discussão em 'Angular' iniciado por Sergey, Dezembro 28, 2024.

  1. Sergey

    Sergey Guest

    There is a some service. Some component get entities via this service. This component get entities multiple times.

    When the component receives an entity again, methods getEntity() and getToken() are launched. However the session token is always the same.

    I need methods getToken to start only once. After this, the component could run method getEntity() repeatedly. Please help me do this.

    @Injectable()
    export class SomeService {
    getToken() {
    return inject(TokenService).get();
    }

    getEntity() {
    return this.getToken().pipe(
    switchMap(token => inject(EntityService).get(token))
    );
    }
    }


    PS: There are many components that use this service, so I don't want to put the token retrieval into every component.

    Continue reading...

Compartilhe esta Página