1. Go to this page and download the library: Download islem-kms/l5-mauth-pass library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;
class Admin extends Authenticatable
{
use Notifiable, HasApiTokens;
use Route;
use Laravel\Passport\Passport;
class AuthServiceProvider extends ServiceProvider
{
...
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Passport::routes();
// Middleware `api` that contains the `custom-provider` middleware group defined on $middlewareGroups above
Route::group(['middleware' => 'api'], function () {
Passport::routes(function ($router) {
return $router->forAccessTokens();
});
});
}
...
}
Route::group(['middleware' => ['api', 'auth:admin']], function () {
Route::get('/admin', function ($request) {
// Get the logged admin instance
return $request->user(); // You can use too `$request->user('admin')` passing the guard.
});
});
// `api` guard on end of guards separated by comma
Route::group(['middleware' => ['api', 'auth:admin,api']], function () {
Route::get('/admin', function ($request) {
// Passing `api` guard to `$request->user()` method
// The instance of user authenticated (Admin or User in this case) will be returned
return $request->user('api');
});
});
Route::group(['middleware' => 'auth:admin,api'], function () {
Route::get('/foo', function ($request) {
return $request->user('api'); // Return user or admin
});
});
use App\User;
use App\Admin;
use Laravel\Passport\Passport;
class MyTest extends TestCase
{
public function testFooAdmin()
{
$admin = factory(Admin::class)->create();
Passport::actingAs($admin);
// When you use your endpoint your admin will be returned
$this->json('GET', '/foo')
->assertStatus(200)
->assertJson([
'data' => [
'id' => 1,
'name' => 'Admin',
'email' => '[email protected]'
]
]);
}
public function testFooUser()
{
$user = factory(User::class)->create();
Passport::actingAs($user);
// When you use your endpoint your user will be returned
$this->json('GET', '/foo')
->assertStatus(200)
->assertJson([
'data' => [
'id' => 1,
'name' => 'User',
'email' => '[email protected]'
]
]);
}
}
Route::group(['middleware' => 'auth:admin'], function () {
Route::get('/foo', function ($request) {
return $request->user(); // Return admin
});
});
console
php artisan migrate
console
php artisan vendor:publish
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.