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 React, { Component } from 'react';
|
||||||
|
import Cookies from 'universal-cookie';
|
||||||
|
|
||||||
import { connect } from 'react-redux';
|
import { connect } from 'react-redux';
|
||||||
import { userLogin } from '../actions';
|
import { userLogin } from '../actions';
|
||||||
|
@ -62,6 +63,7 @@ class AuthMenu extends Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
handleLogin() {
|
handleLogin() {
|
||||||
|
const cookies = new Cookies();
|
||||||
this.props.userLogin(this.state.username, this.state.password)
|
this.props.userLogin(this.state.username, this.state.password)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
this.setState(state => ({
|
this.setState(state => ({
|
||||||
|
@ -70,6 +72,7 @@ class AuthMenu extends Component {
|
||||||
this.props.close()
|
this.props.close()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
.then(console.log(cookies.getAll()))
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
import { CookiesProvider } from 'react-cookie';
|
|
||||||
import { Provider } from 'react-redux';
|
import { Provider } from 'react-redux';
|
||||||
import { createStore, applyMiddleware } from 'redux';
|
import { createStore, applyMiddleware } from 'redux';
|
||||||
import thunk from 'redux-thunk'
|
import thunk from 'redux-thunk'
|
||||||
|
@ -11,10 +10,8 @@ import reducers from './reducers';
|
||||||
const store = createStore(reducers, applyMiddleware(thunk));
|
const store = createStore(reducers, applyMiddleware(thunk));
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<CookiesProvider>
|
<Provider store={store}>
|
||||||
<Provider store={store}>
|
<App />
|
||||||
<App />
|
</Provider>,
|
||||||
</Provider>
|
|
||||||
</CookiesProvider>,
|
|
||||||
document.querySelector('#root')
|
document.querySelector('#root')
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,14 +1,31 @@
|
||||||
|
import Cookies from 'universal-cookie';
|
||||||
|
|
||||||
|
const cookies = new Cookies();
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
user_logged_in: false,
|
user_logged_in: false,
|
||||||
username: '',
|
username: '',
|
||||||
jwt: ''
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default (state = initialState, action) => {
|
export default (state = initialState, action) => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case 'USER_LOGIN':
|
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, ...{
|
return {...state, ...{
|
||||||
jwt: action.payload.data.jwt,
|
|
||||||
username: action.payload.data.username,
|
username: action.payload.data.username,
|
||||||
user_logged_in: true
|
user_logged_in: true
|
||||||
}}
|
}}
|
||||||
|
|
Reference in a new issue