Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Client Panel with Authentication and Profile Management #300

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions app/Filament/Client/Pages/Dashboard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@


<?php

namespace App\Filament\Client\Pages;

use App\Models\Invoice;
use Filament\Pages\Dashboard as BaseDashboard;
use Filament\Widgets\StatsOverviewWidget\Card;
use Filament\Widgets\StatsOverviewWidget;

class Dashboard extends BaseDashboard
{
protected static ?string $navigationIcon = 'heroicon-o-home';
protected static string $view = 'filament.pages.dashboard';

protected function getHeaderWidgets(): array
{
$client = auth()->user();

return [
StatsOverviewWidget::class,
];
}

protected function getColumns(): int
{
return 2;
}
}
87 changes: 87 additions & 0 deletions app/Filament/Client/Pages/Profile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@


<?php

namespace App\Filament\Client\Pages;

use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Pages\Page;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;

class Profile extends Page
{
protected static ?string $navigationIcon = 'heroicon-o-user';
protected static string $view = 'filament.client.pages.profile';

public ?array $data = [];

public function mount(): void
{
$this->form->fill([
'name' => auth()->user()->name,
'email' => auth()->user()->email,
'company' => auth()->user()->company,
'phone' => auth()->user()->phone,
]);
}

public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->required()
->maxLength(255),
TextInput::make('email')
->email()
->required()
->maxLength(255)
->unique('clients', 'email', ignorable: auth()->user()),
TextInput::make('company')
->maxLength(255),
TextInput::make('phone')
->tel()
->maxLength(20),
TextInput::make('current_password')
->password()
->label('Current Password')
->required()
->visible(fn ($get) => (bool) $get('password')),
TextInput::make('password')
->password()
->label('New Password')
->rule(Password::defaults()),
TextInput::make('password_confirmation')
->password()
->label('Confirm Password')
->visible(fn ($get) => (bool) $get('password'))
->same('password'),
]);
}

public function submit(): void
{
$data = $this->form->getState();

$user = auth()->user();

if (isset($data['password'])) {
if (!Hash::check($data['current_password'], $user->password)) {
$this->addError('current_password', 'The provided password is incorrect.');
return;
}
$data['password'] = Hash::make($data['password']);
}

$user->update($data);

$this->notify('success', 'Profile updated successfully');
}

protected static function shouldRegisterNavigation(): bool
{
return true;
}
}
63 changes: 63 additions & 0 deletions app/Filament/Client/Resources/InvoiceResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@


<?php

namespace App\Filament\Client\Resources;

use App\Filament\Client\Resources\InvoiceResource\Pages;
use App\Models\Invoice;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;

class InvoiceResource extends Resource
{
protected static ?string $model = Invoice::class;
protected static ?string $navigationIcon = 'heroicon-o-document-text';
protected static ?string $navigationLabel = 'Invoices';

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('invoice_number')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('total_amount')
->money('usd')
->sortable(),
Tables\Columns\TextColumn::make('status')
->badge()
->colors([
'warning' => 'pending',
'success' => 'paid',
'danger' => 'overdue',
]),
Tables\Columns\TextColumn::make('due_date')
->date()
->sortable(),
])
->filters([
Tables\Filters\SelectFilter::make('status')
->options([
'pending' => 'Pending',
'paid' => 'Paid',
'overdue' => 'Overdue',
]),
])
->defaultSort('created_at', 'desc');
}

public static function getPages(): array
{
return [
'index' => Pages\ListInvoices::class,
'view' => Pages\ViewInvoice::class,
];
}

public static function getEloquentQuery(): \Illuminate\Database\Eloquent\Builder
{
return parent::getEloquentQuery()->where('client_id', auth()->id());
}
}
56 changes: 56 additions & 0 deletions app/Providers/Filament/ClientPanelProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@


<?php

namespace App\Providers\Filament;

use App\Filament\Client\Pages;
use App\Models\Client;
use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\Support\Colors\Color;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;

class ClientPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->id('client')
->path('client')
->login()
->colors([
'primary' => Color::Blue,
])
->discoverResources(in: app_path('Filament/Client/Resources'), for: 'App\\Filament\\Client\\Resources')
->discoverPages(in: app_path('Filament/Client/Pages'), for: 'App\\Filament\\Client\\Pages')
->pages([
Pages\Dashboard::class,
Pages\Profile::class,
])
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->authMiddleware([
Authenticate::class,
])
->authGuard('client');
}
}
Loading