Refactor project structure and update dependencies
This commit is contained in:
@@ -3,14 +3,18 @@
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Helpers\Image;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Str;
|
||||
use App\Models\PersonalAccessToken;
|
||||
use Illuminate\Auth\Events\Lockout;
|
||||
use Illuminate\Auth\Notifications\ResetPassword;
|
||||
use Illuminate\Auth\Notifications\VerifyEmail;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Cache\RateLimiting\Limit;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
@@ -20,7 +24,7 @@ class AppServiceProvider extends ServiceProvider
|
||||
public function register(): void
|
||||
{
|
||||
// Register Telescope only in local environment
|
||||
if ($this->app->environment('local') && class_exists(\Laravel\Telescope\TelescopeServiceProvider::class)) {
|
||||
if (class_exists(\Laravel\Telescope\TelescopeServiceProvider::class) && $this->app->environment('local')) {
|
||||
$this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
|
||||
$this->app->register(TelescopeServiceProvider::class);
|
||||
}
|
||||
@@ -31,25 +35,43 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
RateLimiter::for('api', function (Request $request) {
|
||||
RateLimiter::for('api', static function (Request $request) {
|
||||
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
|
||||
});
|
||||
|
||||
RateLimiter::for('verification-notification', function (Request $request) {
|
||||
RateLimiter::for('verification-notification', static function (Request $request) {
|
||||
return Limit::perMinute(1)->by($request->user()?->email ?: $request->ip());
|
||||
});
|
||||
|
||||
RateLimiter::for('uploads', function (Request $request) {
|
||||
RateLimiter::for('uploads', static function (Request $request) {
|
||||
return $request->user()?->hasRole('admin')
|
||||
? Limit::none()
|
||||
: Limit::perMinute(10)->by($request->ip());
|
||||
});
|
||||
|
||||
ResetPassword::createUrlUsing(function (object $notifiable, string $token) {
|
||||
return config('app.frontend_url') . "/password-reset/{$token}?email={$notifiable->getEmailForPasswordReset()}";
|
||||
RateLimiter::for('login', static function (Request $request) {
|
||||
return Limit::perMinute(5)
|
||||
->by(Str::transliterate(implode('|', [
|
||||
strtolower($request->input('email')),
|
||||
$request->ip()
|
||||
])))
|
||||
->response(static function (Request $request, array $headers): void {
|
||||
event(new Lockout($request));
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'email' => trans('auth.throttle', [
|
||||
'seconds' => $headers['Retry-After'],
|
||||
'minutes' => ceil($headers['Retry-After'] / 60),
|
||||
]),
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
VerifyEmail::createUrlUsing(function (object $notifiable) {
|
||||
ResetPassword::createUrlUsing(static function (object $notifiable, string $token) {
|
||||
return config('app.frontend_url') . '/auth/reset/' . $token . '?email=' . $notifiable->getEmailForPasswordReset();
|
||||
});
|
||||
|
||||
VerifyEmail::createUrlUsing(static function (object $notifiable) {
|
||||
$url = url()->temporarySignedRoute(
|
||||
'verification.verify',
|
||||
now()->addMinutes(config('auth.verification.expire', 60)),
|
||||
@@ -65,8 +87,8 @@ class AppServiceProvider extends ServiceProvider
|
||||
/**
|
||||
* Convert uploaded image to webp, jpeg or png format and resize it
|
||||
*/
|
||||
UploadedFile::macro('convert', function (int $width = null, int $height = null, string $extension = 'webp', int $quality = 90): UploadedFile {
|
||||
return tap($this, function (UploadedFile $file) use ($width, $height, $extension, $quality) {
|
||||
UploadedFile::macro('convert', function (?int $width = null, ?int $height = null, string $extension = 'webp', int $quality = 90) {
|
||||
return tap($this, static function (UploadedFile $file) use ($width, $height, $extension, $quality) {
|
||||
Image::convert($file->path(), $file->path(), $width, $height, $extension, $quality);
|
||||
});
|
||||
});
|
||||
@@ -74,10 +96,23 @@ class AppServiceProvider extends ServiceProvider
|
||||
/**
|
||||
* Remove all special characters from a string
|
||||
*/
|
||||
Str::macro('onlyWords', function (string $text): string {
|
||||
Str::macro('onlyWords', static function (string $text): string {
|
||||
// \p{L} matches any kind of letter from any language
|
||||
// \d matches a digit in any script
|
||||
return Str::replaceMatches('/[^\p{L}\d ]/u', '', $text);
|
||||
});
|
||||
|
||||
Request::macro('deviceName', function (): string {
|
||||
$device = $this->device();
|
||||
|
||||
return implode(' / ', array_filter([
|
||||
trim(implode(' ', [$device->getOs('name'), $device->getOs('version')])),
|
||||
trim(implode(' ', [$device->getClient('name'), $device->getClient('version')])),
|
||||
])) ?? 'Unknown';
|
||||
});
|
||||
|
||||
Sanctum::usePersonalAccessTokenModel(
|
||||
PersonalAccessToken::class
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user