From 9057ca1824f0d5ba32478dd1ead9f97a5e44c387 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 15 Jul 2018 09:48:11 +0300 Subject: localStorage -> cookies --- vnext/package.json | 1 + vnext/src/App.js | 10 ++++++---- vnext/src/api/index.js | 16 ++++++++-------- vnext/src/components/Types.js | 2 +- vnext/yarn.lock | 11 +++++++++++ 5 files changed, 27 insertions(+), 13 deletions(-) (limited to 'vnext') diff --git a/vnext/package.json b/vnext/package.json index 8397fe62..dd09ffcc 100644 --- a/vnext/package.json +++ b/vnext/package.json @@ -72,6 +72,7 @@ "query-string": "^6.1.0", "react": "^16.4.1", "react-content-loader": "^3.1.2", + "react-cookies": "^0.1.0", "react-dom": "^16.4.1", "react-router-dom": "^4.3.1" } diff --git a/vnext/src/App.js b/vnext/src/App.js index 14ae7d5e..20ece23e 100644 --- a/vnext/src/App.js +++ b/vnext/src/App.js @@ -15,6 +15,8 @@ import Avatar from './components/Avatar'; import Header from './components/Header'; import SearchBox from './components/SearchBox'; +import cookies from 'react-cookies'; + import { me } from './api'; export default class App extends React.Component { @@ -22,13 +24,13 @@ export default class App extends React.Component { super(props); let params = qs.parse(window.location.search); if (params.hash) { - localStorage.visitor = JSON.stringify({ uid: 0, hash: params.hash }); + cookies.save('hash', params.hash, { path: '/' }); window.history.replaceState({}, document.title, `${window.location.protocol}//${window.location.host}${window.location.pathname}`); } this.state = { - visitor: localStorage.visitor ? JSON.parse(localStorage.visitor) : { + visitor: { uid: 0, - hash: params.hash || '' + hash: cookies.load('hash') } }; this.pm = React.createRef(); @@ -87,7 +89,7 @@ export default class App extends React.Component { } componentDidMount() { - const { hash, uid } = this.state.visitor; + const { hash } = this.state.visitor; this.initWS(); if (hash) { me().then(visitor => this.auth(visitor)); diff --git a/vnext/src/api/index.js b/vnext/src/api/index.js index c9ae5688..efa57cf1 100644 --- a/vnext/src/api/index.js +++ b/vnext/src/api/index.js @@ -1,4 +1,5 @@ import axios from 'axios'; +import cookies from 'react-cookies'; const apiBaseUrl = 'https://api.juick.com'; @@ -6,11 +7,9 @@ const client = axios.create({ baseURL: apiBaseUrl }); client.interceptors.request.use(config => { - if (localStorage.visitor) { - config.params = Object.assign(config.params || {}, { - hash: JSON.parse(localStorage.visitor).hash - }); - } + config.params = Object.assign(config.params || {}, { + hash: cookies.load('hash') + }); return config; }); @@ -22,10 +21,11 @@ export function me(username = '', password = '') { 'Authorization': 'Basic ' + window.btoa(unescape(encodeURIComponent(username + ':' + password))) } : {} }).then(response => { - localStorage.visitor = JSON.stringify(response.data); - resolve(response.data); + let visitor = response.data; + cookies.save('hash', visitor.hash, { path: '/' }); + resolve(visitor); }).catch(reason => { - localStorage.clear(); + cookies.remove('hash', { path: '/'}); reject(reason); }); }); diff --git a/vnext/src/components/Types.js b/vnext/src/components/Types.js index f7177f6a..6596dc8e 100644 --- a/vnext/src/components/Types.js +++ b/vnext/src/components/Types.js @@ -2,7 +2,7 @@ import PropTypes from 'prop-types'; export const UserType = PropTypes.shape({ uid: PropTypes.number.isRequired, - uname: PropTypes.string.isRequired + uname: PropTypes.string }); export const MessageType = PropTypes.shape({ diff --git a/vnext/yarn.lock b/vnext/yarn.lock index 62773192..5ce11801 100644 --- a/vnext/yarn.lock +++ b/vnext/yarn.lock @@ -1832,6 +1832,10 @@ convert-source-map@^1.4.0, convert-source-map@^1.5.1: version "1.5.1" resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.5.1.tgz#b8278097b9bc229365de5c62cf5fcaed8b5599e5" +cookie@^0.3.1: + version "0.3.1" + resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.3.1.tgz#e7e0a1f9ef43b4c8ba925c5c5a96e806d16873bb" + cookies@~0.7.0: version "0.7.1" resolved "https://registry.yarnpkg.com/cookies/-/cookies-0.7.1.tgz#7c8a615f5481c61ab9f16c833731bcb8f663b99b" @@ -6065,6 +6069,13 @@ react-content-loader@^3.1.2: version "3.1.2" resolved "https://registry.yarnpkg.com/react-content-loader/-/react-content-loader-3.1.2.tgz#98230b4604b4b744eaa2d3fc88917dd988df6766" +react-cookies@^0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/react-cookies/-/react-cookies-0.1.0.tgz#6bb883f2d1a397f138a4110300b46fe25df8f1a9" + dependencies: + cookie "^0.3.1" + object-assign "^4.1.1" + react-dev-utils@^5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/react-dev-utils/-/react-dev-utils-5.0.1.tgz#1f396e161fe44b595db1b186a40067289bf06613" -- cgit v1.2.3