diff --git a/src/actions/auth.js b/src/actions/auth.js index eecdebab3de10ac494dc29f21da369454f76454a..ba8f7d9db5bafbf3509856bb16d95aa31eee127d 100644 --- a/src/actions/auth.js +++ b/src/actions/auth.js @@ -1,5 +1,5 @@ import ax from 'axios'; -import { GET_USERDATA, PROFILE_CHANGE, GROUP_CHANGE } from './types'; +import { GET_USERDATA, PROFILE_CHANGE, GROUP_CHANGE, LOGOUT } from './types'; const axios = ax.create({ xsrfCookieName: 'csrftoken', @@ -45,3 +45,12 @@ export const submitRegistration = ({ } } ); + +export const logout = () => ( + async (dispatch) => { + const response = await axios.get('/api/v1/logout/'); + if (response) { + dispatch({ action: LOGOUT }); + } + } +); diff --git a/src/components/Header.js b/src/components/Header.js index c35d7b121e35d99841665bad9871345ea3453c28..e89414bcd288cf2b1998038b25bea21f387e05f0 100644 --- a/src/components/Header.js +++ b/src/components/Header.js @@ -9,7 +9,7 @@ import { Image, } from 'semantic-ui-react'; import { connect } from 'react-redux'; -import { getUserData } from '../actions'; +import { getUserData, logout } from '../actions'; import KSZKlogo from './images/kszk_logo.svg'; const FixedMenu = ({ user }) => ( @@ -31,7 +31,10 @@ const FixedMenu = ({ user }) => ( <Menu.Item className='item'> { user.id ? - <Button as={Link} to='/profile'>Profilom</Button> + <div> + <Button as={Link} to='/profile'>Profilom</Button> + <Button onClick={() => this.props.logout()}>KijelentkezĂŠs</Button> + </div> : <Button href='/api/v1/login/authsch/'>BejelentkezĂŠs</Button> } @@ -114,4 +117,4 @@ const mapStateToProps = ({ user }) => ({ user, }); -export default connect(mapStateToProps, { getUserData })(Header); +export default connect(mapStateToProps, { getUserData, logout })(Header); diff --git a/src/reducers/UserReducer.js b/src/reducers/UserReducer.js index 839a47eb435192c9bbac2d2f7a23c134a3bcaed6..607f6dca0c0b9c4bfc67cdd7bf5d4b25c0e3ef0e 100644 --- a/src/reducers/UserReducer.js +++ b/src/reducers/UserReducer.js @@ -2,6 +2,7 @@ import { GET_USERDATA, PROFILE_CHANGE, GROUP_CHANGE, + LOGOUT, } from '../actions/types'; const INITIAL_STATE = { @@ -15,6 +16,8 @@ export default (state = INITIAL_STATE, action) => { return { ...state, [action.target]: action.payload }; case GROUP_CHANGE: return { ...state, groups: action.payload }; + case LOGOUT: + return INITIAL_STATE; default: return state; }