title: Angular Cognito issue: Cannot read property 'user' of null at authorization.service.ts:29
body: <p>I am trying to implement this POC of AWS Cognito and API Gateway for understanding purposes. According to the video, I have added my own UserPoolId and ClientId in <strong>authorization.service.ts</strong> and I managed to serve the app successfully on localhost:4200. However, I am unable to register an account on my userpool in AWS console.</p>
  <p>References for the video explanation and source code:</p>
  <ul>
  <li>Video Credits: <a href="https://www.youtube.com/watch?v=ROwjNYlxMAs&amp;t=741s&amp;ab_channel=TKousek" rel="nofollow noreferrer">https://www.youtube.com/watch?v=ROwjNYlxMAs&amp;t=741s&amp;ab_channel=TKousek</a></li>
  <li>Source Code: <a href="https://github.com/kousekt/angularcognitotest" rel="nofollow noreferrer">https://github.com/kousekt/angularcognitotest</a></li>
  </ul>
  <p>Relevant component ts file in source code for references:</p>
  <ul>
  <li>register.component.ts</li>
  </ul>
  <pre><code>import { Component } from '@angular/core';
  import {NgForm} from &quot;@angular/forms&quot;;
  import {AuthorizationService} from &quot;../shared/authorization.service&quot;;
  import { Router } from '@angular/router';
  
  // https://github.com/aws/amazon-cognito-identity-js
  // https://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-javascript-examples.html
  
  @Component({
    selector: 'app-register',
    templateUrl: './register.component.html',
    styleUrls: ['./register.component.css']
  })
  export class RegisterComponent { 
    confirmCode: boolean = false;
    codeWasConfirmed: boolean = false;
    error: string = &quot;&quot;;
    
    constructor(private auth: AuthorizationService,
                private _router: Router) { }
  
    register(form: NgForm) {
      const email = form.value.email;
      const password = form.value.password;
      this.auth.register(email, password).subscribe(
        (data) =&gt; {        
          this.confirmCode = true;
        },
        (err) =&gt; {
          console.log(err);
          this.error = &quot;Registration Error has occurred&quot;;
        }
      );
    }
  
    validateAuthCode(form: NgForm) {
      const code = form.value.code;
      
      this.auth.confirmAuthCode(code).subscribe(
        (data) =&gt; {
          //this._router.navigateByUrl('/');
          this.codeWasConfirmed = true;
          this.confirmCode = false;
        },
        (err) =&gt; {
          console.log(err);
          this.error = &quot;Confirm Authorization Error has occurred&quot;;
        });
    }
  }
  
  </code></pre>
  <ul>
  <li>authorization.service.ts</li>
  </ul>
  <pre><code>import { Injectable } from '@angular/core';
  import {AuthenticationDetails, CognitoUser, CognitoUserPool} from 'amazon-cognito-identity-js';
  import { Observable } from 'rxjs/Observable';
  
  const poolData = {
    UserPoolId: 'ap-southeast-1_4E5c8sOtF', // Your user pool id here
    ClientId: '307efnv599eldkq3odcld2j3dh' // Your client id here  
  };
  
  const userPool = new CognitoUserPool(poolData);
  
  @Injectable()
  export class AuthorizationService {
    cognitoUser: any;
  
    constructor() { }
  
    register(email, password) {
  
      const attributeList = [];
  
      return Observable.create(observer =&gt; {
        userPool.signUp(email, password, attributeList, null, (err, result) =&gt; {
          if (err) {
            console.log(&quot;signUp error&quot;, err);
            observer.error(err);
          }
  
          this.cognitoUser = result.user;
          console.log(&quot;signUp success&quot;, result);
          observer.next(result);
          observer.complete();
        });
      });
  
    }
  
    confirmAuthCode(code) {
      const user = {
        Username : this.cognitoUser.username,
        Pool : userPool
      };
      return Observable.create(observer =&gt; {
        const cognitoUser = new CognitoUser(user);
        cognitoUser.confirmRegistration(code, true, function(err, result) {
          if (err) {
            console.log(err);
            observer.error(err);
          }
          console.log(&quot;confirmAuthCode() success&quot;, result);
          observer.next(result);
          observer.complete();
        });
      });
    }
  
    signIn(email, password) { 
  
      const authenticationData = {
        Username : email,
        Password : password,
      };
      const authenticationDetails = new AuthenticationDetails(authenticationData);
  
      const userData = {
        Username : email,
        Pool : userPool
      };
      const cognitoUser = new CognitoUser(userData);
      
      return Observable.create(observer =&gt; {
  
        cognitoUser.authenticateUser(authenticationDetails, {
          onSuccess: function (result) {
            
            //console.log(result);
            observer.next(result);
            observer.complete();
          },
          onFailure: function(err) {
            console.log(err);
            observer.error(err);
          },
        });
      });
    }
  
    isLoggedIn() {    
      return userPool.getCurrentUser() != null;
    }
  
    getAuthenticatedUser() {
      // gets the current user from the local storage
      return userPool.getCurrentUser();
    }
  
    logOut() {
      this.getAuthenticatedUser().signOut();
      this.cognitoUser = null;
    }
  }
  
  </code></pre>
  <p><a href="https://i.stack.imgur.com/Drqs1.png" rel="nofollow noreferrer">Console Error log when registering</a></p>