对于我的生活,我似乎无法让这个功能发挥作用。
我的用户身份验证流程 --> 用户注册 --。用户确认电子邮件地址 --> 设置 MFA --> 重定向到主页。
这是我的 AWS 身份验证服务:
import { Injectable } from '@angular/core';
import { AmplifyService } from 'aws-amplify-angular';
import { Observable } from 'rxjs';
import { Router } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class AwsAuthService {
awsConfirm: any;
code: any;
awsUserName: string;
currentUser: Observable<any>;
signedIn: any;
currentUserSnapshot: any;
forgotPasswordUsername: string;
totpCode: string;
userForTotp: any;
constructor(private amplifyService: AmplifyService, private router: Router) {
}
signUpNewUser(userDetails: UserDetails): void{
console.log('Sign up new user');
this.amplifyService.auth().signUp(userDetails)
.then(data => {
console.log(data);
this.awsConfirm = data;
if (this.awsConfirm) {
console.log('confirm the signup');
const username = this.awsConfirm.user.username;
this.storeUserName(username);
this.router.navigate(['pages/auth/confirm']);
}
})
.catch(err => console.log(err));
}
confirmSignUp(code): void{
this.amplifyService.auth().confirmSignUp(this.awsUserName, code, { forceAliasCreation: true})
.then(data => {
console.log(data);
this.router.navigate(['pages/auth/login']);
})
.catch(err => console.log(err));
}
async signOut(): Promise<void> {
try {
await this.amplifyService.auth().signOut({global: true});
} catch (error) {
console.log(error);
}
}
storeUserName(username: string): void {
this.awsUserName = username;
}
isUserLoggedIn(): boolean {
this.amplifyService.authStateChange$.subscribe(authState => {
this.signedIn = authState;
if (!authState.user) {
console.log('user does not exist');
return false;
} else {
console.log(authState);
this.userForTotp = authState.user;
console.log('Yay user exists');
return true;
}
});
return false;
}
logInUser(username: string, password: string): void {
const makeRequest = async () => {
try {
await this.amplifyService.auth().signIn(username, password);
console.log('Signin sucess');
} catch (err) {
console.log(err);
}
};
makeRequest();
this.isUserLoggedIn();
this.router.navigate(['pages/auth/totp']);
}
async changePassword(oldPassword: string, newPassword: string): Promise<void> {
try {
this.amplifyService.auth().changePassword(this.currentUserSnapshot, oldPassword, newPassword );
} catch (error) {
console.log(error);
}
}
async forgotPassword (username): Promise<void> {
try {
console.log('reaching out to forgot password service');
await this.amplifyService.auth().forgotPassword(username);
this.forgotPasswordUsername = username;
} catch (error) {
console.log('error');
}
}
async forgotPasswordSubmit(code, newPassword): Promise<void> {
try {
await this.amplifyService.auth().forgotPasswordSubmit(this.forgotPasswordUsername, code, newPassword);
} catch (error) {
console.log(`Username does not exisit: ${error}`);
}
}
generateTOTP(): void {
this.amplifyService.auth().setupTOTP(this.userForTotp)
.then(code => {
console.log(code);
this.totpCode = code;
})
.catch(err => {
console.log(err);
});
}
signInWithTOTP(challengeAnswer): void {
this.amplifyService.auth().verifyTotpToken(this.currentUser, challengeAnswer)
.then(() => {
this.amplifyService.auth().setPreferredMFA(this.currentUser, 'TOTP');
})
.catch(err => console.log(err));
}
}
export interface UserDetails {
username: string;
password: string;
attributes?: {
email?: string;
phone_number?: string;
};
}
我可以有效地注册和登录,但由于某些奇怪的原因,当我尝试生成 setupTOTP 函数时出现以下错误:
TypeError:无法在 AuthClass.push../node_modules/@aws-amplify/auth/lib/Auth.js.AuthClass 的新 ZoneAwarePromise (zone.js:891) 处读取未定义的属性“associateSoftwareToken”。 setupTOTP (Auth.js:688) at AwsAuthService.push../src/@fuse/services/aws-auth.service.ts.AwsAuthService.generateTOTP (aws-auth.service.ts:126) 在 TotpComponent.push.. /src/app/main/pages/authentication/totp/totp.component.ts.TotpComponent.ngOnInit (totp.component.ts:61) 在 checkAndUpdateDirectiveInline (core.js:9250) 在 checkAndUpdateNodeInline (core.js:10514) 在checkAndUpdateNode (core.js:10476) 在 debugCheckAndUpdateNode (core.js:11109) 在 debugCheckDirectivesFn (core.js:11069)