Skip to content

Commit e137afd

Browse files
committed
feat: work on logout
로그아웃 기능 추가
1 parent c4001b2 commit e137afd

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

.eslintrc.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ module.exports = {
1313
node: true,
1414
},
1515
rules: {
16+
'jsx-a11y/no-noninteractive-element-interactions': [
17+
'error',
18+
{
19+
handlers: [
20+
// 'onClick',
21+
'onMouseDown',
22+
'onMouseUp',
23+
'onKeyPress',
24+
// 'onKeyDown',
25+
'onKeyUp',
26+
],
27+
},
28+
],
1629
'react/jsx-filename-extension': [
1730
'error',
1831
{

src/components/common/Sidebar.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { useSelector, useDispatch } from 'react-redux';
2+
import { useHistory } from 'react-router';
13
import styled from 'styled-components';
24
import Logo from '../../assets/common/sidebar_logo.svg';
35
import Logout from '../../assets/common/sidebar_logout.svg';
46
import MenuTab from './MenuTab';
7+
import { logout } from '../../modules/auth/user';
58

69
const StyledSidebarWrapper = styled.div`
710
position: fixed;
@@ -26,11 +29,28 @@ const StyledSidebarWrapper = styled.div`
2629
& > .logout-wrapper {
2730
position: absolute;
2831
bottom: 40px;
32+
& > img {
33+
cursor: pointer;
34+
}
2935
}
3036
}
3137
`;
3238

3339
const Sidebar = () => {
40+
const { token } = useSelector(({ user }) => ({
41+
token: user.token,
42+
}));
43+
const dispatch = useDispatch();
44+
const history = useHistory();
45+
const handleLogout = () => {
46+
dispatch(logout());
47+
};
48+
49+
if (!token) {
50+
localStorage.removeItem('userToken');
51+
history.push('/');
52+
}
53+
3454
return (
3555
<StyledSidebarWrapper>
3656
<div className="sidebar">
@@ -42,7 +62,12 @@ const Sidebar = () => {
4262
<MenuTab menu="TodayILearned" />
4363
</div>
4464
<div className="logout-wrapper">
45-
<img src={Logout} alt="sidebar-logout" />
65+
<img
66+
src={Logout}
67+
alt="sidebar-logout"
68+
onClick={handleLogout}
69+
onKeyDown={handleLogout}
70+
/>
4671
</div>
4772
</div>
4873
</StyledSidebarWrapper>

src/lib/api/auth.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,12 @@ export const changePassword = ({ email, firstPassword, secondPassword }) => {
3737
new_password2: secondPassword,
3838
});
3939
};
40+
export const logout = () => {
41+
return client({
42+
method: 'post',
43+
url: '/v1.0/accounts/api/logout/',
44+
headers: {
45+
Authorization: `Token ${localStorage.getItem('userToken')}`,
46+
},
47+
});
48+
};

src/lib/util/saga.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export default function createRequestSaga(
1212
// eslint-disable-next-line no-unused-vars
1313
sideEffectFn = (actionType, response) => {}
1414
) {
15-
console.log('여기 들어왔나?');
1615
const SUCCESS = `${type}_SUCCESS`;
1716
const FAILURE = `${type}_FAILURE`;
1817

src/modules/auth/user.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const [LOGIN, LOGIN_SUCCESS, LOGIN_FAILURE] =
1111
const [CHANGE_PASSWORD, CHANGE_PASSWORD_SUCCESS, CHANGE_PASSWORD_FAILURE] =
1212
createRequestActionTypes('user/CHANGE_PASSWORD');
1313
const INIT_IS_PASSWORD_CHANGED = 'user/INIT_IS_PASSWORD_CHANGED';
14+
const [LOGOUT, LOGOUT_SUCCESS, LOGOUT_FAILURE] =
15+
createRequestActionTypes('user/LOGOUT');
1416

1517
export const register = (
1618
email,
@@ -46,6 +48,9 @@ export const changePassword = (email, firstPassword, secondPassword) => ({
4648
export const initIsPasswordChanged = () => ({
4749
type: INIT_IS_PASSWORD_CHANGED,
4850
});
51+
export const logout = () => ({
52+
type: LOGOUT,
53+
});
4954

5055
const registerSaga = createRequestSaga(
5156
REGISTER,
@@ -69,10 +74,13 @@ const changePasswordSaga = createRequestSaga(
6974
CHANGE_PASSWORD,
7075
authAPI.changePassword
7176
);
77+
const logoutSaga = createRequestSaga(LOGOUT, authAPI.logout);
78+
7279
export function* userSagas() {
7380
yield takeLatest(REGISTER, registerSaga);
7481
yield takeLatest(LOGIN, loginSaga);
7582
yield takeLatest(CHANGE_PASSWORD, changePasswordSaga);
83+
yield takeLatest(LOGOUT, logoutSaga);
7684
}
7785

7886
const initialState = {
@@ -123,6 +131,18 @@ const user = (state = initialState, action) => {
123131
...state,
124132
isPasswordChanged: false,
125133
};
134+
case LOGOUT_SUCCESS:
135+
return {
136+
...state,
137+
status: action.payload.status,
138+
token: '',
139+
};
140+
case LOGOUT_FAILURE:
141+
return {
142+
...state,
143+
status: action.payload.status,
144+
error: action.payload.data,
145+
};
126146
default:
127147
return state;
128148
}

0 commit comments

Comments
 (0)