Reactjs 尝试访问laravel中的JWT认证用户内容
我有React前端和Laravel后端应用程序,它使用JWT对用户进行身份验证。登录过程工作正常,我可以使用JWT的me功能获得经过身份验证的用户。但现在,当我尝试基于经过身份验证的用户访问内容时,我得到了401错误。我正在添加我的react axios函数,还有我的laravel控制器,它具有给出错误的函数。请指出我做错了什么。非常感谢您的帮助。多谢各位 反应axios功能:Reactjs 尝试访问laravel中的JWT认证用户内容,reactjs,laravel,redux,jwt,Reactjs,Laravel,Redux,Jwt,我有React前端和Laravel后端应用程序,它使用JWT对用户进行身份验证。登录过程工作正常,我可以使用JWT的me功能获得经过身份验证的用户。但现在,当我尝试基于经过身份验证的用户访问内容时,我得到了401错误。我正在添加我的react axios函数,还有我的laravel控制器,它具有给出错误的函数。请指出我做错了什么。非常感谢您的帮助。多谢各位 反应axios功能: export const fetchMyApplications = () => { return as
export const fetchMyApplications = () => {
return async dispatch => {
const url = `/auth/my-applications`;
var formdata = new FormData();
const token = getCookie("userToken");
const response = await api
.get(
url,
{
headers: {
Authorization: `Bearer ${token}`
}
}
)
.then(res => {
return res;
})
.catch(error => {
return error.response;
});
dispatch({
type: "FETCH_MY_APLICATIONS",
payload: response
});
};
}
拉威尔控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\Models\Application;
class ApplyController extends Controller
{
public function __construct()
{
\Config::set('auth.providers.users.model', \App\Applicant::class);
$this->middleware('auth:api', ['except' =['personalDetails']]);
}
public function myApplications(){
try {
$user = auth()->userOrFail();
$applications = Application::where('applicant_id', $user->applicant_id)->with('course')->get();
return response()->json(['applications'=> $applications], 200);
}catch (\Tymon\JWTAuth\Exceptions\UserNotDefinedException $e) {
return response()->json(['status'=> false, 'errors'=> 'Unauthenticated User'], 401);
}
}
}
邮递员正在检查登录用户:
邮递员正在尝试获取登录用户的应用程序:
我发现了问题所在。实际上,这不是我的代码,而是linode服务器上的apache配置。我在apache配置文件中添加了授权头,它成功了
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
'hash' => false,
],
],