API Authentication di Lumen 8 Laravel versi APP Engine | ILMU PROGRAMMER
Home » API Authentication di Lumen 8 Laravel versi APP Engine

API Authentication di Lumen 8 Laravel versi APP Engine

Hai, Setelah kita membuat Membuat Microservices menggunakan Lumen 8 Laravel versi APP Engine maka pada kali ini kita akan mengaktifkan mode authentication di API yang akan kita buat, langkah pertama bungkus api menggunakan auth pada routes/web.php

use Illuminate\Support\Str;

$router->group(['middleware' => 'auth'], function() use($router) {
    $router->get('/', function () use ($router) {
        return $router->app->version();
    });
    $router->get('/key', function(){
        $key = Str::random(32);
        return $key;
    });
});

Jangan lupa unremark pada app.php di foder bootstrap/app.php

$app->routeMiddleware([
    'auth' => App\Http\Middleware\Authenticate::class,
]);

$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class);

Buka file AuthServiceProvider pada path app/Providers/AuthServiceProvider.php pada method boot() kita modifikasi seperti dibawah ini:

$this->app['auth']->viaRequest('api', function ($request) {
        if ($request->header('Authorization')) {
            $explode = explode(' ', $request->header('Authorization'));
            return User::where('api_token', $explode[1])->first();
        }
        });

Buat table users menggunakan migration dengan mengetik php artisan make:migration users, lalu masuk ke database/xxxx_users.php ketik script berikut ini pada method up(); dan down()

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('identity_id');
            $table->char('gender', 1)->comment('0: female, 1: male')->default(0);
            $table->string('address');
            $table->string('photo');
            $table->string('email')->unique();
            $table->string('password');
            $table->string('phone_number');
            $table->string('api_token', 40);
            $table->char('role', 1)->comment('0: admin, 1: drivers, 2: users');
            $table->boolean('status')->default(false);
            $table->timestamps();
        });
    }

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

Tampilan models :

<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;

class User extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable;
    protected $table = 'users';
}

Jangan lupa dirubah koneksi mysql di file .env

lalu run dengan script : php artisan migrate
Jika terjadi error Access denied for user 'db'@'xxxxx' coba lakukan clear cache dengan mengetik script :

php artisan cache: clear
php artisan config: clear
php artisan cache: clear

Atau di .env pada DB_PASSWORD kasih tanda kutip satu, menjadi DB_PASSWORD='W@asas(*)##'

Selesai.

Leave a Comment

 
Open chat
Perlu Bantuan Konsultasi ?
Hallo Pembaca.
Telp (021) 55670115 untuk terhubung langsung dengan kami.

Apakah ada yang bisa kami bantu?