title: Nest.JS and Observables
body: <p>I am new to Nest.JS and apparently don't understand how to use observables so hopefully ya'll can help.</p>
  <p>Basically I have a method that needs to:
  first: login to hashicorp vault and return a client_token via an http call.
  second: if we got a token back from vault, we then check that the request contained a certification-id, if not we have to request a new certification to be generated. Which requires the client_token from vault.</p>
  <p>The problem I am having is that when I call vault to get the client_token, it does not get returned in time for me to be able to use it to generate a new cert via a second api call.</p>
  <p><strong>What can I do in order to be able to use the client_token in the next step?</strong></p>
  <p>Here is the code for my latest attempt:</p>
  <p>Controller:</p>
  <pre><code>@Controller('user')
  export class UserController {
    constructor(private readonly userService: UserService) {}
  
    @Post('getUserCert')
    async getUserCert(@Body() loginDto: vaultLoginReqDto) {
      return this.userService.getCertificate(loginDto);
    }
  }
  </code></pre>
  <p>Controller calls the getCertificate method:</p>
  <pre><code>  getCertificate(loginDto: vaultLoginReqDto) {
      this.loginToVault(loginDto);
  
      if (this.vault_token) {
        if (loginDto.cert_id) {
          this.checkForExistingCert(loginDto);
        } else {
          this.generateNewCert(this.vault_token);
        }
      } else {
        throw new Error('User is not authorized to access Vault.');
      }
    }
  </code></pre>
  <p>The logon method:</p>
  <pre><code>  loginToVault(loginDto: vaultLoginReqDto) {
      const url = 'http://vault:8200/v1/auth/jwt/login';
      const payload: vaultLoginReqDto = {
        jwt: loginDto.jwt,
        role: loginDto.role,
      };
      try {
        this.httpService
          .post(url, payload)
          .subscribe((res: AxiosResponse&lt;vaultLoginResDto&gt;) =&gt; {
            this.vault_token = res.data.auth.client_token;
          });
      } catch (e) {
        this.throwError(e, url, 'Unable to login to vault');
      }
    }
  </code></pre>
  <p>the problem method is the generateNewCert method. It is not getting the vault_token in time.</p>
  <pre><code>  generateNewCert(vault_token: string): Observable&lt;string&gt; {
      const url = `http://127.0.0.1:8200/v1/xxxx/xxxx/issue/reader`;
      const payload = {
        common_name: 'id.xxxx.com',
      };
  
      const headers = {
        'X-Vault-Token': vault_token,
      };
  
      try {
        return this.httpService.post(url, payload, { headers: headers }).pipe(
          map((res: AxiosResponse&lt;vaultGetCertResDto&gt;) =&gt; {
            return res.data.data.certificate;
          }),
        );
      } catch (e) {
        this.throwError(e, url);
      }
    }
  </code></pre>
  <p>I appreciate the help!</p>