2017-12-22 23 views
0

私のリクエストにトークンを適用しようとしています。だから、私はconsole.logにリクエストの結果を試み、オブジェクト配列内にトークンが見つかりませんでした。Axiosリクエストがトークンを返さない

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. が要求ごとにトークンを使用したときにトークンを生成します(どのようにバックエンドトークンの有効性を確認?)

は、任意のヘルプは、バックエンドは、あなたがどちらかを使用すべきAPIリクエストを処理する方法に応じて、

答えて

0

をいただければ幸いですを追加し、トークンをURLに追加するか、axios.postを使用して、オブジェクトをトークンのあるメソッドに本体として渡します。 など。

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); 
}); 

あなたはReduxのを使用している場合、永続ストレージまたはReduxの状態でAsyncStorageで保存要求ごとにトークンを使用します。

サーバー側では、ユーザーの認証とデータベースに格納されたデータの検証に必要なすべてのデータを含むJSON Webトークンを生成できます。 PHPを使用しているため、PHPと組み合わされたJSON Webトークンの紹介をお勧めします:https://www.sitepoint.com/php-authorization-jwt-json-web-tokens/

+0

こんにちは、私はバックエンドでトークンを返すことができますか?あなたの例から、それは私が生成されていない自分のトークンを作成する必要があるように見えます。生成されたトークンを作成するにはどうすればよいですか? –

+0

@KevinGozali私は今あなたのニーズを満たす必要があります私の答えを更新;) – Marco

関連する問題