diff --git a/app/Http/Controllers/AuthenticateSessionController.php b/app/Http/Controllers/AuthenticateSessionController.php index 61cc4a9e..a4bf1da3 100644 --- a/app/Http/Controllers/AuthenticateSessionController.php +++ b/app/Http/Controllers/AuthenticateSessionController.php @@ -17,8 +17,9 @@ class AuthenticateSessionController extends Controller public function authenticate(AuthenticateSessionRequest $request): RedirectResponse { $credentials = $request->only("email", "password"); + $remember = $request->input("remember", false); - if (auth()->attempt($credentials)) { + if (auth()->attempt($credentials, $remember)) { $request->session()->regenerate(); return redirect()->route("home"); diff --git a/app/Http/Requests/Auth/AuthenticateSessionRequest.php b/app/Http/Requests/Auth/AuthenticateSessionRequest.php index af1ae8ee..53e248c3 100644 --- a/app/Http/Requests/Auth/AuthenticateSessionRequest.php +++ b/app/Http/Requests/Auth/AuthenticateSessionRequest.php @@ -18,6 +18,7 @@ public function rules(): array return [ "email" => ["required", "email", "max:255"], "password" => ["required", "string", "min:8", "max:255"], + "remember" => ["nullable", "boolean"], ]; } } diff --git a/resources/js/components/Common/Checkbox.vue b/resources/js/components/Common/Checkbox.vue index cac04da2..c853f431 100644 --- a/resources/js/components/Common/Checkbox.vue +++ b/resources/js/components/Common/Checkbox.vue @@ -1,11 +1,15 @@ + + diff --git a/resources/js/components/Home/LoginForm.vue b/resources/js/components/Home/LoginForm.vue index 5716b099..8b1b52a4 100644 --- a/resources/js/components/Home/LoginForm.vue +++ b/resources/js/components/Home/LoginForm.vue @@ -3,6 +3,7 @@ import CustomInput from '@/components/Common/CustomInput.vue' import { useForm } from '@inertiajs/vue3' import { type Errors } from '@inertiajs/core' import PasswordInput from '@/components/Common//PasswordInput.vue' +import Checkbox from '@/components/Common/Checkbox.vue' const { errors } = defineProps<{ errors: Errors }>() const form = useForm({ @@ -42,6 +43,14 @@ function submit() { + + + + + Zapamiętaj mnie + + + create(["email" => "test@example.com", "password" => "goodPassword"]); + User::factory()->create(["email" => "test@example.com", "password" => "goodPassword"]); $this->post("/auth/login", [ "email" => "test@example.com", "password" => "goodPassword", - ]) - ->assertRedirect("/"); + ])->assertRedirect("/"); + } + + public function testRememberCookieIsCreatedWhenUserLoginWithRememberMeOn(): void + { + $user = User::factory()->create(["email" => "test@example.com", "password" => "goodPassword"]); + + $response = $this->post("/auth/login", [ + "email" => "test@example.com", + "password" => "goodPassword", + "remember" => true, + ]); + + $response->assertCookie(Auth::guard()->getRecallerName(), vsprintf("%s|%s|%s", [ + $user->id, + $user->getRememberToken(), + $user->password, + ])); + } + + public function testRememberCookieIsNotCreatedWhenUserLoginWithRememberMeOff(): void + { + User::factory()->create(["email" => "test@example.com", "password" => "goodPassword"]); + + $response = $this->post("/auth/login", [ + "email" => "test@example.com", + "password" => "goodPassword", + "remember" => false, + ]); + + $response->assertCookieMissing(Auth::guard()->getRecallerName()); } public function testUserCanNotLoginWithWrongPassword(): void
+ Zapamiętaj mnie +