Skip to content
New issue

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

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

Already on GitHub? # to your account

Feat custom auth fields #31

Merged
merged 2 commits into from
Jun 5, 2023
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
20 changes: 18 additions & 2 deletions config/filament-lockscreen.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
<?php

use Filament\Facades\Filament;

return [

/*
* Lock Screen Icon
*/
'icon' => 'heroicon-s-lock-closed',

/*
| ------------------------------------------------------------------------------------------------
| Table Column Name
| ------------------------------------------------------------------------------------------------
| Change the table column name if your login authentication column is checking on a different field and not on the default field ('email and password') column of the table.
*/
'table_columns' => [
'account_username_field' => 'email',
'account_username' => Filament::auth()->user()->email,
'account_password' => 'password',
],

/* =======================================
* if `enable_redirect_to` is TRUE then after login, it will be redirected to the route setup under `redirect_route`
*/
Expand All @@ -21,6 +36,7 @@
'rate_limit_max_count' => 5, // max count for failure login allowed.
'force_logout' => false,
],

/* =========================
* Path segmentation locking
* e.g., if the segment is enabled then locked_path = ['admin', 'employee']
Expand All @@ -33,6 +49,6 @@
'segment' => [
'specific_path_only' => false, // if false, then all the request will be blocked by the locker and will be redirected to the authentication page
'segment_needle' => 1, // see the https://laravel.com/api/9.x/Illuminate/Http/Request.html#method_segment
'locked_path' => [] //
]
'locked_path' => [], //
],
];
4 changes: 2 additions & 2 deletions resources/lang/en/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
],
'button' => [
'switch_account' => 'Switch Account',
'submit_label' => '#'
'submit_label' => '#',
],
'notification' => [
'title' => 'Login failure',
'message' => 'You have been redirected to the login page after succeeding login failure.',
]
],
];
4 changes: 2 additions & 2 deletions resources/lang/fr/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
'heading' => 'Verouillé',
'button' => [
'switch_account' => 'Changer de compte',
'submit_label' => 'Connexion'
]
'submit_label' => 'Connexion',
],
];
4 changes: 2 additions & 2 deletions resources/lang/ru/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
'heading' => 'Экран блокировки',
'button' => [
'switch_account' => 'Сменить аккаунт',
'submit_label' => 'Войти'
'submit_label' => 'Войти',
],
'notification' => [
'title' => 'Ошибка входа',
'message' => 'Вы были перенаправлены на страницу входа после неудачной попытки входа.',
]
],
];
20 changes: 10 additions & 10 deletions resources/lang/uk/default.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

return [
'heading' => 'Екран блокування',
'button' => [
'switch_account' => 'Змінити обліковий запис',
'submit_label' => 'Увійти'
],
'notification' => [
'title' => 'Помилка входу',
'message' => 'Ви були перенаправлені на сторінку входу після невдалої спроби входу.',
]
];
'heading' => 'Екран блокування',
'button' => [
'switch_account' => 'Змінити обліковий запис',
'submit_label' => 'Увійти',
],
'notification' => [
'title' => 'Помилка входу',
'message' => 'Ви були перенаправлені на сторінку входу після невдалої спроби входу.',
],
];
3 changes: 1 addition & 2 deletions src/FilamentLockscreenServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public function configurePackage(Package $package): void
->hasConfigFile()
->hasViews()
->hasTranslations()
->hasRoute('web')
;
->hasRoute('web');
}

/**
Expand Down
34 changes: 18 additions & 16 deletions src/Http/Livewire/LockerScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ class LockerScreen extends Component implements HasForms
public function mount()
{
session(['lockscreen' => true]);
if(!config('filament-lockscreen.enable_redirect_to'))
if(!session()->has('next') || session()->get('next') === null )
{
if (! config('filament-lockscreen.enable_redirect_to')) {
if (! session()->has('next') || session()->get('next') === null) {
session(['next' => url()->previous()]);
}
}
}

protected function forceLogout()
{
Filament::auth()->logout();
Expand All @@ -46,43 +47,45 @@ public function login()
/*
* Rate Limit
*/
if(config('filament-lockscreen.rate_limit.enable_rate_limit'))
{
if (config('filament-lockscreen.rate_limit.enable_rate_limit')) {
try {
$this->rateLimit(config('filament-lockscreen.rate_limit.rate_limit_max_count', 5));
} catch (TooManyRequestsException $exception) {
if(config('filament-lockscreen.rate_limit.force_logout', false))
{
if (config('filament-lockscreen.rate_limit.force_logout', false)) {
$this->forceLogout();

return redirect(url(config('filament.path')));
}
$this->addError(
'password', __('filament::login.messages.throttled', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]));
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]));

return null;
}
}

if (! Filament::auth()->attempt([
'email' => Filament::auth()->user()->email,
'password' => $data['password']
config('filament-lockscreen.table_columns.account_username_field') => config('filament-lockscreen.table_columns.account_username'),
config('filament-lockscreen.table_columns.account_password') => $data['password'],
])) {
$this->addError('password', __('filament::login.messages.failed'));

return null;
}



// redirect to the main page and forge the lockscreen session
session()->forget('lockscreen');
session()->regenerate();
if(config('filament-lockscreen.enable_redirect_to')) return redirect()->route(config('filament-lockscreen.redirect_route'));
if (config('filament-lockscreen.enable_redirect_to')) {
return redirect()->route(config('filament-lockscreen.redirect_route'));
}
// store to variable
$url = session()->get('next');
// remove the value
session()->forget('next');

return redirect($url);
}

Expand All @@ -95,7 +98,6 @@ protected function getFormSchema(): array
];
}


public function render()
{
return view('filament-lockscreen::livewire.locker-screen')
Expand Down
11 changes: 5 additions & 6 deletions src/Http/Middleware/Locker.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ public function handle($request, Closure $next)
* $request->method() === 'GET'
* this will not block the request coming from Livewire page
*/
if(config('filament-lockscreen.segment.specific_path_only', false))
{
if (config('filament-lockscreen.segment.specific_path_only', false)) {
$needle = config('filament-lockscreen.segment.segment_needle', 1);
$blocked_path = config('filament-lockscreen.segment.locked_path');
if ($request->session()->get('lockscreen') && $request->method() === 'GET' && in_array($request->segment($needle),$blocked_path)) return redirect()->route('lockscreenpage');
return $next($request);
if ($request->session()->get('lockscreen') && $request->method() === 'GET' && in_array($request->segment($needle), $blocked_path)) {
return redirect()->route('lockscreenpage');
}

return $next($request);
}


/*
* USE THIS CONDITION IF THE SEGMENT IS DISABLED IN THE CONFIG FILE
* All the request will be blocked
Expand All @@ -39,5 +39,4 @@ public function handle($request, Closure $next)

return $next($request);
}

}