Here are the steps to integrate Laravel Passport API authentication into your Laravel application:
Install Laravel Passport package:
bashcomposer require laravel/passportRun migration to create the necessary tables for Passport:
php artisan migrateAdd the Passport service provider to the
config/app.phpfile:arduino'providers' => [ // ... Laravel\Passport\PassportServiceProvider::class, ]In your
AppServiceProvider, add the following to theboot()method to configure Passport:phpuse Laravel\Passport\Passport; public function boot() {Passport::routes(); }Run the Passport installation command to generate encryption keys:
php artisan passport:installAdd the
Laravel\Passport\HasApiTokenstrait to yourUsermodel:phpuse Laravel\Passport\HasApiTokens; class User extends Authenticatable {.use HasApiTokens, Notifiable; // }In your
config/auth.phpfile, add theapiguard and set the driver topassport:arduino'guards' => [ // ... 'api' => [ 'driver' => 'passport', 'provider' => 'users', ], ]In your
routes/api.phpfile, define the routes that require authentication using theauth:apimiddleware:bashRoute::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); });
That's it! Now you can use the /oauth/token endpoint to generate access tokens for your API clients. You can also use the Passport middleware to protect other routes that require authentication.
No comments:
Post a Comment