我的Laravel + GatsbyJS應用程式使用auth:sanctum進行登入管理,其他用戶遇到伺服器500錯誤,但我的登入卻正常運行
P粉215292716
P粉215292716 2023-09-04 09:08:41
0
1
361

所以我有一個應用程式(後端使用laravel,前端使用GatsbyJS),我正在幫忙開發。 一個月前,所有用戶都能夠無問題地登入。但是我發現現在,所有使用者都無法在生產環境中登入(除了我)。

login.jsx檔

const formChanged = async (e) => { setError(false); e.preventDefault(); setSubmitting(true); let loginData = getValues(); let response = await login(loginData.email, loginData.password); setSubmitting(false); if (response.error) { setError(true); setValue('password', ''); } else { navigate('/app/idm/'); } };

let response = await login() 呼叫了一個名為login的方法,該方法位於api.js檔案中

api.js檔案

// 登入應用程式 export const login = async (email, password) => { // 發送請求 let response = await makeRequest('post', '/login', { email, password }); // 如果沒有錯誤,設定令牌和用戶 if (!response.error && isBrowser()) { localStorage.setItem('idm_token', response.data.access_token); let my_user = JSON.stringify(await me(response.data.access_token)); localStorage.setItem('idm_user', my_user); } return response; };

當我們傳遞電子郵件和密碼時,這些資料會進行驗證,此時,所有使用者都能夠產生令牌,沒有問題。

(僅供參考,產生sanctum令牌的程式碼) api.php檔

Route::post('/login', function(Request $request) { $login = $request->only('email', 'password'); if (Auth::attempt($login)) { $user = User::where('email', $request['email'])->firstOrFail(); $token = $user->createToken('auth_token')->plainTextToken; return response()->json([ 'access_token' => $token, 'token_type' => 'Bearer' ]); } return response()->json(["message" => "認證失敗"], 401); })->name('api.login');

問題似乎出現在訪問目前受auth:sanctum保護的路由上。再次強調,所有用戶都能夠產生令牌,但只有我的登入詳細資訊允許我存取該路由。 其他所有用戶都會收到伺服器500錯誤。

這在我們嘗試獲取my_user詳細資訊時發生在api.js檔案中:

let my_user = JSON.stringify(await me(response.data.access_token));

我遇到的另一個問題是,我的生產環境中的laravel應用程式幾個月前停止輸出錯誤,我無法弄清楚如何修復生產環境中的錯誤日誌記錄問題(在開發環境中,錯誤日誌記錄正常)。

對於缺乏細節,我表示抱歉,我對這一切還很陌生,如果有任何提示或嘗試的事情,我將非常感激,即使我得不到答案,我也非常願意努力學習並解決這個問題。

P粉215292716
P粉215292716

全部回覆 (1)
P粉734486718

為了進一步排查問題,我決定查看為什麼我沒有收到錯誤日誌。

我決定將儲存資料夾及其內容設定為chmod 777

chmod -R 777 storage/

新增-R以遞歸地將內容設定為777

這實際上解決了我的登入問題,但我注意到將權限等級恢復為775後,有些使用者能夠再次登錄,但並非所有使用者。

然後我想,也許我的日誌檔案/資料夾有權限問題,也許這就是為什麼我沒有列印出錯誤日誌的原因?

所以我進一步研究了我的laravel.log檔。結果發現它只能由使用者(ubuntu:ubuntu)讀取。我決定將其群組更改為www-data

chown ubuntu:www-data laravel.log

這對我非常有幫助,我又能夠在laravel中記錄錯誤日誌了!

現在我看到了我的錯誤,大致如下:

production.ERROR: Unable to create lockable file: /var/www/main/backend/storage/framework/cache/data/d5/........etc...etcc

所以我檢查了儲存下的每個權限,發現我的data資料夾只能由使用者存取

我將www-data加入為data資料夾的群組:

chown ubuntu:www-data data

現在,我的問題已解決! (注意:我的chmod權限已恢復為775)

    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!