Skip to content

PHP Articles

php | frameworks | cms | and all web things.

Menu
  • Home
  • Laravel
  • WordPress
  • Server
  • Database
  • Google
Menu
lf

Laravel Api Integration & Configuration

Posted on February 7, 2020February 4, 2021 by admin

Recently I have worked on two Laravel mobile applications in my freelancing work and both are having android applications. So for the API authentication, I have used the Laravel Passport package. It’s very easy to authenticate each and every API using bearer token so I thought I have to create a new article on Laravel API integration & configuration which might be helpful for the new beginners who just started learning Laravel & Passport.

api integration

In this article, we are going to set up and configure Passport in fresh Laravel 6 applications so please stick with the steps, and later on, we will build API with authentication in the next article.

Step 1:

Install the latest version of Laravel using the below commands.

composer create-project laravel/laravel passport

Step 2:

After installing Laravel go to your project directory and install laravel/ui package to setup all routes and views for the authentication purpose.

composer require laravel/ui --dev
 
php artisan ui vue --auth

npm install && npm run dev

Step 3:

Create a new database and set up a connection in your .env file.

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=passport
DB_USERNAME=root
DB_PASSWORD=

Step 4:

Now install the Passport package in our Laravel application.

composer require laravel/passport

Once your installation completed, you have to set up a few things in your config/app.php file.

'providers' => [

    Laravel\Passport\PassportServiceProvider::class,
    
],

Few updates still required on below flies so please update accordingly.

  • app/User.php
  • app/Providers/AuthServiceProvider.php
  • config/auth.php

config/auth.php

<?php

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

app/Providers/AuthServiceProvider.php

<?php

namespace App\Providers;

use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        // 'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Passport::routes();
    }
}

config/auth.php

...

/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
        'hash' => false,
    ],
],

...

Step 4:

Generate tables using migration command.

php artisan migrate

If you are geeting error like “SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `u sers_email_unique`(`email`))” during migrate, refer this article: http://bit.ly/2JiCIVO

Step 5:

Generate passport token keys for the security.

php artisan passport:install --force

That’s it. You have successfully configured a Passport in your Laravel application.

Please visit my next blog post which is related to this article where you can learn on Laravel passport create API for your application.

Keep visiting for new stuff and give your feedback.

Happy Coding 😉

Share this:

  • Twitter
  • Facebook

Top Posts

  • Laravel Swagger Authenticate Users Via Bearer Token
  • How To Use wp_mail With WordPress
  • How To Use Darkaonline/l5-Swagger In Laravel
  • Replace Old To New Url In The WordPress Database
  • How To Change Pre-Installed PHP Version To MAMP In Mac/osx
  • Laravel Api Integration & Configuration
  • WordPress Custom Post Type Pagination

Recent Posts

  • Custom Gutenberg Block In WordPress
  • Laravel Swagger Authenticate Users Via Bearer Token
  • How To Use Darkaonline/l5-Swagger In Laravel
  • How To Fix You Already Have An Adsense Account
  • Laravel Passport Create Api For Your Application
  • Laravel Api Integration & Configuration

Archives

  • February 2021
  • July 2020
  • March 2020
  • February 2020
  • May 2019
  • January 2019
  • April 2018
  • March 2018
  • January 2018
  • December 2017

Categories

  • Ajax
  • Database
  • Google
  • Laravel
  • Server
  • WordPress
© 2022 PHP Articles | Powered by Minimalist Blog WordPress Theme