adrema/app/User/Actions/StoreAction.php

42 lines
1.0 KiB
PHP
Raw Permalink Normal View History

2024-08-02 15:12:47 +02:00
<?php
namespace App\User\Actions;
use App\User;
use App\User\Mail\WelcomeMail;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Password;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class StoreAction
{
use AsAction;
public function rules(): array
{
return [
'firstname' => 'required|string|max:255',
'lastname' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email',
];
}
public function handle(ActionRequest $request): JsonResponse
{
User::create([
...$request->validated(),
'password' => Hash::make(str()->random(32)),
]);
Password::broker()->sendResetLink(
$request->safe()->only('email'),
fn ($user, $token) => Mail::to($user)->send(new WelcomeMail($user, $token))
);
return response()->json([]);
}
}