From 73ba089e46e9277ee6300e52bf28c7ced7d72af8 Mon Sep 17 00:00:00 2001 From: myrmidex Date: Sat, 9 Aug 2025 00:52:14 +0200 Subject: [PATCH] Fix auth error message --- .../Controllers/Api/V1/OnboardingController.php | 15 ++++++++++++++- backend/app/Services/Auth/LemmyAuthService.php | 9 +++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/backend/app/Http/Controllers/Api/V1/OnboardingController.php b/backend/app/Http/Controllers/Api/V1/OnboardingController.php index a1c3b1b..d77057b 100644 --- a/backend/app/Http/Controllers/Api/V1/OnboardingController.php +++ b/backend/app/Http/Controllers/Api/V1/OnboardingController.php @@ -131,8 +131,21 @@ public function createPlatform(Request $request): JsonResponse 'Platform account created successfully.' ); + } catch (\App\Exceptions\PlatformAuthException $e) { + // Handle authentication-specific errors with cleaner messages + return $this->sendError('Invalid username or password. Please check your credentials and try again.', [], 422); } catch (\Exception $e) { - return $this->sendError('Failed to create platform account: ' . $e->getMessage(), [], 422); + // Handle other errors (network, instance not found, etc.) + $message = 'Unable to connect to the Lemmy instance. Please check the URL and try again.'; + + // If it's a network/connection issue, provide a more specific message + if (str_contains(strtolower($e->getMessage()), 'connection') || + str_contains(strtolower($e->getMessage()), 'network') || + str_contains(strtolower($e->getMessage()), 'timeout')) { + $message = 'Connection failed. Please check the instance URL and your internet connection.'; + } + + return $this->sendError($message, [], 422); } } diff --git a/backend/app/Services/Auth/LemmyAuthService.php b/backend/app/Services/Auth/LemmyAuthService.php index 6c0afda..b3b4fe1 100644 --- a/backend/app/Services/Auth/LemmyAuthService.php +++ b/backend/app/Services/Auth/LemmyAuthService.php @@ -51,7 +51,8 @@ public function authenticate(string $instanceUrl, string $username, string $pass $token = $api->login($username, $password); if (!$token) { - throw new PlatformAuthException(PlatformEnum::LEMMY, 'Login failed for user: ' . $username); + // Throw a clean exception that will be caught and handled by the controller + throw new PlatformAuthException(PlatformEnum::LEMMY, 'Invalid credentials'); } // Get user info with the token @@ -67,8 +68,12 @@ public function authenticate(string $instanceUrl, string $username, string $pass ] ] ]; + } catch (PlatformAuthException $e) { + // Re-throw PlatformAuthExceptions as-is to avoid nesting + throw $e; } catch (Exception $e) { - throw new PlatformAuthException(PlatformEnum::LEMMY, 'Authentication failed: ' . $e->getMessage()); + // For other exceptions, throw a clean PlatformAuthException + throw new PlatformAuthException(PlatformEnum::LEMMY, 'Connection failed'); } } }