refactor: AppServiceProvider
- Remove unused authentik stuff - Move things to separate functions - Configure commands for production - Configure modals for better error handling - Improve password security by not allowing compromised passwords. - Rename some things to make it clearer. - Sort imports
This commit is contained in:
@@ -3,37 +3,67 @@
|
|||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
use App\Models\PersonalAccessToken;
|
use App\Models\PersonalAccessToken;
|
||||||
use Illuminate\Support\Facades\Event;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Facades\App;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Illuminate\Validation\Rules\Password;
|
use Illuminate\Validation\Rules\Password;
|
||||||
use Laravel\Sanctum\Sanctum;
|
use Laravel\Sanctum\Sanctum;
|
||||||
|
use Laravel\Telescope\TelescopeServiceProvider;
|
||||||
|
|
||||||
class AppServiceProvider extends ServiceProvider
|
class AppServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
public function register(): void
|
public function register(): void
|
||||||
{
|
{
|
||||||
if ($this->app->environment('local')) {
|
if (App::isLocal()) {
|
||||||
$this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
|
$this->app->register(TelescopeServiceProvider::class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function boot(): void
|
public function boot(): void
|
||||||
{
|
{
|
||||||
Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {
|
$this->configureCommands();
|
||||||
$event->extendSocialite('authentik', \SocialiteProviders\Authentik\Provider::class);
|
$this->configureModels();
|
||||||
});
|
$this->configurePasswords();
|
||||||
Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
|
$this->configureSanctumModel();
|
||||||
|
$this->configureGitHubHttp();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function configureCommands(): void
|
||||||
|
{
|
||||||
|
if (App::isProduction()) {
|
||||||
|
DB::prohibitDestructiveCommands();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function configureModels(): void
|
||||||
|
{
|
||||||
|
Model::shouldBeStrict();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function configurePasswords(): void
|
||||||
|
{
|
||||||
Password::defaults(function () {
|
Password::defaults(function () {
|
||||||
$rule = Password::min(8);
|
$rule = Password::min(8)->letters();
|
||||||
|
|
||||||
return $this->app->isProduction()
|
return App::isProduction()
|
||||||
? $rule->mixedCase()->letters()->numbers()->symbols()
|
? $rule->mixedCase()
|
||||||
|
->numbers()
|
||||||
|
->symbols()
|
||||||
|
->uncompromised()
|
||||||
: $rule;
|
: $rule;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Http::macro('github', function (string $api_url, ?string $github_access_token = null) {
|
private function configureSanctumModel(): void
|
||||||
|
{
|
||||||
|
Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function configureGitHubHttp(): void
|
||||||
|
{
|
||||||
|
Http::macro('GitHub', function (string $api_url, ?string $github_access_token = null) {
|
||||||
if ($github_access_token) {
|
if ($github_access_token) {
|
||||||
return Http::withHeaders([
|
return Http::withHeaders([
|
||||||
'X-GitHub-Api-Version' => '2022-11-28',
|
'X-GitHub-Api-Version' => '2022-11-28',
|
||||||
|
|||||||
Reference in New Issue
Block a user