Download the PHP package islem-kms/l5-mauth-pass without Composer
On this page you can find all versions of the php package islem-kms/l5-mauth-pass. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download islem-kms/l5-mauth-pass
More information about islem-kms/l5-mauth-pass
Files in islem-kms/l5-mauth-pass
Informations about the package l5-mauth-pass
Laravel Passport Multi-Auth
Add multi-authentication support to Laravel Passport
Compatibility
Laravel Passport |
---|
^2.0 |
^3.0 |
Installing and configuring
-
Install using composer:
-
If you are using a Laravel version less than 5.5 you need to add the provider on
config/app.php
: -
Migrate database to create
oauth_access_token_providers
table: - Add new provider in
config/auth.php
using a model that extends ofAuthenticatable
class and useHasApiTokens
trait.
Ex.:
-
Configure your model:
-
And your
config/auth.php
providers: -
Add a new
guard
inconfig/auth.php
guards array using driverpassport
and the provider added above: -
Register the middlewares
AddCustomProvider
andConfigAccessTokenCustomProvider
on$middlewareGroups
attribute onapp/Http/Kernel
. - Encapsulate the passport routes for access token with the registered middleware in
AuthServiceProvider
:
Optional: Publish migrations:
And choose the provider IslemKms\PassportMultiauth\Providers\MultiauthServiceProvider
Usage
-
Add the
provider
parameter in your request at/oauth/token
: - You can pass your guards on
auth
middleware as you wish. Example:
The api
guard use is equals the example with admin
.
- You can pass many guards to
auth
middleware.
OBS: To pass many you need pass the api
guard on end of guards and the guard api
as parameter on $request->user()
method. Ex:
Refreshing tokens
- Add the
provider
parameter in your request at/oauth/token
:
Using scopes
- If you are using more than one guard on same route, use the following package middlewares instead of using the
scope
andscopes
middlewares fromLaravel\Passport
.
The middlewares are equals the Laravel\Passport
middlewares with guard api
on request->user()
object.
User Sample:
Unit tests
If you are using multi-authentication in a request you need to pass just an Authenticatable
object to Laravel\Passport\Passport::actingAs()
. E.g.:
-
You have a route with multi-auth:
-
On your test just pass your entity to
Passport::actingAs()
: -
If your route has just one guard:
- And on your tests just pass your entity, scopes and guard to
Passport::actingAs()
:
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, [], '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]'
]
]);
}
}