Skip to content

navjotsinghprince/laravel-passport-uuid

Repository files navigation

Passport Introduction My Skills

Laravel Passport provides a full OAuth2 server implementation for your Laravel application in a matter of seconds.

Passport Install Version Details

Reference the table below for the correct version to use in conjunction with the version of Laravel you have installed and goldspecdigital/laravel-eloquent-uuid package

Laravel Passport Uuid
v9.* v10.* v9.0

⚠️ READ ALL THE RELATED DOCUMENT CAREFULLY BEFORE IMPLEMENT PASSPORT ON LARAVEL 9.x:

Note

WE ARE INSTALLING TWO PACKAGE HERE:

  1. Implement the goldspecdigital/laravel-eloquent-uuid package for uuid.
  2. Implement the laravel/passport package for authentication.

Step-1 Install UUID Package

composer require goldspecdigital/laravel-eloquent-uuid:^9.0

Step 2: Using the Uuid trait In app/Models/User.php

<?php

namespace App\Models;

use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Uuid;

class User extends Authenticatable
{
    use Uuid;

    /**
     * The "type" of the auto-incrementing ID.
     *
     * @var string
     */
    protected $keyType = 'string';

    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;
}

Step 3: Update Users Table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->uuid('id')->primary();  // Primary key.
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
};

Step 4: Migrate Database

Hint: Create A Database Backup Before Use

php artisan migrate:refresh --force

Step 5: Add Passport Package

composer require laravel/passport

Step 6: Install Passport With Uuid

Type answer to yes

php artisan passport:install --uuids

Step 7: Customization Passport Migration

You should customize passport default migrations for uuid case

php artisan vendor:publish --tag=passport-migrations

Step 8: Update Follwing Database Migrations

2016_06_01_000001_create_oauth_auth_codes_table.php

public function up()
    {
        $this->schema->create('oauth_auth_codes', function (Blueprint $table) {
            $table->string('id', 100)->primary();
         // $table->unsignedBigInteger('user_id')->index();
            $table->string('user_id')->index();
            $table->uuid('client_id');
            $table->text('scopes')->nullable();
            $table->boolean('revoked');
            $table->dateTime('expires_at')->nullable();
        });
    }

2016_06_01_000002_create_oauth_access_tokens_table.php

public function up()
    {
        $this->schema->create('oauth_access_tokens', function (Blueprint $table) {
            $table->string('id', 100)->primary();
            // $table->unsignedBigInteger('user_id')->nullable()->index();
            $table->string('user_id')->nullable()->index();
            $table->uuid('client_id');
            $table->string('name')->nullable();
            $table->text('scopes')->nullable();
            $table->boolean('revoked');
            $table->timestamps();
            $table->dateTime('expires_at')->nullable();
        });
}

2016_06_01_000004_create_oauth_clients_table

public function up()
    {
        $this->schema->create('oauth_clients', function (Blueprint $table) {
            $table->uuid('id')->primary();
            // $table->unsignedBigInteger('user_id')->nullable()->index();
            $table->string('user_id')->nullable()->index();
            $table->string('name');
            $table->string('secret', 100)->nullable();
            $table->string('provider')->nullable();
            $table->text('redirect');
            $table->boolean('personal_access_client');
            $table->boolean('password_client');
            $table->boolean('revoked');
            $table->timestamps();
        });
}

Step 9: Migrate Database

php artisan migrate:refresh --force

Step 10: Generate Client ID and Secret:

php artisan passport:client --personal --no-interaction

Step 11: Update App\Models\User model

<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens; //Here
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Uuid;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, Uuid;


    /**
     * The "type" of the auto-incrementing ID.
     *
     * @var string
     */
    protected $keyType = 'string';

    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

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

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

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

    

Step 12: Configuration App\Providers\AuthServiceProvider.php

<?php

namespace App\Providers;

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

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

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

        if (! $this->app->routesAreCached()) {
            Passport::routes();
        }
    }
}
    

Step 13: Finally config/auth.php

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

Step 14: Modify database/seeders/DatabaseSeeder.php

<?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        \App\Models\User::truncate();
        \App\Models\User::factory()->create([
            'name' => 'Test User',
            'email' => 'test@user.com',
            'password' => Hash::make("12345"),
        ]);
    }
}

Step 15: Create Controller

php artisan make:controller LoginController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

//Feel Free To Visit https://navjotsinghprince.com
class LoginController extends Controller
{
    public function login(Request $request)
    {
        $email = 'test@user.com';
        $password = '12345';

        if (Auth::attempt(['email' =>  $email, 'password' =>  $password])) {
            $user = Auth::user();
            $success['access_token'] =  $user->createToken('PrinceFerozepuria')->accessToken;
            return response()->json(['success' => $success], 200);
        } else {
            return response()->json(['error' => 'Unauthorised'], 401);
        }
    }

    public function getUser(Request $request)
    {
        $user = Auth::user();
        $response = [
            "user" =>  $user,
            "message" => "success"
        ];
        return response()->json($response, 200);
    }
}

Step 16: Create Routes routes/api.php

<?php

Route::post('login', [LoginController::class, 'login']);

Route::group(['middleware' => 'auth:api'], function () {
    Route::post('getUser', [LoginController::class, 'getUser']);
});

Step 17: Run

php artisan db:seed
php artisan optimize:clear
composer dump-autoload
php artisan serve

Authors

πŸ‘‰ Navjot Singh Prince

See also the site of contributor who participated in this project.

Contact

If you discover any question within passport, please send an e-mail to Prince Ferozepuria via fzr@navjotsinghprince.com. Your all questions will be answered.

Buy Me A Coffee! β˜•

Feel free to buy me a coffee at Buy me a coffee! β˜•, I would be really grateful for anything, be it coffee or just a kind comment towards my work, that helps me a lot.

Donation

The passport with uuid project is completely free to use, however, it has taken a lot of time to build. If you would like to show your appreciation by leaving a small donation, you can do so by clicking here here. Thanks!

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

About

Implements the laravel 9 passport authentication with uuid

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages