REST API with Laravel 5.8 using Laravel Passport
In this article, I will show how we can create REST API using laravel passport package by developing four API signup, login, logout, and getuser.
Laravel makes API authentication using Laravel Passport, which provides a full OAuth2 server implementation for your Laravel application in a matter of minutes.
Step 1: Install Laravel by issuing the Composer create-project command in your terminal.
we can get more details about server requirement and laravel installation on laravel documentation.
Step 2: Install Passport via the Composer package manage.
Step 3: Run Migration command.
The Passport service provider registers its own database migration directory with the framework, so you should migrate your database after installing the package. The Passport migrations will create the tables your application needs to store clients and access tokens.
Step 4: Run Passport install command.
This command will create the encryption keys needed to generate secure access tokens.
Step 5: Passport Configuration.
Next we have to configure passport by changing 3 files.
- add the Laravel\Passport\HasApiTokens trait to your App/User model.
app/user.php
2 . Next, you should call the Passport::routes() method within the boot method of your AuthServiceProvider.
app/Providers/AuthServiceProvider.php
3 . Finally, in your config/auth.php configuration file, you should set the driver option of the api authentication guard to passport.
config/auth.php
Step 6: Create API Routes
routes/api.php
Step 7: Create a Common Response Controller to return response.
app/Http/Controllers/API/ResponseController.php
Step 8: Create a Auth Controller.
app/Http/Controllers/API/AuthController.php
9. Now run the local development server.
This command will start a development server at localhost:8000.
Test : we can use postman tool test the API.
- signup
2. Login
3. Logout
4. get user