laravel passport block user code example

Example 1: disable laravel passport

You can remove passport by manually deleting this line "laravel/passport": "^4.0" 
in your composer.json file then run composer update. Or composer remove laravel/passport

If you're running Laravel 5.4 or below, make sure to remove this line in 
your app.config file Laravel\Passport\PassportServiceProvider::class

And all classes that relies on passport must be edited as well. 
The most common classes are:

1) User model, remove the HasApiToken trait.
2) AuthServiceProvider, remove Passport::routes(); in your boot method.
3) Your config/auth.php, change your driver option for api authentication

php artisan migrate:refresh run at the end of step. 
To remove the Passport Migrations in database migrations table. 
but please be careful in you are in production.





With Laravel 7

Step 1. In the file app/Providers/AuthServiceProvider.php, remove these two lines:

use Laravel\Passport\Passport;
Passport::routes();
Step 2.

$ composer remove laravel/passport
$ rm -r ./resources/js/components/passport # if any
$ rm -r ./resources/views/vendor/passport # if any
Step 3. In the file resources/js/app.js, remove passport components registration. You may also find and remove these registered components if you used it somewhere:

$ grep -rn 'passport-authorized-clients'     resources/js/*
$ grep -rn 'passport-personal-access-tokens' resources/js/*
$ grep -rn 'passport-clients'                resources/js/*
Step 4. Find and remove HasApiTokens from your models:

$ grep -rn HasApiTokens * 
Remove also the import line going with it:

use Laravel\Passport\HasApiTokens;
Step 5. Remove oauth keys

$ rm storage/oauth-*.key
Step 6. In the file config/auth.php, look for guards:api:driver and revert from passport to token.

Step 7. Drop Passport tables and clean migration table

$ php artisan tinker
>>> Schema::drop('oauth_access_tokens');
>>> Schema::drop('oauth_auth_codes');
>>> Schema::drop('oauth_clients');
>>> Schema::drop('oauth_personal_access_clients');
>>> Schema::drop('oauth_refresh_tokens');
>>> DB::table('migrations')->where('migration', 'like', '%_oauth_access_tokens_table')->delete();
>>> DB::table('migrations')->where('migration', 'like', '%_oauth_auth_codes_table')->delete();
>>> DB::table('migrations')->where('migration', 'like', '%_oauth_clients_table')->delete();
>>> DB::table('migrations')->where('migration', 'like', '%_oauth_personal_access_clients_table')->delete();
>>> DB::table('migrations')->where('migration', 'like', '%_oauth_refresh_tokens_table')->delete();
>>> exit

Step 8. And finally, refresh your installation:

$ composer dump-autoload
$ php artisan optimize:clear
$ npm run dev



https://stackoverflow.com/questions/47567249/how-to-uninstall-laravel-passport

Example 2: laravel token authentication

# Database Preparation
// add api_token to users table
Schema::table('users', function ($table) {
    $table->string('api_token', 80)->after('password')
                        ->unique()
                        ->nullable()
                        ->default(null);
});

// Create token for existing users, code can also be added to registerController
    $token = Str::random(60);
    $user = User::find(1);
    $user->api_token = hash('sha256', $token); // <- This will be used in client access
    $user->save();



//config/auth.php
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token', // <- Add this entry
            'provider' => 'users',
            'hash' => false,
        ],
    ],

          
          
//routes/api.php
    // Add "middleware('auth:api')" as below        
	Route::middleware('auth:api')->get('/user', function (Request $request) {
        return $request->user();
    });          



//client access example (in Vue js)

axios.get('http://example.com/api/user', 
          {
  headers: { 
    'Accept': 'application/json', 
    'Authorization': 'Bearer '+ 'user-api-token'
  }
}
         )
  .then(function (response) {
  // handle success
  console.log(response);
})
  .catch(function (error) {
  // handle error
  console.log(error);
})

Tags:

Misc Example