Skip to content

[3.x] Add email verification route #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Http/Controllers/Auth/VerifyEmailController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Cachet\Http\Controllers\Auth;

use Illuminate\Foundation\Auth\EmailVerificationRequest;
use Illuminate\Http\RedirectResponse;

final class VerifyEmailController
{
/**
* Mark the authenticated user's email address as verified.
*/
public function __invoke(EmailVerificationRequest $request): RedirectResponse
{
$request->fulfill();

return redirect()->intended(route('cachet.status-page', absolute: false).'?verified=1');
}
}
3 changes: 2 additions & 1 deletion src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Cachet\Concerns\CachetUser;
use Cachet\Database\Factories\UserFactory;
use Illuminate\Auth\MustVerifyEmail as MustVerifyEmailTrait;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\Factories\Factory;
Expand All @@ -23,7 +24,7 @@
class User extends Authenticatable implements CachetUser, HasLocalePreference, MustVerifyEmail
{
/** @use HasFactory<\Cachet\Database\Factories\UserFactory> */
use HasApiTokens, HasFactory, Notifiable;
use HasApiTokens, HasFactory, Notifiable, MustVerifyEmailTrait;

/**
* The attributes that are mass assignable.
Expand Down
15 changes: 15 additions & 0 deletions src/PendingRouteRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Cachet;

use Cachet\Http\Controllers\Auth\EmailVerificationPromptController;
use Cachet\Http\Controllers\Auth\VerifyEmailController;
use Cachet\Http\Controllers\HealthController;
use Cachet\Http\Controllers\RssController;
use Cachet\Http\Controllers\Setup\SetupController;
Expand Down Expand Up @@ -40,9 +42,22 @@ public function register(): void
$router->get('/health', HealthController::class)->name('health');

$router->get('/rss', RssController::class)->name('rss');

});

$this->registerEmailVerificationRoutes();
}

private function registerEmailVerificationRoutes(): void
{
Route::middleware('auth')->group(function () {
Route::get('verify-email/{id}/{hash}', VerifyEmailController::class)
->middleware(['signed', 'throttle:6,1'])
->name('verification.verify');
});
}


/**
* Handle the object's destruction.
*
Expand Down
45 changes: 45 additions & 0 deletions tests/Feature/Http/Controllers/Auth/EmailVerificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Cachet\Tests\Feature\Auth;

use Illuminate\Auth\Events\Verified;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\URL;
use Workbench\App\User;

use function Pest\Laravel\actingAs;
use function PHPUnit\Framework\assertFalse;
use function PHPUnit\Framework\assertTrue;

test('email can be verified', function () {
$user = User::factory()->unverified()->create();

Event::fake();

$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)]
);

$response = actingAs($user)->get($verificationUrl);

Event::assertDispatched(Verified::class);
assertTrue($user->fresh()->hasVerifiedEmail());
$response->assertRedirect(route('cachet.status-page', absolute: false).'?verified=1');
});

test('email is not verified with invalid hash', function () {
$user = User::factory()->unverified()->create();

$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1('wrong-email')]
);

actingAs($user)->get($verificationUrl);
assertFalse($user->fresh()->hasVerifiedEmail());
});
10 changes: 10 additions & 0 deletions workbench/database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ public function active()
];
});
}

/**
* Indicate that the model's email address should be unverified.
*/
public function unverified(): static
{
return $this->state(fn (array $attributes) => [
'email_verified_at' => null,
]);
}
}