At first, I was only relying on the documentation and also laracasts video to setup Passport. But as I go through, some undocumented steps are missing that I had to google it. Luckily I found a a solution from stackoverflow, and my project is back on schedule. Perhaps some of you experiencing the same problem, as I did, here are 10 easy direct steps you can follow (copy & paste) along without any hassles.
Ready?
Here we go...
- Run
composer require laravel/passport
from your shell (command prompt on Windows). - Open the config/app.php file, and add
Laravel\Passport\PassportServiceProvider::class
somewhere inside the $providers array - Run
php artisan:migrate
from your shell. - Still at your shell, run
php artisan passport:install
. - Open the User model class, it is located at app/User.php, and add
use Laravel\Passport\HasApiTokens;
somewhere between the namespace and the class declaration, also use this HasApiTokens trait inside the class like souse HasApiTokens, Notifiable;
. - Open the app/Providers/AuthServiceProvider.php file, and add
use Laravel\Passport\Passport;
between the namespace and the class declaration, and also add this linePassport::routes();
inside theboot()
function. - Open the config/auth.php file and change the API driver from token to passport
- If you intend to consume your API from the same Laravel application, simply open the app/Http/Kernel.php file, and add
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class
inside the$web
array. - Run
php artisan passport:client
to create an Oauth2 client for your API. From this step, you will get a personal access token. - Use the personal access token you get from step #9 and use it to hit your API.
- Authorization : "Bearer <token>"
- Accept : application/json
Happy coding guys ^_^