mirror of
https://github.com/filamentphp/filament.git
synced 2026-10-24 23:35:30 +08:00
159 lines
4.6 KiB
PHP
159 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace Filament\Pages\Auth;
|
|
|
|
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
|
|
use DanHarrin\LivewireRateLimiting\WithRateLimiting;
|
|
use Filament\Actions\Action;
|
|
use Filament\Facades\Filament;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Form;
|
|
use Filament\Http\Responses\Auth\Contracts\RegistrationResponse;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\CardPage;
|
|
use Illuminate\Auth\EloquentUserProvider;
|
|
use Illuminate\Auth\Events\Registered;
|
|
use Illuminate\Auth\SessionGuard;
|
|
use Illuminate\Contracts\Support\Htmlable;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Validation\Rules\Password;
|
|
|
|
/**
|
|
* @property Form $form
|
|
*/
|
|
class Register extends CardPage
|
|
{
|
|
use WithRateLimiting;
|
|
|
|
/**
|
|
* @var view-string
|
|
*/
|
|
protected static string $view = 'filament::pages.auth.register';
|
|
|
|
/**
|
|
* @var array<string, mixed> | null
|
|
*/
|
|
public ?array $data = [];
|
|
|
|
protected string $userModel;
|
|
|
|
public function mount(): void
|
|
{
|
|
if (Filament::auth()->check()) {
|
|
redirect()->intended(Filament::getUrl());
|
|
}
|
|
|
|
$this->form->fill();
|
|
}
|
|
|
|
public function register(): ?RegistrationResponse
|
|
{
|
|
try {
|
|
$this->rateLimit(2);
|
|
} catch (TooManyRequestsException $exception) {
|
|
Notification::make()
|
|
->title(__('filament::pages/auth/register.messages.throttled', [
|
|
'seconds' => $exception->secondsUntilAvailable,
|
|
'minutes' => ceil($exception->secondsUntilAvailable / 60),
|
|
]))
|
|
->danger()
|
|
->send();
|
|
|
|
return null;
|
|
}
|
|
|
|
$data = $this->form->getState();
|
|
|
|
$user = $this->getUserModel()::create($data);
|
|
|
|
app()->bind(
|
|
\Illuminate\Auth\Listeners\SendEmailVerificationNotification::class,
|
|
\Filament\Listeners\Auth\SendEmailVerificationNotification::class,
|
|
);
|
|
event(new Registered($user));
|
|
|
|
Filament::auth()->login($user);
|
|
|
|
session()->regenerate();
|
|
|
|
return app(RegistrationResponse::class);
|
|
}
|
|
|
|
public function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
TextInput::make('name')
|
|
->label(__('filament::pages/auth/register.fields.name.label'))
|
|
->required()
|
|
->maxLength(255)
|
|
->autofocus(),
|
|
TextInput::make('email')
|
|
->label(__('filament::pages/auth/register.fields.email.label'))
|
|
->email()
|
|
->required()
|
|
->maxLength(255)
|
|
->unique($this->getUserModel()),
|
|
TextInput::make('password')
|
|
->label(__('filament::pages/auth/register.fields.password.label'))
|
|
->password()
|
|
->required()
|
|
->rule(Password::default())
|
|
->dehydrateStateUsing(fn ($state) => Hash::make($state))
|
|
->same('passwordConfirmation')
|
|
->validationAttribute(__('filament::pages/auth/register.fields.password.validation_attribute')),
|
|
TextInput::make('passwordConfirmation')
|
|
->label(__('filament::pages/auth/register.fields.passwordConfirmation.label'))
|
|
->password()
|
|
->required()
|
|
->dehydrated(false),
|
|
])
|
|
->statePath('data');
|
|
}
|
|
|
|
public function registerAction(): Action
|
|
{
|
|
return Action::make('register')
|
|
->label(__('filament::pages/auth/register.buttons.register.label'))
|
|
->submit('register');
|
|
}
|
|
|
|
public function loginAction(): Action
|
|
{
|
|
return Action::make('login')
|
|
->link()
|
|
->label(__('filament::pages/auth/register.buttons.login.label'))
|
|
->url(filament()->getLoginUrl());
|
|
}
|
|
|
|
protected function getUserModel(): string
|
|
{
|
|
if (isset($this->userModel)) {
|
|
return $this->userModel;
|
|
}
|
|
|
|
/** @var SessionGuard $authGuard */
|
|
$authGuard = Filament::auth();
|
|
|
|
/** @var EloquentUserProvider $provider */
|
|
$provider = $authGuard->getProvider();
|
|
|
|
return $this->userModel = $provider->getModel();
|
|
}
|
|
|
|
public static function getName(): string
|
|
{
|
|
return 'filament.core.auth.register';
|
|
}
|
|
|
|
public function getTitle(): string
|
|
{
|
|
return __('filament::pages/auth/register.title');
|
|
}
|
|
|
|
public function getHeading(): string | Htmlable
|
|
{
|
|
return __('filament::pages/auth/register.heading');
|
|
}
|
|
}
|