Angular Cognito 问题:无法在 authorization.service.ts:29 读取 null 的属性'user'



为了理解目的,我正在尝试实现AWS Cognito和API网关的POC。根据视频,我在authorization.service.ts中添加了自己的UserPoolId和ClientId,并成功地在localhost:4200上为该应用程序提供了服务。但是,我无法在AWS控制台的用户池中注册帐户。

视频解释和源代码参考:

  • 视频署名:https://www.youtube.com/watch?v=ROwjNYlxMAs&t=741s&ab_channel=T转盘
  • 源代码:https://github.com/kousekt/angularcognitotest

参考源代码中的相关组件ts文件:

  • 寄存器组件.ts
import { Component } from '@angular/core';
import {NgForm} from "@angular/forms";
import {AuthorizationService} from "../shared/authorization.service";
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 = "";

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) => {        
this.confirmCode = true;
},
(err) => {
console.log(err);
this.error = "Registration Error has occurred";
}
);
}
validateAuthCode(form: NgForm) {
const code = form.value.code;

this.auth.confirmAuthCode(code).subscribe(
(data) => {
//this._router.navigateByUrl('/');
this.codeWasConfirmed = true;
this.confirmCode = false;
},
(err) => {
console.log(err);
this.error = "Confirm Authorization Error has occurred";
});
}
}
  • authorization.service.ts
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 => {
userPool.signUp(email, password, attributeList, null, (err, result) => {
if (err) {
console.log("signUp error", err);
observer.error(err);
}
this.cognitoUser = result.user;
console.log("signUp success", result);
observer.next(result);
observer.complete();
});
});
}
confirmAuthCode(code) {
const user = {
Username : this.cognitoUser.username,
Pool : userPool
};
return Observable.create(observer => {
const cognitoUser = new CognitoUser(user);
cognitoUser.confirmRegistration(code, true, function(err, result) {
if (err) {
console.log(err);
observer.error(err);
}
console.log("confirmAuthCode() success", 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 => {
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;
}
}

注册时的控制台错误日志

您的错误并没有停止流,这就是为什么您的代码尝试执行下一行。要解决这个问题,只需在此处添加退货

if (err) {
console.log("signUp error", err);
observer.error(err);
return;
}

相关内容

最新更新