본문 바로가기
AWS 개발일지/AWS 예제로 사용해보기

4. AWS Amplify 모듈 3 시작하기

by 우딬 2022. 8. 22.

1. 인증 서비스 생성

 

 Do you want to use the default authentication and security configuration? Default configuration
 Warning: you will not be able to edit these selections.
 How do you want users to be able to sign in? Username
 Do you want to configure advanced settings? No, I am done.

 

1. Default configuration을 선택

2. Username 선택

>>  여기서 Username을 선택하는 이유는 예제를 사용하기 위해 선택하지만

설명과 같이 '로그인할때 뭐로 로그인 할것인가?' 인데


Email을 선택하게 되면 회원가입할때 Username을 >  Email을 적어야된다라는 오류가 나올것이다.

그리고 이메일로 설정해서 회원가입하면 로그인할때 Email로 로그인

 

이 예제는 Username으로 선택하고 로그인할때 Username으로 로그인한다.

 

실제 서비스 할때는 Email로 로그인하도록 만들거니까~ 이부분을 Email로 하면되겠다라고 생각했어요 ㅎ

 

3. No, I am done 선택

 

2. 종속 구성 요소 설치

 

따로 오류 없음으로 패스

 

3. 플러그인 구성

... // void _configureAmplify() async {

_amplify.addPlugin(authPlugins: [AmplifyAuthCognito()]);

... // try {

이 코드는 아래와 같이 변경

_amplify.addPlugin(AmplifyAuthCognito());

//또는

_amplify.addPlugins([AmplifyAuthCognito()]);

잘 보면 addPlugin 하고 addPlugins  's'가 붙는다.

단순 예제로 사용하기 위해서는 위의 코드를 사용하고

추가 플러그인을 더 넣는 코드라고하면 아래와 같이 리스트를 만들어서 추가하면 된다.

 

3. 기능 구현

signUpWithCredentials 여기 코드는 수정해야한다.

// 2
final userAttributes = {'email': credentials.email};

// 3
final result = await Amplify.Auth.signUp(
    username: credentials.username,
    password: credentials.password,
    options: CognitoSignUpOptions(userAttributes: userAttributes));

변경 코드

// 2
Map<CognitoUserAttributeKey, String> userAttributes = {
  CognitoUserAttributeKey.email: credentials.email,
};
// 3
final result = await Amplify.Auth.signUp(
    username: credentials.username,
    password: credentials.password,
    options: CognitoSignUpOptions(userAttributes: userAttributes));

 

https://stackoverflow.com/questions/70482158/flutter-keeps-throwing-an-error-while-defining-cognitosignupoptions

 

Flutter keeps throwing an error, while defining CognitoSignUpOptions

Error: The argument type 'Map<String, String>' can't be assigned to the parameter type 'Map<CognitoUserAttributeKey, String>'. When I looked up the error there was no information about...

stackoverflow.com

 

 

 

일단은 적혀진 코드를 모두 사용하고 실행도 해본다.

여기서 발생 했던 문제를 순서대로 작성해 본다.

 

1. Username을 이메일로 해야한다.

>> 이건 처음 아무것도 모르고 만들때 Email로 만들었기 때문에 났던 오류였습니다.

 

2. Password오류

>> 1234 이런 패스워드쓰면 오류납니다.

10글자 이상 대문자 있는 등등 조건으로 패스워드 써야 오류 안납니다.

 

3. 잘 적었는데 인증확인 화면으로 안넘어가는 오류

 >> 이 오류가 진짜 이해가 안되는 오류 중 하나다.

signUpWithCredentials 여기서 나는 오류이다. 코드로 살펴보면

// 1
void signUpWithCredentials(SignUpCredentials credentials) async {
  try {
    // 2
    Map<CognitoUserAttributeKey, String> userAttributes = {
      CognitoUserAttributeKey.email: credentials.email,
    };
    // 3
    final result = await Amplify.Auth.signUp(
        username: credentials.username,
        password: credentials.password,
        options: CognitoSignUpOptions(userAttributes: userAttributes));

    // 4
    if (result.isSignUpComplete) {
      loginWithCredentials(credentials);
    } else {
      // 5
      this._credentials = credentials;

      // 6
      final state = AuthState(authFlowStatus: AuthFlowStatus.verification);
      authStateController.add(state);
    }
  
  // 7
  } on AuthError catch (authError) {
    print('Failed to sign up - ${authError.cause}');
  }
}

//4  에서 발생하는 오류로 if(result.isSignUpComplete) 여기가 true 로 나와서

로그인(loginWithCredentials)으로 넘어가버린다.

하지만 아직 인증이 완료되지 않았기 때문false가 나와  //5 //6 번 코드가 움직여야하는데

로그인화면으로 넘어가면서 인증 완료 되지 않았다는 예외를 띄운다.

 

아직 문제 원인이 파악되지 않아 //4번의 if else 부분을 지우고

어플을 사용하니 문제 없이 잘 동작 하였다.

 

4.자잘한 에러

>> 이미 등록된 유저 이름으로 재등록이 안된다.

>> 회원가입 테스트에도 많은 예외 확인이 가능할것이다.

 

 

 

댓글