我正在使用laravel为React前端构建api身份验证。 问题是当我用Insomnia到/ auth进行发布时,我正确地获得了响应。但是当我在React中使用axios进行操作时,响应返回null。
LARAVEL
public function store(AuthRequest $request)
{
$credentials = $this->authService->getCredentials($request->all());
if (!$token = auth('api')->attempt($credentials)) {
return $this->errorResponse("Unauthorized", 401);
}
$tokenData = $this->authService->getTokenData($token);
return response()->json($tokenData);
}
反应
export function* signIn({ payload }) {
const { email, password } = payload;
const response = yield call(api.post, 'auth', {
email,
password,
});
console.tron.log(response);
const { token, user } = response.data;
yield put(signInSuccess(token, user));
history.push('home');
}
INSOMNIA
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3Q6ODAwMFwvYXBpXC9hdXRoIiwiaWF0IjoxNTc2NTg0Nzc1LCJleHAiOjE1NzY1ODgzNzUsIm5iZiI6MTU3NjU4NDc3NSwianRpIjoiSGlnZlNvcVN1OWZaU3AwUyIsInN1YiI6MSwicHJ2IjoiMjNiZDVjODk0OWY2MDBhZGIzOWU3MDFjNDAwODcyZGI3YTU5NzZmNyJ9.SK4c2sm7mVnmZVAqkujYhcPWDHKzmGUjUNzoOsQx7eg",
"token_type": "bearer",
"expires_in": 3600
}
AXIOS
data:""
status:200
statusText:OK
headers:
url:http://localhost:8000/api/auth
method:post
data:{"email":"admin@admin.com","password":"321321"}
baseURL:http://localhost:8000/api
timeout:0
adapter: xhrAdapter()
xsrfCookieName:XSRF-TOKEN
xsrfHeaderName:X-XSRF-TOKEN
maxContentLength:-1
validateStatus: validateStatus()
request:{}
有人可以帮助我吗?