diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 2c40fc4..2272ef7 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -24,6 +24,7 @@ use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use Illuminate\Support\ValidatedInput; use Illuminate\View\View; +use Throwable; class AuthController extends Controller { @@ -43,7 +44,21 @@ class AuthController extends Controller $user = User::create($userAttributes); - $user->sendEmailVerificationNotification(); + try { + $user->sendEmailVerificationNotification(); + } catch (Throwable $exception) { + report($exception); + + $user->forceDelete(); + + if ($avatarPath !== null) { + Storage::disk('public')->delete($avatarPath); + } + + return response()->json([ + 'message' => __('api.auth.verification_email_failed'), + ], 503); + } $token = $user->createToken('api', ['user'])->plainTextToken; diff --git a/lang/en/api.php b/lang/en/api.php index 3a89996..337c4fd 100644 --- a/lang/en/api.php +++ b/lang/en/api.php @@ -10,6 +10,7 @@ return [ 'verified' => 'Account verified successfully.', 'verification_sent' => 'Verification email sent.', 'verification_sent_if_unverified' => 'If an unverified account exists for this address, a verification email has been sent.', + 'verification_email_failed' => 'Unable to send the verification email right now. Please try again later.', 'password_reset_link_sent' => 'If an account exists for this address, a password reset link has been sent.', 'password_reset' => 'Password reset successfully.', 'logout' => 'Logged out successfully.', diff --git a/lang/fr/api.php b/lang/fr/api.php index 109dbd2..a37c8ca 100644 --- a/lang/fr/api.php +++ b/lang/fr/api.php @@ -10,6 +10,7 @@ return [ 'verified' => 'Compte vérifié avec succès.', 'verification_sent' => 'Email de vérification envoyé.', 'verification_sent_if_unverified' => 'Si un compte non vérifié existe avec cette adresse, un email de vérification vient d’être envoyé.', + 'verification_email_failed' => "Impossible d'envoyer l'email de vérification pour le moment. Réessaie plus tard.", 'password_reset_link_sent' => 'Si un compte existe avec cette adresse, un lien de réinitialisation vient d’être envoyé.', 'password_reset' => 'Mot de passe réinitialisé avec succès.', 'logout' => 'Déconnecté avec succès.', diff --git a/tests/Feature/EmailVerificationTest.php b/tests/Feature/EmailVerificationTest.php index c16acb3..ecf98dd 100644 --- a/tests/Feature/EmailVerificationTest.php +++ b/tests/Feature/EmailVerificationTest.php @@ -56,6 +56,25 @@ it('sends a verification email when a user registers', function () { Notification::assertSentTo($user, VerifyEmail::class); }); +it('returns a temporary error when the verification email cannot be sent during registration', function () { + Notification::shouldReceive('send') + ->once() + ->andThrow(new RuntimeException('Mail transport unavailable')); + + $this->postJson('/api/auth/register', [ + 'name' => 'Leon', + 'email' => 'leon@example.com', + 'password' => 'Motsdfdepasse123*', + 'locale' => 'fr-FR', + ]) + ->assertServiceUnavailable() + ->assertJsonPath('message', __('api.auth.verification_email_failed')); + + $this->assertDatabaseMissing('users', [ + 'email' => 'leon@example.com', + ]); +}); + it('uses the custom account verification mailable', function () { $user = User::factory()->unverified()->create([ 'locale' => 'fr',