我正在訪問一個PayPalController直通路由,routes/api.php但是當我嘗試檢查用戶是否經過身份驗證時,它會回傳null.
PayPalController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Srmklive\PayPal\Service\Paypal;
class PayPalController extends Controller
{
public function create(Request $request)
{
// returns null
$id = Auth::id();
// can't read "id" of null
$id = auth('api')->user()->id;
}
}
routes/api.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::post('/paypal/order/create', [PayPalController::class, 'create']);
我試過創建一個api guardinconfig/auth.php并像這樣使用它:
auth('api')->user()->id
但它什么也沒改變。
編輯:
用戶已通過身份驗證,但仍回傳 null。
uj5u.com熱心網友回復:
它回傳 null,因為尚未對用戶進行身份驗證。如果要檢查用戶身份驗證,可以像這樣使用它:
If(Auth::check()){
//Your code here
}
或相反亦然。
uj5u.com熱心網友回復:
也許您可以在控制器的建構式中定義中間件
public function __construct()
{
$this->middleware('auth:api');
}
uj5u.com熱心網友回復:
=>your route will be add on middleware.
Route::group(['middleware' => 'auth:api'] ,function () {
Route::post('/paypal/order/create', [PayPalController::class, 'create']);
});
=>auth will be check this is login ya not and pass your login.if you check your api then pass the bearer token then run the api and access your $id = Auth::id(); auth id.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/506721.html
