Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add laravel precognition and ajax #72

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 7 additions & 11 deletions app/Http/Controllers/Auth/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,25 @@
namespace App\Http\Controllers\Auth;

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

class PasswordController extends Controller
{
use PasswordValidationRules;

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

$request->user()->update([
'password' => Hash::make($validated['password']),
]);

return back()->with('status', 'password-updated');
return $request->expectsJson()
? response()->json(['status' => 'password-updated'])
: back()->with('status', 'password-updated');
}
}
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') || app()->environment('testing')
? 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
28 changes: 18 additions & 10 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,15 +36,17 @@ public function new(): View
]);
}

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

$list = (new CreateList(
name: $request->input('list-name'),
description: $request->input('description'),
name: $validated['listname'],
description: $validated['description'],
isPublic: false,
canBeModified: true,
gender: $request->input('gender'),
categoryId: $request->input('category'),
gender: $validated['gender'],
categoryId: $validated['category'],
))->execute();

Cache::forget('route-list-' . $list->id);
Expand Down Expand Up @@ -81,13 +84,18 @@ public function edit(Request $request): View
]);
}

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

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

$list->name = $request->input('list-name');
$list->description = $request->input('description');
$list->list_category_id = $request->input('category');
$list->name = $validated['listname'];
$list->description = $validated['description'];
$list->gender = $validated['gender'];
if (auth()->user()->is_administrator) {
$list->list_category_id = $validated['category'];
}
$list->save();

Cache::forget('route-list-' . $list->id);
Expand All @@ -96,7 +104,7 @@ public function update(Request $request): RedirectResponse

return Redirect::route('list.show', [
'liste' => $list->id,
]);
])->withStatus('La liste a été mise à jour.');
}

public function destroy(Request $request): RedirectResponse
Expand Down
31 changes: 22 additions & 9 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,40 @@

namespace App\Http\Controllers;

use App\Http\Requests\ProfileNameRequest;
use App\Http\Requests\ProfileUpdateRequest;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Symfony\Component\HttpFoundation\Response;

class ProfileController extends Controller
{
public function show(Request $request): View
public function edit(Request $request): View
{
return view('user.account', [
return view('profile.edit', [
'user' => $request->user(),
]);
}

/**
* Update the user's password.
* Update the user's profile.
*/
public function update(Request $request): RedirectResponse
public function update(ProfileUpdateRequest $request): Response
{
$request->user()->fill($request->validated());

if ($request->user()->isDirty('email')) {
$request->user()->email_verified_at = null;
}

$request->user()->save();

return Redirect::route('profile.edit')->with('status', 'profile-updated');
return $request->expectsJson()
? response()->json(['status' => 'profile-updated'])
: Redirect::route('profile.edit')->with('status', 'profile-updated');
}

/**
Expand All @@ -48,11 +59,13 @@ public function destroy(Request $request): RedirectResponse
return Redirect::to('/');
}

public function name(Request $request): RedirectResponse
public function name(ProfileNameRequest $request): Response
{
auth()->user()->last_name = $request->get('last_name');
auth()->user()->save();
$request->user()->fill($request->validated());
$request->user()->save();

return redirect()->route('profile.show');
return $request->expectsJson()
? response()->json(['status' => 'name-updated'])
: Redirect::route('profile.edit')->with('status', 'name-updated');
}
}
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(),
];
}
}
39 changes: 39 additions & 0 deletions app/Http/Requests/Auth/PasswordRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Http\Requests\Auth;

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

class PasswordRequest extends FormRequest
{
use PasswordValidationRules;

/**
* The key to be used for the view error bag.
*
* @var string
*/
protected $errorBag = 'updatePassword';

/**
* 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 [
'current_password' => 'required|string|current_password',
'password' => $this->passwordRules(),
];
}
}
41 changes: 41 additions & 0 deletions app/Http/Requests/Auth/RegisteredUserRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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',
'lowercase',
'email',
'max:255',
Rule::unique(User::class),
],
'password' => $this->passwordRules(),
];
}
}
Loading
Loading