다음을 통해 공유


자습서: 네이티브 인증 JavaScript SDK를 사용하여 Angular 단일 페이지 앱에서 암호 재설정

적용 대상: 외부 테넌트에 다음 내용이 적용되었음을 나타내는 흰색 확인 표시 기호가 있는 녹색 원입니다. 외부 테넌트(자세한 정보)

이 자습서에서는 네이티브 인증 JavaScript SDK를 사용하여 Angular 단일 페이지 앱에서 암호 재설정을 사용하도록 설정하는 방법을 알아봅니다. 암호 재설정은 암호 인증 흐름이 있는 전자 메일을 사용하는 사용자 계정에 사용할 수 있습니다.

이 자습서에서는 다음을 수행합니다.

  • Angular 앱을 업데이트하여 사용자의 암호를 재설정합니다.
  • 암호 재설정 흐름 테스트

필수 조건

암호 재설정 구성 요소 만들기

  1. Angular CLI를 사용하여 다음 명령을 실행하여 구성 요소 폴더 내에서 암호 재설정을 위한 새 구성 요소를 생성합니다.

    cd components
    ng generate component reset-password
    
  2. 암호 재설정/reset-password.component.ts 파일을 열고 해당 내용을 reset-password.component.ts 내용으로 바꿉니다.

  3. 암호 재설정/reset-password.component.html 파일을 열고 reset-password.component.html내용을 추가합니다.

    • reset-password.component.ts 다음 논리는 초기 암호 재설정 작업 후 다음 단계를 결정합니다. 결과에 따라reset-password.component.html코드 입력 양식을 표시하여 암호 재설정 프로세스를 계속합니다.

      const result = await client.resetPassword({ username: this.email });
      if (result.isCodeRequired()) {
                  this.showCode = true;
                  this.isReset = false;
                  this.showNewPassword = false;
              }
      
      • SDK의 인스턴스 메서드인 resetPassword()는 암호 재설정 흐름을 시작합니다.
      • reset-password.component.html 파일에서 다음을 수행합니다.
      <form *ngIf="showCode" (ngSubmit)="submitCode()">
          <input type="text" [(ngModel)]="code" name="code" placeholder="OTP Code" required />
          <button type="button" (click)="submitCode()" [disabled]="loading">{{ loading ? 'Verifying...' : 'Verify Code' }}</button>
          </form>
      
    • 성공적으로 호출된 후 submitCode() 결과는 다음 단계를 결정합니다. 암호가 필요한 경우 사용자가 암호 재설정 프로세스를 계속할 수 있도록 새 암호 양식이 표시됩니다.

      if (result.isPasswordRequired()) {
          this.showCode = false;
          this.showNewPassword = true;
          this.isReset = false;
          this.resetState = result.state;
      }
      

      reset-password.component.html 파일에서 다음을 수행합니다.

      <form *ngIf="showNewPassword" (ngSubmit)="submitNewPassword()">
        <input type="password" [(ngModel)]="newPassword" name="newPassword" placeholder="New Password" required />
        <button type="button" (click)="submitNewPassword()" [disabled]="loading">{{ loading ? 'Submitting...' : 'Submit New Password' }}</button>
      </form>
    
    • 암호 재설정이 완료된 직후 사용자가 로그인 흐름을 시작하도록 하려면 다음 코드 조각을 사용합니다.

      <div *ngIf="isReset">
          <p>The password has been reset, please click <a href="/sign-in">here</a> to sign in.</p>
      </div>
      
      
      
      

암호 재설정 후 자동으로 로그인(선택 사항)

새 로그인 흐름을 시작하지 않고 암호 재설정에 성공한 후 자동으로 사용자를 로그인할 수 있습니다. 이렇게 하려면 다음 코드 조각을 사용합니다. reset-password.component.ts 전체 예제를 참조하세요.

if (this.resetState instanceof ResetPasswordCompletedState) {
    const result = await this.resetState.signIn();
    
    if (result.isFailed()) {
        this.error = result.error?.errorData?.errorDescription || "An error occurred during auto sign-in";
    }
    
    if (result.isCompleted()) {
        this.userData = result.data;
        this.resetState = result.state;
        this.isReset = true;
        this.showCode = false;
        this.showNewPassword = false;
    }
}

사용자를 자동으로 로그인할 때는 reset-password.component.html 파일에서 다음 코드 스니펫을 사용하십시오.

<div *ngIf="userData && !isSignedIn">
    <p>Password reset completed, and signed in as {{ userData?.getAccount()?.username }}</p>
</div>
<div *ngIf="isReset && !userData">
    <p>Password reset completed! Signing you in automatically...</p>
</div>

라우팅 모듈 업데이트

src/app/app.routes.ts 파일을 연 다음 암호 재설정 구성 요소에 대한 경로를 추가합니다.

import { ResetPasswordComponent } from './reset-password/reset-password.component';

const routes: Routes = [
    { path: 'reset-password', component: ResetPasswordComponent },
    ...
];

암호 재설정 기능 테스트

  1. CORS 프록시 서버를 시작하려면 터미널에서 다음 명령을 실행합니다.

    npm run cors
    
  2. 애플리케이션을 시작하려면 터미널에서 다음 명령을 실행합니다.

    npm start
    
  3. 웹 브라우저를 열고 http://localhost:4200/reset-password로 이동합니다. 등록 양식이 나타납니다.

  4. 기존 사용자 계정의 암호를 재설정하려면 세부 정보를 입력하고 계속 단추를 선택한 다음 프롬프트를 따릅니다.

next.config.js에 poweredByHeader: false 설정하기

  • Next.jsx-powered-by 헤더는 기본적으로 HTTP 응답에 포함되어 애플리케이션이 Next.js의해 구동됨을 나타냅니다. 그러나 보안 또는 사용자 지정 이유로 이 헤더를 제거하거나 수정할 수 있습니다.

    const nextConfig: NextConfig = {
      poweredByHeader: false,
      /* other config options here */
    };