Skip to content

Commit

Permalink
Merge pull request #11 from riodwanto/feature/forgot-password
Browse files Browse the repository at this point in the history
Feature/forgot password
  • Loading branch information
riodwanto authored Mar 31, 2024
2 parents 662323d + 3a404ad commit 682c26b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
85 changes: 85 additions & 0 deletions app/Filament/Pages/Auth/RequestPasswordReset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace App\Filament\Pages\Auth;

use App\Settings\MailSettings;
use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
use Exception;
use Filament\Facades\Filament;
use Filament\Forms\Form;
use Filament\Notifications\Auth\ResetPassword as ResetPasswordNotification;
use Filament\Notifications\Notification;
use Filament\Pages\Auth\PasswordReset\RequestPasswordReset as BaseRequestPasswordReset;
use Illuminate\Contracts\Auth\CanResetPassword;
use Illuminate\Support\Facades\Password;

class RequestPasswordReset extends BaseRequestPasswordReset
{
protected static string $view = 'filament-panels::pages.auth.password-reset.request-password-reset';

public function form(Form $form): Form
{
return $form
->schema([
$this->getEmailFormComponent()->label('Email'),
]);
}

public function request(): void
{
try {
$this->rateLimit(3);
} catch (TooManyRequestsException $exception) {
Notification::make()
->title(__('filament-panels::pages/auth/password-reset/request-password-reset.notifications.throttled.title', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]))
->body(array_key_exists('body', __('filament-panels::pages/auth/password-reset/request-password-reset.notifications.throttled') ?: []) ? __('filament-panels::pages/auth/password-reset/request-password-reset.notifications.throttled.body', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]) : null)
->danger()
->send();

return;
}

$data = $this->form->getState();

$status = Password::broker(Filament::getAuthPasswordBroker())->sendResetLink(
$data,
function (CanResetPassword $user, string $token): void {
if (! method_exists($user, 'notify')) {
$userClass = $user::class;

throw new Exception("Model [{$userClass}] does not have a [notify()] method.");
}

$settings = app(MailSettings::class);
$notification = new ResetPasswordNotification($token);
$notification->url = Filament::getResetPasswordUrl($token, $user);

$settings->loadMailSettingsToConfig();

$user->notify($notification);
},
);

if ($status !== Password::RESET_LINK_SENT) {
Notification::make()
->title(__($status))
->danger()
->send();

return;
}

Notification::make()
->title(__($status))
->success()
->send();

$this->form->fill();
}
}
2 changes: 2 additions & 0 deletions app/Providers/Filament/AdminPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Filament\Pages\Auth\EmailVerification;
use App\Filament\Pages\Auth\Login;
use App\Filament\Pages\Auth\RequestPasswordReset;
use App\Livewire\MyProfileExtended;
use App\Settings\GeneralSettings;
use Filament\Http\Middleware\Authenticate;
Expand Down Expand Up @@ -31,6 +32,7 @@ public function panel(Panel $panel): Panel
->id('admin')
->path('admin')
->login(Login::class)
->passwordReset(RequestPasswordReset::class)
->emailVerification(EmailVerification::class)
->favicon(fn (GeneralSettings $settings) => Storage::url($settings->site_favicon))
->brandName(fn (GeneralSettings $settings) => $settings->brand_name)
Expand Down

0 comments on commit 682c26b

Please sign in to comment.