diff --git a/src/components/AuthMenu.js b/src/components/AuthMenu.js index 9ef009c..c8cfe24 100644 --- a/src/components/AuthMenu.js +++ b/src/components/AuthMenu.js @@ -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); }) diff --git a/src/index.js b/src/index.js index d00bcd2..2f8c633 100644 --- a/src/index.js +++ b/src/index.js @@ -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( - - - - - , + + + , document.querySelector('#root') ); diff --git a/src/reducers/authReducer.js b/src/reducers/authReducer.js index c24d51b..7ae725b 100644 --- a/src/reducers/authReducer.js +++ b/src/reducers/authReducer.js @@ -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 }}