Switch react-cookie to universal-cookie
This commit is contained in:
parent
92ef06cafc
commit
ed0903528f
3 changed files with 25 additions and 8 deletions
|
@ -1,4 +1,5 @@
|
|||
import React, { Component } from 'react';
|
||||
import Cookies from 'universal-cookie';
|
||||
|
||||
import { connect } from 'react-redux';
|
||||
import { userLogin } from '../actions';
|
||||
|
@ -62,6 +63,7 @@ class AuthMenu extends Component {
|
|||
}
|
||||
|
||||
handleLogin() {
|
||||
const cookies = new Cookies();
|
||||
this.props.userLogin(this.state.username, this.state.password)
|
||||
.then(res => {
|
||||
this.setState(state => ({
|
||||
|
@ -70,6 +72,7 @@ class AuthMenu extends Component {
|
|||
this.props.close()
|
||||
}
|
||||
)
|
||||
.then(console.log(cookies.getAll()))
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
})
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import { CookiesProvider } from 'react-cookie';
|
||||
import { Provider } from 'react-redux';
|
||||
import { createStore, applyMiddleware } from 'redux';
|
||||
import thunk from 'redux-thunk'
|
||||
|
@ -11,10 +10,8 @@ import reducers from './reducers';
|
|||
const store = createStore(reducers, applyMiddleware(thunk));
|
||||
|
||||
ReactDOM.render(
|
||||
<CookiesProvider>
|
||||
<Provider store={store}>
|
||||
<App />
|
||||
</Provider>
|
||||
</CookiesProvider>,
|
||||
<Provider store={store}>
|
||||
<App />
|
||||
</Provider>,
|
||||
document.querySelector('#root')
|
||||
);
|
||||
|
|
|
@ -1,14 +1,31 @@
|
|||
import Cookies from 'universal-cookie';
|
||||
|
||||
const cookies = new Cookies();
|
||||
|
||||
const initialState = {
|
||||
user_logged_in: false,
|
||||
username: '',
|
||||
jwt: ''
|
||||
};
|
||||
|
||||
|
||||
|
||||
export default (state = initialState, action) => {
|
||||
switch (action.type) {
|
||||
case 'USER_LOGIN':
|
||||
const full_jwt = action.payload.data.jwt
|
||||
const jwt_split = full_jwt.split('.')
|
||||
const jwt_signature = jwt_split.pop()
|
||||
const jwt_header_payload = jwt_split.join('.')
|
||||
cookies.set("jwt.signature", jwt_signature,
|
||||
{
|
||||
path: '/',
|
||||
httpOnly: true
|
||||
})
|
||||
cookies.set("jwt.header_payload", jwt_header_payload,
|
||||
{
|
||||
path: '/',
|
||||
})
|
||||
return {...state, ...{
|
||||
jwt: action.payload.data.jwt,
|
||||
username: action.payload.data.username,
|
||||
user_logged_in: true
|
||||
}}
|
||||
|
|
Reference in a new issue