Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into private-tests
  • Loading branch information
AmonDeShir committed Dec 16, 2024
2 parents 955fb1d + 5398f65 commit 5796684
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
3 changes: 2 additions & 1 deletion app/Http/Controllers/AuthenticateSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions app/Http/Requests/Auth/AuthenticateSessionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function rules(): array
return [
"email" => ["required", "email", "max:255"],
"password" => ["required", "string", "min:8", "max:255"],
"remember" => ["nullable", "boolean"],
];
}
}
6 changes: 5 additions & 1 deletion resources/js/components/Common/Checkbox.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<script setup lang="ts">
defineProps<{ required?: boolean }>()
</script>

<template>
<div class="inline-flex items-center">
<label class="relative flex items-center rounded-full cursor-pointer">
<input
id="checkbox"
required
type="checkbox"
class="before:content[''] peer relative size-5 cursor-pointer appearance-none rounded-md border-2 border-primary/30 transition-all before:absolute before:top-2/4 before:left-2/4 before:block before:size-12 before:-translate-y-2/4 before:-translate-x-2/4 before:rounded-full before:bg-blue-gray-500 before:opacity-0 before:transition-opacity checked:border-primary checked:bg-primary checked:before:bg-primary hover:before:opacity-10"
:required="required"
>

<span class="absolute text-white transition-opacity opacity-0 pointer-events-none top-2/4 left-2/4 -translate-y-2/4 -translate-x-2/4 peer-checked:opacity-100">
Expand Down
9 changes: 9 additions & 0 deletions resources/js/components/Home/LoginForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -42,6 +43,14 @@ function submit() {
</a>
</div>

<label class="mx-2 mt-4 flex flex-row items-center gap-4">
<Checkbox />

<p class="w-fit text-sm text-gray-500">
Zapamiętaj mnie
</p>
</label>

<div>
<button
:disabled="form.processing"
Expand Down
36 changes: 33 additions & 3 deletions tests/Feature/AuthenticateSessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;

class AuthenticateSessionTest extends TestCase
Expand All @@ -14,13 +15,42 @@ class AuthenticateSessionTest extends TestCase

public function testUserCanLogin(): void
{
$user = User::factory()->create(["email" => "[email protected]", "password" => "goodPassword"]);
User::factory()->create(["email" => "[email protected]", "password" => "goodPassword"]);

$this->post("/auth/login", [
"email" => "[email protected]",
"password" => "goodPassword",
])
->assertRedirect("/");
])->assertRedirect("/");
}

public function testRememberCookieIsCreatedWhenUserLoginWithRememberMeOn(): void
{
$user = User::factory()->create(["email" => "[email protected]", "password" => "goodPassword"]);

$response = $this->post("/auth/login", [
"email" => "[email protected]",
"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" => "[email protected]", "password" => "goodPassword"]);

$response = $this->post("/auth/login", [
"email" => "[email protected]",
"password" => "goodPassword",
"remember" => false,
]);

$response->assertCookieMissing(Auth::guard()->getRecallerName());
}

public function testUserCanNotLoginWithWrongPassword(): void
Expand Down

0 comments on commit 5796684

Please sign in to comment.