Laravel护照仅在初始加载时有效

时间:2018-11-03 07:16:04

标签: reactjs laravel laravel-passport

我有一种奇怪的情况,在我的componentWillMount中,我将getUserDetail调用到我的后端,并使用Laravel 5.7的护照功能很好地接收了数据。我还有一个测试按钮,该按钮在同一文件上调用getUserDetail,但返回401未经身份验证的错误。

getUserDetail() {
    this.props.getUserDetail((response) => {
        if (!response.success) {
            // window.location.href = '/logout';
        }
    });
}

componentWillMount() {
    this.getUserDetail();
    this.getUserDetail();
    this.getUserDetail();
}

handleTestBtnClick() {
    this.getUserDetail();
}

Screen shot

看起来成功和失败响应之间的标头确实不同

Request URL: http://abc.test/api/userDetails
Request Method: GET
Status Code: 200 OK
Remote Address: 127.0.0.1:80
Referrer Policy: no-referrer-when-downgrade
Cache-Control: no-cache, private
Connection: keep-alive
Content-Encoding: gzip
Content-Type: application/json
Date: Sat, 03 Nov 2018 07:10:26 GMT
Server: nginx/1.15.3
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Powered-By: PHP/7.2.10
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 57
Accept: application/json, text/plain, */*
 Accept-Encoding: gzip, deflate
 Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,zh-TW;q=0.6
 Connection: keep-alive
 Cookie: XDEBUG_SESSION=XDEBUG_ECLIPSE; laravel_token=eyJpdiI6IlZiUk1aa3VKbng5TVZ0bFlKZ0N4Zmc9PSIsInZhbHVlIjoiOXdjVTlpR1NTQ2hsQk9udGp2UDJ5NmYzSm9FVzd3NXBVNmNzSXhEVCtvdStYSjhwR0FNNldYRzhJQzlZdzNwUkg3anUyVktIczFadzk2OVE0NzBVWENqSkdwRTJwcmhRS2JMZXo5d1lvTzhLWlpjY1BoWG9zRHNJMkVWRHFpWFhOOEdWeG9yYkdselZtWeWdNVDMxVHVMMkprbloyaGp2b3JWcU9aMit4QWRCTDhuZlNYam8zMmFWN2VVXC9zSmh0VUxDcFhwbWJRdHNlUUpLYjAxVzZnd0pqaDJPQVpDZFZkK0NaVHh5MzBEZUR6UWpzUXFBNkZSNjNXNXYyNVRsOEUiLCJtYWMiOiI1ZjliOGM1Yzk4YjU4NWI1M2IyZDYzZjlmY2U4M2NkNzEzNzI4MzM5ODNmYmFkZmU0OTE4ZTUxNDA2OTZiZTE5In0%3D; XSRF-TOKEN=eyJpdiI6InhkNlNSbUlJb29WQU02dWtPRWNkMnc9PSIsInZhbHVlIjoia09xcUhkcWVLanp5Q0xUOEVFOWJXM1BqRG1tZ29DVEJMb2xlWU5iekRoeWtsaWhOZDhFZkNzQUVSNlwvR3JNdmIiLCJtYWMiOiI2MWRiYTBmYTRiMjI5OGE5ODQyMTEyODAwOTA4ZWMwYjFkZGExZjc0ZmE5NjU4OTE2MDQ3ZDQzYzkzZWYzZGEyIn0%3D; rice2_session=eyJpdiI6ImNmN0pMWDZFamg4d1ZoZ3BDQTNxbFE9PSIsInZhbHVlIjoiazR6TUd4UmNBUDA2djhCcnJqMDRnN0pRWWtHTkRXZEJvbm1PQXg0UnQ1S21hV2FCK3FvRzJMSjRRQnhTMnczdiIsIm1hYyI6IjVjZTg1MGYxODFmYzdhOWYxNmZmNGRhMTE5MmRiNWY0ZDZlZWRjNzBmZWFmNzUwNzRjMmRjYzRhYzY1NDE1ZTcifQ%3D%3D
 Host: rice2.test
 Referer: http://rice2.test/dashboard
 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
 X-CSRF-TOKEN: uOOKwiNYh1ZZeYjPo29nbxlojlmW1CvhFKTSmEu5e
 X-Requested-With: XMLHttpRequest
 X-XSRF-TOKEN: eyJpdiI6InhkNlNSbUlJb29WQU02dWtPRWNkMnc9PSIsInZhbHVlIjoia09xcUhkcWVLanp5Q0xUOEVFOWJXM1BqRG1tZ29DVEJMb2xlWU5iekRoeWtsaWhOZDhFZkNzQUVSNlwvR3JNdmIiLCJtYWMiOiI2MWRiYTBmYTRiMjI5OGE5ODQyMTEyODAwOTA4ZWMwYjFkZGExZjc0ZmE5NjU4OTE2MDQ3ZDQzYzkzZWYzZGEyIn0e=

失败

Request URL: http://abc.test/api/userDetails
Request Method: GET
Status Code: 401 Unauthorized
Remote Address: 127.0.0.1:80
Referrer Policy: no-referrer-when-downgrade
Cache-Control: no-cache, private
Connection: keep-alive
Content-Type: application/json
Date: Sat, 03 Nov 2018 07:10:58 GMT
Server: nginx/1.15.3
Transfer-Encoding: chunked
X-Powered-By: PHP/7.2.10
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 56
Accept: */*
 Accept-Encoding: gzip, deflate
 Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,zh-TW;q=0.6
 Connection: keep-alive
 Cookie: XDEBUG_SESSION=XDEBUG_ECLIPSE; laravel_token=eyJpdiI6IlZiUk1aa3VKbng5TVZ0bFlKZ0N4Zmc9PSIsInZhbHVlIjoiOXdjVTlpR1NTQ2hsQk9udGp2UDJ5NmYzSm9FVzd3NXBVNmNzSXhEVCtvdStYSjhwR0FNNldYRzhJQzlZdzNwUkg3anUyVktIczFadzk2OVE0NzBVWENqSkdwRTJwcmhRS2JMZXo5d1lvTzhLWlpjY1BoWG9zRHNJMkVWRHFpWFhOOEdWeG9yYkdselZetWWdNVDMxVHVMMkprbloyaGp2b3JWcU9aMit4QWRCTDhuZlNYam8zMmFWN2VVXC9zSmh0VUxDcFhwbWJRdHNlUUpLYjAxVzZnd0pqaDJPQVpDZFZkK0NaVHh5MzBEZUR6UWpzUXFBNkZSNjNXNXYyNVRsOEUiLCJtYWMiOiI1ZjliOGM1Yzk4YjU4NWI1M2IyZDYzZjlmY2U4M2NkNzEzNzI4MzM5ODNmYmFkZmU0OTE4ZTUxNDA2OTZiZTE5In0%3D; XSRF-TOKEN=eyJpdiI6InhkNlNSbUlJb29WQU02dWtPRWNkMnc9PSIsInZhbHVlIjoia09xcUhkcWVLanp5Q0xUOEVFOWJXM1BqRG1tZ29DVEJMb2xlWU5iekRoeWtsaWhOZDhFZkNzQUVSNlwvR3JNdmIiLCJtYWMiOiI2MWRiYTBmYTRiMjI5OGE5ODQyMTEyODAwOTA4ZWMwYjFkZGExZjc0ZmE5NjU4OTE2MDQ3ZDQzYzkzZWYzZGEyIn0%3D; rice2_session=eyJpdiI6ImNmN0pMWDZFamg4d1ZoZ3BDQTNxbFE9PSIsInZhbHVlIjoiazR6TUd4UmNBUDA2djhCcnJqMDRnN0pRWWtHTkRXZEJvbm1PQXg0UnQ1S21hV2FCK3FvRzJMSjRRQnhTMnczdiIsIm1hYyI6IjVjZTg1MGYxODFmYzdhOWYxNmZmNGRhMTE5MmRiNWY0ZDZlZWRjNzBmZWFmNzUwNzRjMmRjYzRhYzY1NDE1ZTcifQ%3D%3D
 Host: rice2.test
 Referer: http://rice2.test/dashboard
 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
 X-Requested-With: XMLHttpRequest
 X-XSRF-TOKEN: eyJpdiI6InhkNlNSbUlJb29WQU02dWtPRWNkMnc9PSIsInZhbHVlIjoia09xcUhkcWVLanp5Q0xUOEVFOWJXM1BqRG1tZ29DVEJMb2xlWU5iekRoeWtsaWhOZDhFZkNzQUVSNlwvR3JNdmIiLCJtYWMiOiI2MWRiYTBmYTRiMjI5OGE5ODQyMTEyODAwOTA4ZWMwYjFkZGExZjc0ZmE5NjU4OTE2MDQ3ZDQzYzkzZWYzZGEyIn0e=

-------更新-----

这是我用来调用backe的代码

//UserAction
function getUserDetail(callback) {
    return dispatch => {
        userService.getUserDetail()
            .then(
                response => {
                    if (response.data.success) {
                        dispatch({type: userConstants.USER_DATA_RECEIVED, payload: response.data.data});
                    } else {
                        callback({success: false});
                    }
                },
                error => {
                    // user may be logged out from the database.
                    console.log('Logging out because user failed to be authenticated');
                    callback({success: false});
                }
            );
    };
}
// UserService
function getUserDetail() {
    return axios.get('/api/userDetails')
        .then(response => {
            return response;
        })
}

1 个答案:

答案 0 :(得分:0)

发现了问题。 @TravisBritz向我指出了这是客户端问题的正确方向。

我盲目添加到主刀片文件中。我看到添加了,因为我在网上看到了它。

<div style="border-style: solid; 
            color: {{ $vehicule->adtype === 1 ? 'black' : 'blue' }} ">
    <img src="/images/{{ $upload->resized_name }}" height="150" width="250">
</div>

但是,此行已添加到bootstrap.js中。因此,我添加此行会覆盖导致令牌不存在的那部分。