Laravel api authentication package
Install the package, run the migration, initialize the passport
composer require azibom/who-are-you
php artisan migrate
php artisan passport:install
- Add the HasApiTokens trait to the user model
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use Notifiable, HasApiTokens;
/**
* 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',
];
}
- Change the api guard in the config/auth.php
...
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
...
- Add the WHO_ARE_YOU_BASE_URL env var in the .env file of the project
WHO_ARE_YOU_BASE_URL=http://localhost
(If you are using docker for example you should change it with your webserver container's name for example)
WHO_ARE_YOU_BASE_URL=http://nginx