1:version:
PHP Version | 7.2.8
Swoole Version | 4.2.10
Laravel Version | 5.5.44
Listen IP | 127.0.0.1
Listen Port | 5200
Server Status | Offline
Reactor Num | 6
Worker Num | 6
Task Worker Num | 6
Websocket Mode | On
PID | None
Log Path | /Users/noecs/Desktop/noecs-laravel-shop/storage/logs/swoole_http.log
2.Package:
"require": {
"php": ">=7.0.0",
"dingo/api": "2.0.0-alpha1",
"fideloper/proxy": "~3.3",
"gregwar/captcha": "dev-master",
"laravel/framework": "5.5.*",
"laravel/passport": "~4.0",
"laravel/tinker": "~1.0",
"liyu/dingo-serializer-switch": "dev-master",
"paragonie/random_compat": "^2.0",
"predis/predis": "v1.1.x-dev",
"swooletw/laravel-swoole": "dev-master"
},
3.Question: Forgive me for using Google Translate to communicate with you. The above API package I use wants to get authorized users in the websocket.
The access_token is transferred to the backend websocket server in the address bar. Because the passport needs the Authorization of the header to authenticate the user, I declare the middleware in the websocket configuration, and change the access_token passed in the address bar to the Authorization of the header. Secondly, I am trying to use the middleware Authenticate that comes with Swooletw. When I get the user information, it is always null. I wrote a middleware myself, the code is as follows:
<?php
Namespace App\Http\Middleware;
Use Closure;
Use Illuminate\Auth\AuthManager;
Use Illuminate\Auth\AuthenticationException;
Use Illuminate\Http\Request;
/**
聽* Class Authenticate
聽*/
Class WebSocketAuthorization
{
聽聽聽聽Protected $auth;
聽聽聽聽Protected $guard = 'api';
聽聽聽聽Public function __construct(Request $request, AuthManager $auth)
聽聽聽聽{
聽聽聽聽聽聽聽聽Var_dump($request->headers->get('Authorization'));
聽聽聽聽聽聽聽聽$this->auth = $auth->guard($this->guard);
聽聽聽聽聽聽聽聽Var_dump($this->auth->user());
聽聽聽聽}
聽聽聽聽/**
聽聽聽聽聽* Handle an incoming request.
聽聽聽聽聽*
聽聽聽聽聽* @param \Illuminate\Http\Request $request
聽聽聽聽聽* @param \Closure $next
聽聽聽聽聽*
聽聽聽聽聽* @return mixed
聽聽聽聽聽*
聽聽聽聽聽*/
聽聽聽聽Public function handle($request, Closure $next)
聽聽聽聽{
聽聽聽聽聽聽聽聽Try {
聽聽聽聽聽聽聽聽聽聽聽聽If ($user = $this->auth->user()) {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽$request->setUserResolver(function () use ($user) {
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽Return $user;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽});
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽} catch (AuthenticationException $e) {
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽Return $next($request);
聽聽聽聽}
}
Still unable to get user information. An error occurred while printing $this->auth->user(): local.ERROR: Auth guard driver [api] is not defined. {"exception":"[object] (InvalidArgumentException(code: 0) : Auth guard driver [api] is not defined. at /Users/noecs/Desktop/noecs-laravel-shop/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php:97)
[stacktrace]
I noticed that the package dingo/api is installed in your project. Please see #137 and #149 for the reference.
@albertcht
Thank you for your help, I found the problem. I added the following configuration in the configuration file config/swoole_http.php to solve my problem.
/*
|--------------------------------------------------------------------------
| Instances here will be cleared on every request.
|--------------------------------------------------------------------------
*/
'instances' => [
//Reset http request to prevent singleton
'auth',
],
/*
|--------------------------------------------------------------------------
| Providers here will be registered on every request.
|--------------------------------------------------------------------------
*/
'providers' => [
Illuminate\Pagination\PaginationServiceProvider::class,
//Solve the error:`Auth guard driver [api] is not defined.`
Laravel\Passport\PassportServiceProvider::class
],
The above code has made my project run normally. Although I don't know what the principle is.
Most helpful comment
@albertcht
Thank you for your help, I found the problem. I added the following configuration in the configuration file
config/swoole_http.phpto solve my problem.The above code has made my project run normally. Although I don't know what the principle is.