From 1b96f53acf69fdf5b70a3608e50580ff4f695e8a Mon Sep 17 00:00:00 2001 From: w-arantes Date: Thu, 26 Mar 2020 14:23:20 -0300 Subject: [PATCH] fix: auth logOut & persist --- package.json | 2 +- src/components/Header/index.js | 17 ++++++++--------- src/components/Header/styles.js | 7 ------- src/pages/Profile/index.js | 10 +++++----- src/store/modules/auth/actions.js | 2 +- src/store/modules/auth/reducer.js | 6 +++--- src/store/modules/auth/sagas.js | 6 +++--- src/store/modules/user/actions.js | 2 +- src/store/modules/user/reducer.js | 4 ++-- yarn.lock | 2 +- 10 files changed, 25 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index 5c4cac1..4eaef4a 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "axios": "^0.19.2", "date-fns": "^2.0.0-beta.5", "date-fns-tz": "^1.0.7", - "history": "^4.9.0", + "history": "^4.10.1", "immer": "^6.0.2", "polished": "^3.5.1", "prop-types": "^15.7.2", diff --git a/src/components/Header/index.js b/src/components/Header/index.js index 6986fee..4fc8355 100644 --- a/src/components/Header/index.js +++ b/src/components/Header/index.js @@ -2,11 +2,11 @@ import React from 'react'; import { useSelector } from 'react-redux'; import { Link } from 'react-router-dom'; +import Notifications from '~/components/Notifications'; + import logo from '~/assets/logo-purple.svg'; import { Container, Content, Profile } from './styles'; -import Notifications from '../Notifications'; - export default function Header() { const profile = useSelector(state => state.user.profile); @@ -14,25 +14,24 @@ export default function Header() { diff --git a/src/components/Header/styles.js b/src/components/Header/styles.js index 6d1b9bb..023a622 100644 --- a/src/components/Header/styles.js +++ b/src/components/Header/styles.js @@ -12,23 +12,19 @@ export const Content = styled.div` height: 64px; max-width: 900px; margin: 0 auto; - nav { display: flex; align-items: center; - img { margin-right: 20px; padding-right: 20px; border-right: 1px solid #eee; } - a { font-weight: bold; color: #7159c1; } } - aside { display: flex; align-items: center; @@ -40,16 +36,13 @@ export const Profile = styled.div` margin-left: 20px; padding-left: 20px; border-left: 1px solid #eee; - div { text-align: right; margin-right: 10px; - strong { display: block; color: #333; } - a { display: block; margin-top: 2px; diff --git a/src/pages/Profile/index.js b/src/pages/Profile/index.js index 6d2a21f..c7bb38b 100644 --- a/src/pages/Profile/index.js +++ b/src/pages/Profile/index.js @@ -1,8 +1,8 @@ import React from 'react'; -import { useSelector, useDispatch } from 'react-redux'; +import { useDispatch, useSelector } from 'react-redux'; import { Form, Input } from '@rocketseat/unform'; -import { signOut } from '~/store/modules/auth/actions'; +import { logOut } from '~/store/modules/auth/actions'; import { updateProfileRequest } from '~/store/modules/user/actions'; import AvatarInput from './AvatarInput'; @@ -17,8 +17,8 @@ export default function Profile() { dispatch(updateProfileRequest(data)); } - function handleSignOut() { - dispatch(signOut()); + function handlelogOut() { + dispatch(logOut()); } return ( @@ -45,7 +45,7 @@ export default function Profile() { - diff --git a/src/store/modules/auth/actions.js b/src/store/modules/auth/actions.js index 844fbfa..f37fd70 100644 --- a/src/store/modules/auth/actions.js +++ b/src/store/modules/auth/actions.js @@ -25,7 +25,7 @@ export function signFailure() { }; } -export function signOut() { +export function logOut() { return { type: '@auth/SIGN_OUT', }; diff --git a/src/store/modules/auth/reducer.js b/src/store/modules/auth/reducer.js index 37280eb..3a9193b 100644 --- a/src/store/modules/auth/reducer.js +++ b/src/store/modules/auth/reducer.js @@ -9,11 +9,11 @@ const INITIAL_STATE = { export default function auth(state = INITIAL_STATE, action) { return produce(state, draft => { switch (action.type) { - case '@auth/LOG_IN_REQUEST': { + case '@auth/SIGN_IN_REQUEST': { draft.loading = true; break; } - case '@auth/LOG_IN_SUCCESS': { + case '@auth/SIGN_IN_SUCCESS': { draft.token = action.payload.token; draft.signed = true; draft.loading = false; @@ -23,7 +23,7 @@ export default function auth(state = INITIAL_STATE, action) { draft.loading = false; break; } - case '@auth/LOG_OUT': { + case '@auth/SIGN_OUT': { draft.token = null; draft.signed = false; break; diff --git a/src/store/modules/auth/sagas.js b/src/store/modules/auth/sagas.js index 54dfaa8..d5ebf1d 100644 --- a/src/store/modules/auth/sagas.js +++ b/src/store/modules/auth/sagas.js @@ -4,7 +4,7 @@ import { toast } from 'react-toastify'; import api from '~/services/api'; import history from '~/services/history'; -import { signInSuccess, signFailure, signOut } from './actions'; +import { signInSuccess, signFailure } from './actions'; export function* signIn({ payload }) { try { @@ -62,7 +62,7 @@ export function setToken({ payload }) { } } -export function signOut() { +export function logOut() { history.push('/'); } @@ -70,5 +70,5 @@ export default all([ takeLatest('persist/REHYDRATE', setToken), takeLatest('@auth/SIGN_IN_REQUEST', signIn), takeLatest('@auth/SIGN_UP_REQUEST', signUp), - takeLatest('@auth/SIGN_OUT', signOut), + takeLatest('@auth/SIGN_OUT', logOut), ]); diff --git a/src/store/modules/user/actions.js b/src/store/modules/user/actions.js index e0a4dc6..f6df118 100644 --- a/src/store/modules/user/actions.js +++ b/src/store/modules/user/actions.js @@ -5,7 +5,7 @@ export function updateProfileRequest(data) { }; } -export function updateProfileSuccess(data, profile) { +export function updateProfileSuccess(profile) { return { type: '@user/UPDATE_PROFILE_SUCCESS', payload: { profile }, diff --git a/src/store/modules/user/reducer.js b/src/store/modules/user/reducer.js index 7f261a0..f9906be 100644 --- a/src/store/modules/user/reducer.js +++ b/src/store/modules/user/reducer.js @@ -7,7 +7,7 @@ const INITIAL_STATE = { export default function user(state = INITIAL_STATE, action) { return produce(state, draft => { switch (action.type) { - case '@auth/LOG_IN_SUCCESS': { + case '@auth/SIGN_IN_SUCCESS': { draft.profile = action.payload.user; break; } @@ -15,7 +15,7 @@ export default function user(state = INITIAL_STATE, action) { draft.profile = action.payload.profile; break; } - case '@auth/LOG_OUT': { + case '@auth/SIGN_OUT': { draft.profile = null; break; } diff --git a/yarn.lock b/yarn.lock index 6d7f85b..57526fb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5078,7 +5078,7 @@ hex-color-regex@^1.1.0: resolved "https://registry.yarnpkg.com/hex-color-regex/-/hex-color-regex-1.1.0.tgz#4c06fccb4602fe2602b3c93df82d7e7dbf1a8a8e" integrity sha512-l9sfDFsuqtOqKDsQdqrMRk0U85RZc0RtOR9yPI7mRVOa4FsR/BVnZ0shmQRM96Ji99kYZP/7hn1cedc1+ApsTQ== -history@^4.9.0: +history@^4.10.1, history@^4.9.0: version "4.10.1" resolved "https://registry.yarnpkg.com/history/-/history-4.10.1.tgz#33371a65e3a83b267434e2b3f3b1b4c58aad4cf3" integrity sha512-36nwAD620w12kuzPAsyINPWJqlNbij+hpK1k9XRloDtym8mxzGYl2c17LnV6IAGB2Dmg4tEa7G7DlawS0+qjew==