Skip to content

Commit

Permalink
chore(request): refactor auth request
Browse files Browse the repository at this point in the history
  • Loading branch information
WailanTirajoh committed Apr 10, 2024
1 parent d846019 commit b4ac047
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 11 deletions.
15 changes: 4 additions & 11 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace App\Http\Controllers;

use App\Http\Requests\ForgotPasswordRequest;
use App\Http\Requests\PostLoginRequest;
use App\Http\Requests\ResetPasswordRequest;
use App\Models\User;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
Expand All @@ -30,10 +31,8 @@ public function logout()
return redirect('login')->with('success', 'Logout success, goodbye '.$name);
}

public function forgotPassword(Request $request)
public function forgotPassword(ForgotPasswordRequest $request)
{
$request->validate(['email' => 'required|email']);

$status = Password::sendResetLink(
$request->only('email')
);
Expand All @@ -43,14 +42,8 @@ public function forgotPassword(Request $request)
: back()->withErrors(['email' => __($status)]);
}

public function resetPassword(Request $request)
public function resetPassword(ResetPasswordRequest $request)
{
$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) {
Expand Down
31 changes: 31 additions & 0 deletions app/Http/Requests/ForgotPasswordRequest.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 ForgotPasswordRequest 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 [
'email' => [
'required',
'email',
],
];
}
}
39 changes: 39 additions & 0 deletions app/Http/Requests/ResetPasswordRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ResetPasswordRequest 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 [
'token' => [
'required',
],
'email' => [
'required',
'email',
],
'password' => [
'required',
'min:8',
'confirmed',
],
];
}
}

0 comments on commit b4ac047

Please sign in to comment.