diff --git a/app/Http/Controllers/RegisterUserController.php b/app/Http/Controllers/RegisterUserController.php index b8dff544..7c3dd6be 100644 --- a/app/Http/Controllers/RegisterUserController.php +++ b/app/Http/Controllers/RegisterUserController.php @@ -8,6 +8,7 @@ use App\Models\User; use Illuminate\Auth\Events\Registered; use Illuminate\Http\RedirectResponse; +use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Redirect; @@ -22,6 +23,7 @@ public function store(RegisterUserRequest $request): RedirectResponse $user->password = Hash::make($request->password); $user->save(); event(new Registered($user)); + Auth::login($user); } return Redirect::route("home"); diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 225834b1..6c18b01e 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -49,4 +49,13 @@ public function superAdmin(): static $user->syncRoles("super_admin"); }); } + + public function unverifiedUser(): static + { + return $this->state(function (array $attributes) { + return [ + "email_verified_at" => null, + ]; + }); + } } diff --git a/resources/js/Pages/Auth/Verify-Email.vue b/resources/js/Pages/Auth/Verify-Email.vue new file mode 100644 index 00000000..711ac358 --- /dev/null +++ b/resources/js/Pages/Auth/Verify-Email.vue @@ -0,0 +1,26 @@ + + + + diff --git a/routes/web.php b/routes/web.php index 93d45c3f..fb17aa25 100644 --- a/routes/web.php +++ b/routes/web.php @@ -23,8 +23,9 @@ use Illuminate\Support\Facades\Route; Route::get("/email/verify", [EmailVerifyController::class, "create"])->middleware("auth")->name("verification.notice"); -Route::get("/email/{id}/{hash}", EmailVerifyController::class)->middleware(["signed", "throttle:6,1"])->name("verification.verify"); -Route::post("email/verification-notification", [EmailVerifyController::class, "send"])->middleware("auth", "throttle:6,1")->name("verification.send"); +Route::get("/email/{id}/{hash}", EmailVerifyController::class)->middleware(["auth", "throttle:6,1"])->name("verification.verify"); +Route::post("/email/verification-notification", [EmailVerifyController::class, "send"])->middleware("auth", "throttle:3,60")->name("verification.send"); +Route::get("/auth/logout", [AuthenticateSessionController::class, "logout"])->middleware("auth")->name("logout"); Route::middleware(["guest"])->group(function (): void { Route::get("/", [ContestController::class, "index"])->name("home"); @@ -35,9 +36,8 @@ Route::post("/auth/forgot-password", [PasswordResetLinkController::class, "store"])->name("password.email"); }); -Route::middleware("auth")->group(function (): void { +Route::middleware(["auth", "verified"])->group(function (): void { Route::get("/dashboard", [ContestController::class, "create"])->name("dashboard"); - Route::get("/auth/logout", [AuthenticateSessionController::class, "logout"])->name("logout"); Route::get("/profile", [ProfileUserController::class, "create"])->name("profile"); Route::patch("/profile/password", [ProfileUserController::class, "update"])->name("profile.password.update"); }); diff --git a/tests/Feature/AuthenticateSessionTest.php b/tests/Feature/AuthenticateSessionTest.php index 7628918b..e4078dd9 100644 --- a/tests/Feature/AuthenticateSessionTest.php +++ b/tests/Feature/AuthenticateSessionTest.php @@ -58,4 +58,22 @@ public function testUserCanNotLoginWithEmptyEmailAndPassword(): void ->assertRedirect("/test") ->assertSessionHasErrors(["email" => "Pole e-mail jest wymagane.", "password" => "Pole hasło jest wymagane."]); } + + public function testUnverifiedUserCanLogin(): void + { + User::factory()->unverifiedUser()->create(["email" => "test@example.com", "password" => "goodPassword"]); + $this->from("/")->post("/auth/login", [ + "email" => "test@example.com", + "password" => "goodPassword", + ]) + ->assertRedirect("/dashboard"); + } + + public function testUnverifiedUserIsRedirectedToVerifyEmail(): void + { + $user = User::factory()->unverifiedUser()->create(); + $this->actingAs($user) + ->get("/dashboard") + ->assertRedirect("/email/verify"); + } }