You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.5 KiB
40 lines
1.5 KiB
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
|
|
use Laravel\Passport\Passport;
|
|
use League\OAuth2\Server\AuthorizationServer;
|
|
use App\Auth\PassportUserRepository; // Custom Passport user repository
|
|
use Laravel\Passport\Bridge\RefreshTokenRepository;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use App\Auth\CustomUserProvider; // Custom user provider for web login
|
|
use DateInterval;
|
|
|
|
class AuthServiceProvider extends ServiceProvider
|
|
{
|
|
protected $policies = [];
|
|
|
|
public function boot(): void
|
|
{
|
|
// Set Passport token expiration times
|
|
Passport::tokensExpireIn(now()->addHour());
|
|
Passport::refreshTokensExpireIn(now()->addMonth());
|
|
|
|
// Register CustomUserProvider for web login
|
|
Auth::provider('custom', function ($app, array $config) {
|
|
return new CustomUserProvider($app['hash'], $config['model']);
|
|
});
|
|
|
|
// Use custom Passport user repository for API login
|
|
$this->app->afterResolving(AuthorizationServer::class, function ($server) {
|
|
$grant = new \League\OAuth2\Server\Grant\PasswordGrant(
|
|
app(PassportUserRepository::class), // Custom Passport user repository
|
|
app(RefreshTokenRepository::class)
|
|
);
|
|
$grant->setRefreshTokenTTL(new DateInterval('P1M')); // 1 month refresh token
|
|
|
|
$server->enableGrantType($grant, new DateInterval('PT1H')); // 1 hour access token
|
|
});
|
|
}
|
|
}
|
|
|