diff --git a/rog/backend.py b/rog/backend.py index b70c44d..ac89070 100755 --- a/rog/backend.py +++ b/rog/backend.py @@ -42,15 +42,24 @@ class EmailOrUsernameModelBackend(ModelBackend): kwargs = {'username': username} try: user = CustomUser.objects.get(**kwargs) - if check_password(password, user.password): + logger.info(f"User found in database: {username}") + + # パスワード検証の詳細ログ + password_valid = check_password(password, user.password) + logger.debug(f"Password validation for {username}: {password_valid}") + + if password_valid: logger.info(f"User authenticated successfully: {username}") return user else: logger.warning(f"Password mismatch for user: {username}") + logger.debug(f"Provided password length: {len(password) if password else 0}") except CustomUser.DoesNotExist: logger.warning(f"User does not exist: {username}") except Exception as e: logger.error(f"Authentication error for {username}: {str(e)}") + import traceback + logger.error(f"Authentication traceback: {traceback.format_exc()}") return None def get_user(self, user_id): diff --git a/rog/views.py b/rog/views.py index 2968841..b890578 100755 --- a/rog/views.py +++ b/rog/views.py @@ -906,16 +906,37 @@ class LoginAPI(generics.GenericAPIView): serializer_class = LoginUserSerializer def post(self, request, *args, **kwargs): - logger.info(f"Login attempt for identifier: {request.data.get('identifier', 'identifier not provided')}") - logger.debug(f"Request data: {request.data}") + # より詳細なログ情報を収集 + request_data = request.data + identifier = request_data.get('identifier', 'not provided') + email = request_data.get('email', 'not provided') + has_password = bool(request_data.get('password')) + + logger.info(f"Login attempt - identifier: {identifier}, email: {email}, has_password: {has_password}") + logger.debug(f"Full request data keys: {list(request_data.keys())}") logger.debug(f"Request headers: {dict(request.headers)}") - # フロントエンドの 'identifier' フィールドを 'email' にマッピング - data = request.data.copy() + # フロントエンドのフィールドマッピングを拡張 + data = request_data.copy() + + # identifierが提供された場合はemailにマッピング if 'identifier' in data and 'email' not in data: data['email'] = data['identifier'] + logger.info(f"Mapped identifier '{data['identifier']}' to email field") - logger.debug(f"Processed data: {data}") + # emailが直接提供されている場合はそのまま使用 + elif 'email' in data: + logger.info(f"Email directly provided: {data['email']}") + + # どちらも提供されていない場合 + else: + logger.error("Neither 'identifier' nor 'email' provided in request") + return Response({ + "error": "メールアドレスが提供されていません。", + "details": "email or identifier field is required" + }, status=status.HTTP_400_BAD_REQUEST) + + logger.debug(f"Processed data for serializer: email={data.get('email')}, has_password={bool(data.get('password'))}") serializer = self.get_serializer(data=data) try: @@ -935,18 +956,38 @@ class LoginAPI(generics.GenericAPIView): }) except serializers.ValidationError as e: - logger.error(f"Login failed for identifier {request.data.get('identifier', 'identifier not provided')}: {str(e)}") + # より詳細なエラーログ + attempted_email = data.get('email', 'unknown') + logger.error(f"Login validation failed for email '{attempted_email}': {e.detail}") logger.error(f"Serializer errors: {serializer.errors}") - error_msg = serializer.errors.get('non_field_errors', ['ログインに失敗しました。'])[0] + # ユーザーフレンドリーなエラーメッセージ + if 'non_field_errors' in serializer.errors: + error_detail = serializer.errors['non_field_errors'][0] + if 'Incorrect password' in str(error_detail): + error_msg = "パスワードが正しくありません。" + elif 'User with this email does not exist' in str(error_detail): + error_msg = "このメールアドレスのユーザーは存在しません。" + elif 'User account is disabled' in str(error_detail): + error_msg = "このアカウントは無効になっています。" + else: + error_msg = "ログインに失敗しました。メールアドレスとパスワードを確認してください。" + else: + error_msg = "ログインに失敗しました。入力内容を確認してください。" + return Response({ "error": error_msg, - "details": serializer.errors + "field_errors": serializer.errors }, status=status.HTTP_400_BAD_REQUEST) except Exception as e: - logger.error(f"Unexpected error during login for identifier {request.data.get('identifier', 'identifier not provided')}: {str(e)}") + attempted_email = data.get('email', 'unknown') + logger.error(f"Unexpected error during login for email '{attempted_email}': {str(e)}") logger.error(f"Traceback: {traceback.format_exc()}") + + return Response({ + "error": "ログイン処理中に予期しないエラーが発生しました。しばらく後に再試行してください。" + }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response({ "error": "予期せぬエラーが発生しました。",