diff --git a/backend/app/Http/Controllers/Auth/RegisteredUserController.php b/backend/app/Http/Controllers/Auth/RegisteredUserController.php index cc84828..98509eb 100644 --- a/backend/app/Http/Controllers/Auth/RegisteredUserController.php +++ b/backend/app/Http/Controllers/Auth/RegisteredUserController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; +use App\Mail\UserPasswordReset; use App\Mail\UserRegistrationCompleted; use App\Models\Company; use App\Models\StudentData; @@ -10,7 +11,6 @@ use App\Models\User; use Illuminate\Auth\Events\Registered; use Illuminate\Http\Request; use Illuminate\Http\Response; -use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Mail; @@ -78,4 +78,23 @@ class RegisteredUserController extends Controller return response()->noContent(); } -} + + public function reset_password(Request $request): Response { + $request->validate([ + 'email' => ['required', 'string', 'lowercase', 'email', 'max:255'], + ]); + + $user = User::whereEmail($request->email)->first(); + if (!$user) { + return response(status: 400); + } + + $newPassword = bin2hex(random_bytes(16)); + $user->password = Hash::make($newPassword); + $user->save(); + + Mail::to($user)->sendNow(new UserPasswordReset($user->name, $newPassword)); + + return response()->noContent(); + } +} \ No newline at end of file diff --git a/backend/app/Mail/UserPasswordReset.php b/backend/app/Mail/UserPasswordReset.php new file mode 100644 index 0000000..f5fe139 --- /dev/null +++ b/backend/app/Mail/UserPasswordReset.php @@ -0,0 +1,61 @@ +name = $name; + $this->password = $password; + } + + /** + * Get the message envelope. + */ + public function envelope(): Envelope + { + return new Envelope( + subject: '[ISOP] Reset hesla', + ); + } + + /** + * Get the message content definition. + */ + public function content(): Content + { + return new Content( + view: 'mail.passwordreset', + with: [ + 'name' => $this->name, + 'password' => $this->password, + ] + ); + } + + /** + * Get the attachments for the message. + * + * @return array + */ + public function attachments(): array + { + return []; + } +} diff --git a/backend/resources/views/mail/passwordreset.blade.php b/backend/resources/views/mail/passwordreset.blade.php new file mode 100644 index 0000000..606a2a5 --- /dev/null +++ b/backend/resources/views/mail/passwordreset.blade.php @@ -0,0 +1,12 @@ +@include("parts.header") +

Vážená/ý {{ $name }},

+

vaše heslo bolo úspešne resetované

+
+ +

Vaše nové heslo je: {{ $password }}

+ +
+ +

s pozdravom

+

Systém ISOP UKF

+@include("parts.footer") \ No newline at end of file diff --git a/backend/routes/api.php b/backend/routes/api.php index 874bb78..1092813 100644 --- a/backend/routes/api.php +++ b/backend/routes/api.php @@ -1,5 +1,6 @@ get('/user', function (Request $request) { return $user; }); + +Route::post('/password-reset', [RegisteredUserController::class, 'reset_password']) + ->middleware(['guest', 'throttle:6,1']) + ->name('password.reset'); \ No newline at end of file diff --git a/frontend/app/pages/reset_psw.vue b/frontend/app/pages/reset_psw/index.vue similarity index 80% rename from frontend/app/pages/reset_psw.vue rename to frontend/app/pages/reset_psw/index.vue index 235ca59..b8f5029 100644 --- a/frontend/app/pages/reset_psw.vue +++ b/frontend/app/pages/reset_psw/index.vue @@ -1,5 +1,5 @@ + + + +