Skip to content

Commit

Permalink
feat: add laravel precognition
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Mar 12, 2024
1 parent d461e51 commit deb2683
Show file tree
Hide file tree
Showing 44 changed files with 3,210 additions and 464 deletions.
11 changes: 3 additions & 8 deletions app/Http/Controllers/Auth/NewPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\NewPasswordRequest;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
Expand All @@ -13,8 +14,6 @@

class NewPasswordController extends Controller
{
use PasswordValidationRules;

/**
* Display the password reset view.
*/
Expand All @@ -28,13 +27,9 @@ public function create(Request $request): View
*
* @throws \Illuminate\Validation\ValidationException
*/
public function store(Request $request): RedirectResponse
public function store(NewPasswordRequest $request): RedirectResponse
{
$request->validate([
'token' => ['required'],
'email' => ['required', 'email'],
'password' => $this->passwordRules(),
]);
$request->validated();

// Here we will attempt to reset the user's password. If it is successful we
// will update the password on an actual user model and persist it to the
Expand Down
12 changes: 3 additions & 9 deletions app/Http/Controllers/Auth/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,18 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\PasswordRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;

class PasswordController extends Controller
{
use PasswordValidationRules;

/**
* Update the user's password.
*/
public function update(Request $request): RedirectResponse
public function update(PasswordRequest $request): RedirectResponse
{
$validated = $request->validateWithBag('updatePassword', [
'current_password' => ['required', 'current_password'],
'password' => $this->passwordRules(),
]);
$validated = $request->validated();

$request->user()->update([
'password' => Hash::make($validated['password']),
Expand Down
11 changes: 10 additions & 1 deletion app/Http/Controllers/Auth/PasswordValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ trait PasswordValidationRules
*/
protected function passwordRules(): array
{
return ['required', 'string', Password::default(), 'confirmed'];
return [
'required',
'string',
app()->environment('local')
? Password::min(4)
: ($this->isPrecognitive()
? Password::default()
: Password::default()->uncompromised()),
'confirmed',
];
}
}
19 changes: 3 additions & 16 deletions app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Http\Requests\Auth\RegisteredUserRequest;
use App\Providers\RouteServiceProvider;
use App\Services\CreateAccount;
use Illuminate\Auth\Events\Registered;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

class RegisteredUserController extends Controller
{
use PasswordValidationRules;

/**
* Display the registration view.
*/
Expand All @@ -30,18 +26,9 @@ public function create(): View
*
* @throws \Illuminate\Validation\ValidationException
*/
public function store(Request $request): RedirectResponse
public function store(RegisteredUserRequest $request): RedirectResponse
{
$request->validate([
'email' => [
'required',
'string',
'email',
'max:255',
Rule::unique(User::class),
],
'password' => $this->passwordRules(),
]);
$request->validated();

$user = (new CreateAccount(
email: $request->input('email'),
Expand Down
14 changes: 10 additions & 4 deletions app/Http/Controllers/ListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Http\Requests\NameListRequest;
use App\Http\ViewModels\User\ListViewModel;
use App\Models\ListCategory;
use App\Services\CreateList;
Expand Down Expand Up @@ -35,10 +36,12 @@ public function new(): View
]);
}

public function store(Request $request): RedirectResponse
public function store(NameListRequest $request): RedirectResponse
{
$request->validated();

$list = (new CreateList(
name: $request->input('list-name'),
name: $request->input('listname'),
description: $request->input('description'),
isPublic: false,
canBeModified: true,
Expand Down Expand Up @@ -81,12 +84,15 @@ public function edit(Request $request): View
]);
}

public function update(Request $request): RedirectResponse
public function update(NameListRequest $request): RedirectResponse
{
$request->validated();

$list = $request->attributes->get('list');

$list->name = $request->input('list-name');
$list->name = $request->input('listname');
$list->description = $request->input('description');
$list->gender = $request->input('gender');
$list->list_category_id = $request->input('category');
$list->save();

Expand Down
33 changes: 33 additions & 0 deletions app/Http/Requests/Auth/NewPasswordRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Requests\Auth;

use App\Http\Controllers\Auth\PasswordValidationRules;
use Illuminate\Foundation\Http\FormRequest;

class NewPasswordRequest extends FormRequest
{
use PasswordValidationRules;

/**
* 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, \Illuminate\Contracts\Validation\Rule|array|string>
*/
public function rules(): array
{
return [
'token' => ['required'],
'email' => ['required', 'email'],
'password' => $this->passwordRules(),
];
}
}
31 changes: 31 additions & 0 deletions app/Http/Requests/Auth/PasswordRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Requests\Auth;

use App\Http\Controllers\Auth\PasswordValidationRules;
use Illuminate\Foundation\Http\FormRequest;

class PasswordRequest extends FormRequest
{
use PasswordValidationRules;

/**
* 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, \Illuminate\Contracts\Validation\Rule|array|string>
*/
public function rules(): array
{
return [
'password' => $this->passwordRules(),
];
}
}
40 changes: 40 additions & 0 deletions app/Http/Requests/Auth/RegisteredUserRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Requests\Auth;

use App\Http\Controllers\Auth\PasswordValidationRules;
use App\Models\User;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class RegisteredUserRequest extends FormRequest
{
use PasswordValidationRules;

/**
* 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, \Illuminate\Contracts\Validation\Rule|array|string>
*/
public function rules(): array
{
return [
'email' => [
'required',
'string',
'email',
'max:255',
Rule::unique(User::class),
],
'password' => $this->passwordRules(),
];
}
}
31 changes: 31 additions & 0 deletions app/Http/Requests/NameListRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class NameListRequest 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, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'listname' => 'string|required|max:255',
'description' => 'string|nullable|max:255',
'gender' => 'string|nullable|max:255',
'category' => 'int|nullable|exists:categories,id',
];
}
}
1 change: 1 addition & 0 deletions app/Http/ViewModels/User/ListViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public static function edit(NameList $list): array
'id' => $list->id,
'name' => $list->name,
'description' => $list->description,
'gender' => $list->gender,
'list_category_id' => $list->list_category_id,
'url' => [
'update' => route('list.update', [
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"brianium/paratest": "^7.4",
"fakerphp/faker": "^1.9",
"larastan/larastan": "^2.7",
"laravel-lang/common": "^6.1",
"laravel/breeze": "^1.26",
"laravel/dusk": "^7.11",
"laravel/pint": "^1.13",
Expand Down
Loading

0 comments on commit deb2683

Please sign in to comment.