Axios请求不返回令牌

时间:2017-12-22 05:48:39

标签: authentication react-native jwt access-token

我试图为我的请求申请令牌。所以我尝试了console.log请求的结果,并且在对象数组中找不到任何令牌。

Object {
  "config": Object {
    "adapter": [Function xhrAdapter],
    "data": "ktp=3578270708950002&member=199508070003",
    "headers": Object {
      "Accept": "application/json, text/plain, */*",
      "Content-Type": "application/x-www-form-urlencoded",
    },
    "maxContentLength": -1,
    "method": "post",
    "timeout": 0,
    "transformRequest": Object {
      "0": [Function transformRequest],
    },
    "transformResponse": Object {
      "0": [Function transformResponse],
    },
    "url": "http://103.53.10.122/mobile/LoginCheck.php",
    "validateStatus": [Function validateStatus],
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
  },
  "data": Array [
    Object {
      "status": "67457",
    },
  ],
  "headers": Object {
    "connection": "keep-alive",
    "content-type": "text/html; charset=UTF-8",
    "date": "Fri, 22 Dec 2017 05:38:21 GMT",
    "server": "nginx",
    "transfer-encoding": "chunked",
    "vary": "Accept-Encoding",
  },
  "request": XMLHttpRequest {
    "DONE": 4,
    "HEADERS_RECEIVED": 2,
    "LOADING": 3,
    "OPENED": 1,
    "UNSENT": 0,
    "_aborted": false,
    "_cachedResponse": undefined,
    "_hasError": false,
    "_headers": Object {
      "accept": "application/json, text/plain, */*",
      "content-type": "application/x-www-form-urlencoded",
    },
    "_incrementalEvents": false,
    "_lowerCaseResponseHeaders": Object {
      "connection": "keep-alive",
      "content-type": "text/html; charset=UTF-8",
      "date": "Fri, 22 Dec 2017 05:38:21 GMT",
      "server": "nginx",
      "transfer-encoding": "chunked",
      "vary": "Accept-Encoding",
    },
    "_method": "POST",
    "_requestId": null,
    "_response": "[{\"status\":\"67457\"}]",
    "_responseType": "",
    "_sent": true,
    "_subscriptions": Array [],
    "_timedOut": false,
    "_trackingName": "unknown",
    "_url": "http://103.53.10.122/mobile/LoginCheck.php",
    "readyState": 4,
    "responseHeaders": Object {
      "Connection": "keep-alive",
      "Content-Type": "text/html; charset=UTF-8",
      "Date": "Fri, 22 Dec 2017 05:38:21 GMT",
      "Server": "nginx",
      "Transfer-Encoding": "chunked",
      "Vary": "Accept-Encoding",
    },
    "responseURL": "http://103.53.10.122/mobile/LoginCheck.php",
    "status": 200,
    "timeout": 0,
    "upload": XMLHttpRequestEventTarget {},
    "withCredentials": true,
  },
  "status": 200,
  "statusText": undefined,
}

有人可以指出我如何添加令牌进行身份验证,因为即使在阅读之后我仍然会对此概念感到困惑。所以,如果我没有弄错的话,我应该按顺序执行以下操作:

  1. 用户成功登录时生成令牌
  2. 将令牌保存在本地存储中
  3. 为每个请求使用令牌(后端如何检查令牌的有效性?)
  4. 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

根据后端处理API请求的方式,您应该使用axios.get并将令牌附加到URL或使用axios.post并使用您的令牌将对象作为正文传递给方法。 E.g。

axios.post('http://103.53.10.122/mobile/LoginCheck.php', {
    username: "test",
    password: "1234"
})
.then((res) => {
     console.log(res);
     /*
    In this example I assume that res.data has the token returned from the backend
    The res.data should look like this then:
    {
        token: "1234"
    }
     */
    let token = res.data.token;
    AsyncStorage.setItem("token", token);
})
.catch((err) => {
    console.log(err);
});

要为每个请求使用令牌,请将其保存在AsyncStorage中以进行持久存储,如果使用redux则将其保存在redux状态。

在服务器端,您可以生成一个JSON Web令牌,其中包含验证用户所需的所有数据,并使用存储在数据库中的数据对其进行验证。由于您使用的是PHP,我可以向您推荐这个与PHP结合使用的JSON Web令牌简介:https://www.sitepoint.com/php-authorization-jwt-json-web-tokens/