Open developcreativo opened 8 years ago
Archive GuardServiceProvider.php in App\Providers
<?php
/**
* Created by PhpStorm.
* User: vdjke
* Date: 10/28/2016
* Time: 6:31 p.m.
*/
namespace App\Providers;
use Dingo\Api\Routing\Route;
use Illuminate\Http\Request;
use Illuminate\Auth\AuthManager;
use Dingo\Api\Auth\Provider\Authorization;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
class GuardServiceProvider extends Authorization
{
/**
* Illuminate authentication manager.
*
* @var \Illuminate\Contracts\Auth\Guard
*/
protected $auth;
/**
* The guard driver name.
*
* @var string
*/
protected $guard = 'api';
/**
* Create a new basic provider instance.
*
* @param \Illuminate\Auth\AuthManager $auth
*/
public function __construct(AuthManager $auth)
{
$this->auth = $auth->guard($this->guard);
}
/**
* Authenticate request with a Illuminate Guard.
*
* @param \Illuminate\Http\Request $request
* @param \Dingo\Api\Routing\Route $route
*
* @return mixed
*/
public function authenticate(Request $request, Route $route)
{
if (! $user = $this->auth->user()) {
throw new UnauthorizedHttpException(
get_class($this),
'Unable to authenticate with invalid API key and token.'
);
}
return $user;
}
/**
* Get the providers authorization method.
*
* @return string
*/
public function getAuthorizationMethod()
{
return 'Bearer';
}
}
config\api.php
'auth' => [
'Guard' => \App\Providers\GuardServiceProvider::class
],
@vdjkelly Are there any other changes you made to get dingo to work with passport? My user keeps returning null, but I'm not sure why. I am trying to use the api for internal request with passport as the auth provider
@Jmrich
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class
],
'api' => [
'auth:api',
'api.auth',
'throttle:60,1',
'bindings',
],
];
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
$api->group(['namespace' => 'App\Http\Controllers\Api'], function($api){
$api->group(['middleware' => ['api']], function($api){
$api->post('comment/store', 'ApiCommentController@store');
});
});
});
Hi @vdjkelly , your provider work but I can't access to Auth::user() after that in my controller, it returns null.
### route.php ###
...
$api->group(['middleware'=>'api:auth'],function($api){
$api->get('/me', 'UserController@index');
$api->put('/me', 'UserController@update');
});
...
### kernel.php ###
...
'api' => [
'throttle:60,1',
'bindings',
],
'api:auth' => [
'auth:api',
'api.auth'
],
...
### UserController.php ###
...
$user = Auth::user();
dd($user); <-- returns null
...
Ok, just need to call first api.auth
:
'api:auth' => [
'api.auth',
'auth:api'
],
I made a similar effort, in case anyone is interested the details are here - https://github.com/dingo/api/issues/1159
Hi guys, the thread is quite old, but I am currently strugling on the same solution. One problem is that when i extend from Authorization
I get
[Symfony\Component\Debug\Exception\FatalThrowableError]
Call to undefined method App\Providers\PassportDingoProvider::isDeferred()
Is anybody aware what has changed ? Thanks
Use laravel Passport with Api Dingo for email and password authentication With