簡體   English   中英

Laravel 中可選的身份驗證中間件

[英]Auth middleware optional in Laravel

我有我的 controller ExampleController

class ExampleController extends Controller
{
    function __construct()
    {
        $this->middleware('auth:student')->only(['store', 'update', 'destroy']);
    }

    public function index()
    {
        if(CheckUser::student()) {
            dd("Is student");
        }
        dd("Isn't student");
    }

    /**
     * Another method's not relevant.
     **/
}

如果是學生,我正在嘗試添加一些邏輯。 但是有一個問題,如果我設置中間件,我只能訪問: Auth::user() 但是這個特定的方法可以在沒有登錄的情況下訪問。

我的問題

如果記錄獲取用戶信息,是否可以創建一個不需要的中間件?

注意:我正在使用具有多重身份驗證的 Laravel 護照。

如果您想保護 controller 中的特定方法,並在同一 controller 中省略其他方法

您可以嘗試在路由文件(api.php 或 web.php)上保護您的端點,而不是在構造函數上

//protected routes
Route::group(['middleware' => 'auth:api'], function () {
    Route::post('customer/picture/add', 'Mobile\AuthController@addProfilePicture');
    Route::post('customer/phone/update', 'Mobile\AuthController@updatePhoneNumber');
});

//unprotected routes
Route::get('customer/login', 'Mobile\AuthController@getLoginForm');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM