Skip to content

Commit 640918b

Browse files
Revert "some fixes"
This reverts commit 66857fe.
1 parent 66857fe commit 640918b

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ With "Laravel-ProjectAssist" at your disposal, you can expedite your Laravel pro
2828
- [DataClass Maker](#dataclass-maker)
2929
- [Lang file Maker](#lang-file-maker)
3030
- [.env.example generator](#envexample-generator)
31+
- [Useful Middlewares](#useful-middlewares)
32+
- [VerifyPassword](#verifypassword)
3133
- [License](#license)
3234

3335
## Installation
@@ -167,6 +169,38 @@ If you want to customize those command files, you can publish them using the fol
167169
php artisan vendor:publish --provider=HichemtabTech\LaravelProjectAssist\ProjectAssistServiceProvider --tag=console
168170
```
169171

172+
### Middlewares
173+
174+
Here is a list of middlewares provided by this library:
175+
176+
#### VerifyPassword
177+
178+
This middleware is used to verify the current password of the authenticated user before making an action you have to simply add a params to the reqyest named `currentPassword`.
179+
180+
Add this middleware to Kernel.php file:
181+
```php
182+
protected $routeMiddleware = [
183+
// ...
184+
'verifyPassword' => \App\Http\Middleware\VerifyPassword::class,
185+
];
186+
```
187+
188+
Then use it in your routes:
189+
190+
```php
191+
Route::post('/edit-profile', function (Request $request) {
192+
// The current password is valid...
193+
// continue the profile update
194+
})->middleware('verifyPassword');
195+
```
196+
197+
#### Customization
198+
199+
If you want to customize those middlewares, you can publish them using the following command:
200+
```Bash
201+
php artisan vendor:publish --provider=HichemtabTech\LaravelProjectAssist\ProjectAssistServiceProvider --tag=middleware
202+
```
203+
170204
## License
171205

172206
[MIT](https://github.com/HichemTab-tech/Laravel-ProjectAssist/blob/master/LICENSE)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Closure;
6+
use Exception;
7+
use Illuminate\Http\JsonResponse;
8+
use Illuminate\Http\RedirectResponse;
9+
use Illuminate\Http\Request;
10+
use Illuminate\Http\Response;
11+
use Illuminate\Support\Facades\Auth;
12+
use Illuminate\Support\Facades\Hash;
13+
use Illuminate\Validation\ValidationException;
14+
15+
class VerifyPassword
16+
{
17+
/**
18+
* Handle an incoming request.
19+
*
20+
* @param Request $request
21+
* @param Closure(Request): (Response|RedirectResponse) $next
22+
* @return Response|RedirectResponse|JsonResponse
23+
* @throws Exception
24+
*/
25+
public function handle(Request $request, Closure $next): Response|RedirectResponse|JsonResponse
26+
{
27+
$request->validate([
28+
'currentPassword' => 'required',
29+
]);
30+
31+
$user = Auth::user();
32+
if ($user == null) {
33+
throw new Exception('User not found');
34+
}
35+
$password = $request->input('currentPassword');
36+
37+
// Perform password verification against the user's actual stored password
38+
if (!Hash::check($password, $user->password)) {
39+
throw ValidationException::withMessages([
40+
'currentPassword' => ['The provided password does not match your current password.'],
41+
]);
42+
}
43+
return $next($request);
44+
}
45+
}

src/ProjectAssistServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public function boot(): void
1919
$this->publishes([
2020
__DIR__.'\Console' => app_path('Console'),
2121
], 'console');
22+
$this->publishes([
23+
__DIR__.'\Http\Middleware\VerifyPassword.php' => app_path('Http\Middleware\VerifyPassword.php'),
24+
], 'middleware');
2225
}
2326

2427
/**

0 commit comments

Comments
 (0)