signIn method

  1. @override
Future<AuthResult> signIn()
override

애플 로그인 실행

Implementation

@override
Future<AuthResult> signIn() async {
  try {
    // 플랫폼 지원 확인
    if (!await isAvailable()) {
      return ErrorMapper.toFailure(
        AuthProvider.apple,
        KAuthError.fromCode(ErrorCodes.appleNotSupported),
      );
    }

    final credential = await SignInWithApple.getAppleIDCredential(
      scopes: [
        if (config.collect.email) AppleIDAuthorizationScopes.email,
        if (config.collect.fullName) AppleIDAuthorizationScopes.fullName,
      ],
    );

    // 원본 데이터 구성
    final rawData = <String, dynamic>{
      'userIdentifier': credential.userIdentifier,
      'email': credential.email,
      'givenName': credential.givenName,
      'familyName': credential.familyName,
      'authorizationCode': credential.authorizationCode,
      'identityToken': credential.identityToken,
      'state': credential.state,
    };

    return AuthResult.success(
      provider: AuthProvider.apple,
      user: KAuthUser.fromApple(rawData),
      accessToken: credential.authorizationCode,
      idToken: credential.identityToken,
      rawData: rawData,
    );
  } on SignInWithAppleAuthorizationException catch (e) {
    if (e.code == AuthorizationErrorCode.canceled) {
      return ErrorMapper.toFailure(
        AuthProvider.apple,
        KAuthError.fromCode(ErrorCodes.userCancelled),
      );
    }
    return ErrorMapper.toFailure(
      AuthProvider.apple,
      KAuthError.fromCode(
        ErrorCodes.appleSignInFailed,
        details: {'appleError': e.message},
        originalError: e,
      ),
    );
  } catch (e) {
    return ErrorMapper.handleException(
      AuthProvider.apple,
      e,
      operation: '로그인',
      errorCode: ErrorCodes.appleSignInFailed,
    );
  }
}