Using Angular 9 service class, I'm leveraging RxJS's concatMap
to chain multiple HTTP calls. This involves passing the output of the first call as input to the second:
getUserDetails(authorisationCode: string): Observable<UserInfo> {
this.getAuthorisationTokens(authorisationCode)
.pipe(
concatMap(authorisationTokens => this.getUserInfo(authorisationTokens.access_token)))
.subscribe(
(data: UserInfo) => {
this.userInfo = data;
console.log(JSON.stringify(this.userInfo));
console.log(JSON.stringify(data));
},
(err: any) => console.log('Error getting user info details : ' + err),
() => {
console.log('Got user information: ' + this.userInfo);
}
);
return of(this.userInfo);
}
To return this.userInfo
to a caller, my initial approach was to wrap it in an Observable (return of(this.userInfo)
) and use it like this:
export class LandingComponent implements OnInit {
username: string;
userFirstName: string;
email: string;
constructor(private route: ActivatedRoute, private userService: UserDataService) {
this.authorisationCode = route.snapshot.queryParamMap.get('code');
console.log('Code was ' + this.authorisationCode);
}
ngOnInit(): void {
this.userService.getUserDetails(this.authorisationCode)
.subscribe((data: UserInfo) => {
this.userFirstName = data.given_name;
this.username = data.preferred_username;
this.email = data.email;
console.log('Got: ' + this.userFirstName + ', ' + this.username + ', ' + this.email);
});
}
}
Upon checking the browser console, I noticed that the service calls are successful in populating this.userInfo
, but only after encountering an undefined error when attempting to use it:
Code was 2ffa40f9-5e71-4f29-8ddd-318e8d0b99bc
main-es2015.8df8d853b157ca70b40a.js:1 Getting authorisation tokens in exchange for authorisation code 2ffa40f9-5e71-4f29-8ddd-318e8d0b99bc
main-es2015.8df8d853b157ca70b40a.js:1 Header: [object Object]
main-es2015.8df8d853b157ca70b40a.js:1 Body: grant_type=authorization_code&redirect_uri=https://xxx/landing/&client_id=xxx&code=2ffa40f9-5e71-4f29-8ddd-318e8d0b99bc&client_secret=xxx
main-es2015.8df8d853b157ca70b40a.js:1 TOKEN endpoint: https://xxx.amazoncognito.com/oauth2/token
TOKEN endpoint: https://xxx.amazoncognito.com/oauth2/token
main-es2015.8df8d853b157ca70b40a.js:1 ERROR TypeError: Cannot read property 'given_name' of undefined
...
USERINFO endpoint https://xxx.amazoncognito.com/oauth2/userInfo
main-es2015.8df8d853b157ca70b40a.js:1 USERINFO endpoint https://xxx.amazoncognito.com/oauth2/userInfo
main-es2015.8df8d853b157ca70b40a.js:1 {"sub":"4bfd88a4-5439-4ad6-a399-71b02034dfa1","email_verified":"true","given_name":"Craig","family_name":"Caulfield","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89eafbe8e0eea7eae8fce5efe0ece5edc9f1f1f1a7eae6e4">[email protected]</a>","username":"4bfd88a4-5439-4ad6-a399-xxx"}
main-es2015.8df8d853b157ca70b40a.js:1 Got user information: [object Object]
I've explored various solutions without success. Is there anything obvious that I might have overlooked?