Skip to content

Commit

Permalink
Add validation errors and messages
Browse files Browse the repository at this point in the history
Add:
 validation errors and messages
Improve:
 Controllers with requests
 Tests
  • Loading branch information
PrabuckiDominik committed Aug 14, 2024
1 parent f742250 commit d8815a6
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 75 deletions.
11 changes: 4 additions & 7 deletions app/Http/Controllers/AuthenticateSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace App\Http\Controllers;

use App\Http\Requests\Auth\AuthenticateSessionRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Validation\ValidationException;
use Inertia\Inertia;
Expand All @@ -21,12 +21,9 @@ public function create(): Response
/**
* @throws ValidationException
*/
public function authenticate(Request $request): RedirectResponse
public function authenticate(AuthenticateSessionRequest $request): RedirectResponse
{
$credentials = $request->validate([
"email" => "required|email",
"password" => "required|string",
]);
$credentials = $request->only("email", "password");

if (auth()->attempt($credentials)) {
$request->session()->regenerate();
Expand All @@ -46,6 +43,6 @@ public function logout(): RedirectResponse
request()->session()->invalidate();
request()->session()->regenerateToken();

return Redirect::route("home")->with("succes");
return Redirect::route("home")->with("success");
}
}
17 changes: 5 additions & 12 deletions app/Http/Controllers/PasswordResetLinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace App\Http\Controllers;

use App\Http\Requests\Auth\ForgotPasswordRequest;
use App\Http\Requests\Auth\ResetPasswordRequest;
use App\Models\User;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Str;
Expand All @@ -21,11 +22,9 @@ public function create(): Response
return Inertia::render("Auth/Forgot-Password");
}

public function store(Request $request): RedirectResponse
public function store(ForgotPasswordRequest $request): RedirectResponse
{
$request->validate([
"email" => "required|string|email",
]);
$request->only("email");

$status = Password::sendResetLink(
$request->only("email"),
Expand All @@ -41,14 +40,8 @@ public function resetCreate(string $token): Response
return Inertia::render("Auth/Reset-Password", ["token" => $token]);
}

public function resetStore(Request $request): RedirectResponse
public function resetStore(ResetPasswordRequest $request): RedirectResponse
{
$request->validate([
"token" => "required",
"email" => "required|email",
"password" => "required|min:8|confirmed",
]);

$status = Password::reset(
$request->only("email", "password", "password_confirmation", "token"),
function (User $user, string $password): void {
Expand Down
43 changes: 43 additions & 0 deletions app/Http/Requests/Auth/AuthenticateSessionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests\Auth;

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;

class AuthenticateSessionRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}

/**
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
"email" => "required|email|max:255",
"password" => "required|string",
];
}

public function messages(): array
{
return [
"email" => [
"required" => "The email field is required.",
"max" => "Your email is too long. It must not be greater than 255 characters.",
"unique" => "The email has already been taken.",
"email" => "Your email is invalid.",
],

"password" => [
"required" => "The password field is required.",
],
];
}
}
37 changes: 37 additions & 0 deletions app/Http/Requests/Auth/ForgotPasswordRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests\Auth;

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;

class ForgotPasswordRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}

/**
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
"email" => "required|string|email:dns|max:255",
];
}

public function messages(): array
{
return [
"email" => [
"required" => "The email field is required.",
"max" => "Your email is too long. It must not be greater than 255 characters.",
"email" => "Your email is invalid.",
],
];
}
}
39 changes: 31 additions & 8 deletions app/Http/Requests/Auth/RegisterUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,50 @@

class RegisterUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
"email" => "required|string|email|max:255|unique:" . User::class,
"name" => "required|string",
"surname" => "required|string",
"email" => "required|string|email:rfc,dns|max:255|unique:" . User::class,
"name" => "required|string|max:255",
"surname" => "required|string|max:255",
"password" => "required|string|min:8",
"school_id" => "required|integer|exists:schools,id",
];
}

public function messages(): array
{
return [
"email" => [
"required" => "The email field is required.",
"max" => "Your email is too long. It must not be greater than 255 characters.",
"unique" => "The email has already been taken.",
"email" => "Your email is invalid.",
],
"name" => [
"required" => "The name field is required",
"max" => "Your name is too long. It must not be greater than 255 characters.",
],
"surname" => [
"required" => "The surname field is required",
"max" => "Your surname is too long. It must not be greater than 255 characters.",
],
"password" => [
"required" => "The password field is required.",
"min" => "Your password is too short. It must be at least 8 characters.",
],
"school_id" => [
"required" => "The school field is required.",
"exists" => "Your school is invalid. Check it again.",
],
];
}
}
47 changes: 47 additions & 0 deletions app/Http/Requests/Auth/ResetPasswordRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests\Auth;

use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;

class ResetPasswordRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}

/**
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
"token" => "required",
"email" => "required|email|max:255",
"password" => "required|min:8|confirmed:password_confirmation",
];
}

public function messages(): array
{
return [
"token" => [
"required" => "The token field is required.",
],
"email" => [
"required" => "The email field is required.",
"max" => "Your email is too long. It must not be greater than 255 characters.",
"email" => "Your email is invalid.",
],
"password" => [
"required" => "The password field is required.",
"min" => "Your password is too short. It must be at least 8 characters.",
"confirmed" => "The password field confirmation does not match.",
],
];
}
}
13 changes: 10 additions & 3 deletions resources/js/Pages/Auth/Forgot-Password.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<script setup>
<script setup lang="ts">
import { reactive } from 'vue'
import { router } from '@inertiajs/vue3'
const { errors } = defineProps<{
errors: Record<string, string[]>
}>()
const form = reactive({
email: null,
Expand All @@ -14,8 +18,11 @@ function submit() {

<template>
<form @submit.prevent="submit">
<label for="email">Email:</label>
<input v-model="form.email" name="email" type="email">
<div>
<label for="email">Email:</label>
<input v-model="form.email" name="email" type="email">
<div v-if="errors.email">{{ errors.email }}</div>
</div>
<button type="submit">Reset?</button>
</form>
</template>
20 changes: 15 additions & 5 deletions resources/js/Pages/Auth/Login.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<script setup>
<script setup lang="ts">
import { reactive } from 'vue'
import { router } from '@inertiajs/vue3'
const { errors } = defineProps<{
errors: Record<string, string[]>
}>()
const form = reactive({
email: null,
password: null,
Expand All @@ -14,10 +18,16 @@ function submit() {

<template>
<form @submit.prevent="submit">
<label for="email">Email:</label>
<input v-model="form.email" name="email" type="email">
<label for="password">password:</label>
<input v-model="form.password" name="password" type="password">
<div>
<label for="email">Email:</label>
<input v-model="form.email" name="email" type="email">
<div v-if="errors.email">{{ errors.email }}</div>
</div>
<div>
<label for="password">password:</label>
<input v-model="form.password" name="password" type="password">
<div v-if="errors.password">{{ errors.password }}</div>
</div>
<button type="submit">Login</button>
</form>

Expand Down
41 changes: 30 additions & 11 deletions resources/js/Pages/Auth/Register.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<script setup>
<script setup lang="ts">
import { reactive } from 'vue'
import { router } from '@inertiajs/vue3'
const { errors } = defineProps<{
errors: Record<string, string[]>
}>()
const form = reactive({
name: null,
surname: null,
Expand All @@ -17,16 +21,31 @@ function submit() {

<template>
<form @submit.prevent="submit">
<label for="name">Name:</label>
<input v-model="form.name" name="name" type="text">
<label for="surname">Surname:</label>
<input v-model="form.surname" name="surname" type="text">
<label for="email">Email:</label>
<input v-model="form.email" name="email" type="email">
<label for="password">password:</label>
<input v-model="form.password" name="password" type="password">
<label for="school_id">School ID:</label>
<input v-model="form.school_id" name="school_id" type="number">
<div>
<label for="name">Name:</label>
<input v-model="form.name" name="name" type="text">
<div v-if="errors.name">{{ errors.name }}</div>
</div>
<div>
<label for="surname">Surname:</label>
<input v-model="form.surname" name="surname" type="text">
<div v-if="errors.surname">{{ errors.surname }}</div>
</div>
<div>
<label for="email">Email:</label>
<input v-model="form.email" name="email" type="email">
<div v-if="errors.email">{{ errors.email }}</div>
</div>
<div>
<label for="password">password:</label>
<input v-model="form.password" name="password" type="password">
<div v-if="errors.password">{{ errors.password }}</div>
</div>
<div>
<label for="school_id">School ID:</label>
<input v-model="form.school_id" name="school_id" type="number">
<div v-if="errors.school_id">{{ errors.school_id }}</div>
</div>
<button type="submit">Register</button>
</form>
</template>
Loading

0 comments on commit d8815a6

Please sign in to comment.