sintonia_webapp/app/Actions/Fortify/CreateNewUser.php

45 lines
1.4 KiB
PHP

<?php
namespace App\Actions\Fortify;
use App\Models\User;
use Exception;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Laravel\Fortify\Contracts\CreatesNewUsers;
class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;
/**
* Validate and create a newly registered user.
*
* @param array{name: string, email: string, password: string} $userInfos An associative array containing user information.
*
* @throws Exception
*/
public function create(array $userInfos): User|Exception
{
try {
Validator::make($userInfos, [
'username' => ['required', 'string', 'max:255', Rule::unique(User::class, 'login')],
'password' => $this->passwordRules(),
'email' => ['required', 'string', 'max:255', Rule::unique(User::class, 'email')],
])->validate();
} catch (Exception $e) {
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
try {
return User::create([
'login' => $userInfos['username'],
'pass' => Hash::make($userInfos['password']),
'email' => $userInfos['email'],
]);
} catch (Exception $e) {
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
}
}