<?php
namespace App\MyClass;
use AmoCRM\Client\AmoCRMApiClient;
use App\Models\UsersData;
use \Illuminate\Http\Request;
use League\OAuth2\Client\Token\AccessToken;
class GetAmoCRMApiClient {
private $request;
public function __construct(Request $request)
{
$this->request = $request;
}
public function get() {
$clientId = $this->request->bearerToken();
$userData = UsersData::where('client_id', $clientId)->first();
$amocrmTokens = $userData->amocrmTokens;
$config = config('amocrm');
$clientSecret = $config['clientSecret'];
$redirectUri = $config['redirectUri'];
$accessToken = new AccessToken([
'access_token' => $amocrmTokens['access_token'],
'refresh_token' => $amocrmTokens['refresh_token'],
'expires' => $amocrmTokens['expires_in'],
]);
$apiClient = new AmoCRMApiClient($clientId, $clientSecret, $redirectUri);
$apiClient->setAccessToken($accessToken);
$apiClient->setAccountBaseDomain($userData['referer']);
return $apiClient;
}
}
public function index(Request $request)
{
$apiClient = app(GetAmoCRMApiClient::class)->get();
try {
return $apiClient->pipelines()->get();
} catch (AmoCRMApiException $e) {
return $e;
}
}
protected $middlewarePriority = [
];
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' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
'json_responce' => JsonResponceMiddleware::class,
'auth_api' => AuthenticateApiMiddleware::class,
],
];
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Middleware\JsonResponceMiddleware;
Route::middleware('api')->group(function () {
Route::get('/array', function (Request $request) {
$x = [
'a' => 3,
'b' => 2,
];
return $x;
});
});
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class AuthenticateApiMiddleware extends Middleware
{
public function handle($request, Closure $next, ...$guards)
{
$request->headers->set('Accept', 'application/json');
return parent::handle($request, $next, $guards);
}
protected function authenticate($request, array $guards)
{
$token = $request->bearerToken();
if ($token === 'aaabbbccc') return;
$this->unauthenticated($request, $guards);
}
}
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class AuthenticateApiMiddleware extends Middleware
{
public function handle($request, Closure $next, ...$guards)
{
return parent::handle($request, $next, $guards);
}
protected function authenticate($request, array $guards)
{
$token = $request->bearerToken();
if ($token === 'aaabbbccc') return;
$this->unauthenticated($request, $guards);
}
protected function unauthenticated($request, array $guards)
{
parent::unauthenticated($request, $guards); // TODO: Change the autogenerated stub
}
}
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::middleware('auth_api')->get('/array', function (Request $request) {
$x = [
'a' => 3,
'b' => 2,
];
return $x;
});
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth_api' => AuthenticateApiMiddleware::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];