0

对于我的生活,我似乎无法让这个功能发挥作用。

我的用户身份验证流程 --> 用户注册 --。用户确认电子邮件地址 --> 设置 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)

4

1 回答 1

0

我终于让它使用以下代码。我之前传递了一个空对象。

import { Injectable } from '@angular/core';
import { AmplifyService } from 'aws-amplify-angular';
import {  Observable } from 'rxjs';
import { Router } from '@angular/router';
import { MatSnackBar, MatSnackBarHorizontalPosition,
  MatSnackBarVerticalPosition, } from '@angular/material';


@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;
  horizontalPosition: MatSnackBarHorizontalPosition = 'center';
  verticalPosition: MatSnackBarVerticalPosition = 'top';

  constructor(private amplifyService: AmplifyService,
    public snackBar: MatSnackBar, 
    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');
        this.snackBar.open('Please confirm your email address', 'close', {
          duration: 2000,
          horizontalPosition: this.horizontalPosition,
          verticalPosition: this.verticalPosition
        });
        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.snackBar.open('Sign Up success, please log in', 'close', {
        duration: 2000,
        horizontalPosition: this.horizontalPosition,
        verticalPosition: this.verticalPosition
      });
      this.router.navigate(['pages/auth/login']);

     })
     .catch(err => console.log(err));
   }

  async  signOut(): Promise<void>  {
    try {
      await  this.amplifyService.auth().signOut({global: true});
      await  this.snackBar.open('You have been signed out', 'close', {
        duration: 2000,
        horizontalPosition: this.horizontalPosition,
        verticalPosition: this.verticalPosition
      });
    } catch (error) {
      console.log(error);
    }
   }
   storeUserName(username: string): void {
     this.awsUserName = username;
   }

   firstTimeIsUserLoggedIn(): 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;
        this.awsUserName = authState.user.username;
        console.log('Yay user exists');
        this.generateTOTP(this.userForTotp);
        return true;
      }
    });
    return false;
   }
   // MFA v2
   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;
        this.awsUserName = authState.user.username;
        console.log('Yay user exists');
        return true;
      }
    });
    return false;
   }

   firstTimeLogInUser(username: string, password: string): void  {
    this.awsUserName = username;
    const makeRequest = async () => {
      try {
        await this.amplifyService.auth().signIn(username, password);
        console.log('Signin sucess');
      } catch (err) {
        console.log(err);
      }
    };
    makeRequest();
    this.snackBar.open('Please authenticate with MFA', 'close', {
      duration: 2000,
      horizontalPosition: this.horizontalPosition,
      verticalPosition: this.verticalPosition
    });
    this.router.navigate(['pages/auth/totp']);

   }
   // Login V2
   logInUser(username: string, password: string): void  {
    this.awsUserName = username;
    const makeRequest = async () => {
      try {
        await this.amplifyService.auth().signIn(username, password);
        console.log('Signin sucess');
      } catch (err) {
        console.log(err);
      }
    };
    makeRequest();
    this.snackBar.open('Please authenticate with MFA', 'close', {
      duration: 2000,
      horizontalPosition: this.horizontalPosition,
      verticalPosition: this.verticalPosition
    });
    this.router.navigate(['pages/auth/mfa']);

   }
  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(user): string {
    this.amplifyService.auth().setupTOTP(user)
    .then(code => {
      console.log(code);
      this.totpCode = code;
      this.snackBar.open('Token has been generated', 'close', {
        duration: 2000,
        horizontalPosition: this.horizontalPosition,
        verticalPosition: this.verticalPosition
      });
      return this.totpCode;
    })
    .catch(err => {

      console.log(err);
    });
    return this.totpCode;
  }
  signInWithTOTP(challengeAnswer): void {
    console.log(challengeAnswer);
    console.log(this.userForTotp);
    this.amplifyService.auth().verifyTotpToken(this.userForTotp, challengeAnswer)
    .then((data) => {
      console.log(data);
      this.amplifyService.auth().setPreferredMFA(this.userForTotp, 'TOTP');
      this.signedIn = this.amplifyService.auth().currentAuthenticatedUser();
      console.log('yay -soiemthing');
      if (data) {
      // sign in success, query params?
      this.snackBar.open('Yay you have been signed in', 'close', {
        duration: 2000,
        horizontalPosition: this.horizontalPosition,
        verticalPosition: this.verticalPosition
      });
      this.router.navigate(['/']);
      }
    })
    .catch(err => console.log(err));

  }

}
export interface UserDetails {
  username: string;
  password: string;
  attributes?: {
    email?: string;
  phone_number?: string;
  };
} 
于 2018-10-22T13:33:04.960 回答