簡體   English   中英

無法使用Laravel 5.8和Auth0獲得身份驗證的用戶

[英]Unable to get authenticated user using Laravel 5.8 and Auth0

我有一個想使用Auth0保護的Laravel 5.8 API。 到目前為止,我已經遵循了本教程的每個步驟:

  1. 在正面,Login / logout鏈接當前在Blade中實現,盡管頁面上的呈現內容是使用Vue Router完成的,向數據API提出AJAX請求,但這仍然可以正常工作。
  2. Laravel中的默認User模型已被修改為按照教程存儲namesubemail ,並且也會填充它。
  3. 使用在教程中創建的jwt中間件來保護API端點,並且我可以在Postman中成功提交GET和硬編碼的 Bearer auth令牌,並獲得良好的響應。

但是,在某個時候,我希望能夠將訪問令牌傳遞給Vue,以便它可以完成其工作,但是我無法獲得當前經過身份驗證的用戶。 擊中Auth0后,它使用URL中的auth gobbledlygook重定向回我的回調路由。 該路由反過來加載一個controller方法 ,那里的一切看起來都不錯:

// Get the user related to the profile
$auth0User = $this->userRepository->getUserByUserInfo($profile);   // returns good user

if ($auth0User) {
    // If we have a user, we are going to log them in, but if
    // there is an onLogin defined we need to allow the Laravel developer
    // to implement the user as they want an also let them store it.
    if ($service->hasOnLogin()) {                                  // returns false
        $user = $service->callOnLogin($auth0User);
    } else {
        // If not, the user will be fine
        $user = $auth0User;
    }
    \Auth::login($user, $service->rememberUser());     // "normal" Laravel login flow?
}

我不是該框架的專家,但是上面的最后一行似乎開始了“正常”的Laravel用戶登錄流程。 鑒於此,當我執行auth()->user()甚至app('auth0')->getUser()時,是否應該看到null以外的內容?

如果您是初學者,請嘗試使用簡單的教程,我建議您這樣做

它使用一個簡單的JWT包來創建一個jwt令牌,當用戶進行身份驗證時可以獲取該令牌。

JWTAuth::attempt(['email'=>$email,'password'=>$password]);

暫無
暫無

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

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