2023-11-17 01:44:01 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
2023-12-04 07:40:49 -05:00
|
|
|
use App\Exceptions\ApiException;
|
2023-11-17 01:44:01 -05:00
|
|
|
use Closure;
|
|
|
|
use App\Models\User;
|
2025-01-06 12:20:11 -05:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2023-11-17 01:44:01 -05:00
|
|
|
|
|
|
|
class Client
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
2025-01-06 12:20:11 -05:00
|
|
|
$token = $request->input('token', $request->route('token'));
|
2023-11-17 01:44:01 -05:00
|
|
|
if (empty($token)) {
|
2023-12-06 15:01:32 -05:00
|
|
|
throw new ApiException('token is null',403);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
$user = User::where('token', $token)->first();
|
|
|
|
if (!$user) {
|
2023-12-06 15:01:32 -05:00
|
|
|
throw new ApiException('token is error',403);
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
2025-01-06 12:20:11 -05:00
|
|
|
|
|
|
|
Auth::setUser($user);
|
2023-11-17 01:44:01 -05:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|