Skip to content

Commit

Permalink
add auth cookie validation
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiolcastro committed May 17, 2020
1 parent 1553b07 commit 4719b55
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/hooks/useAuthenticated.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import Cookies from 'js-cookie';
import { loginUser } from '../store/login';


export default function useAuthenticated() {
export default function useAuthenticated(loginPage = false) {
const dispatch = useDispatch();
const history = useHistory();
const isAuth = useSelector((state) => state.login.isAuthenticated);

// TODO: improve Auth logic
useEffect(() => {
if (!isAuth) {
const ongIdCookie = Cookies.get('ong_id');
if (ongIdCookie) {
return dispatch(loginUser(ongIdCookie));
}
return history.push('/');
}

return true;
}, []);
if (ongIdCookie) {
dispatch(loginUser(ongIdCookie));
if (!isAuth) history.push('/');
} else { history.push('/'); }
} else if (loginPage) history.push('/profiles');
}, [isAuth]);
}
3 changes: 3 additions & 0 deletions src/pages/Login/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import { useSelector, useDispatch } from 'react-redux';

import { FiLogIn } from 'react-icons/fi';
import { loginUser } from '../../store/login';
import useAuthenticated from '../../hooks/useAuthenticated';

import { Container, FormSection } from './styles';

import logoImg from '../../assets/images/logo.svg';
import herosImg from '../../assets/images/heroes.png';

export default function Login() {
useAuthenticated(true);

const history = useHistory();
const dispatch = useDispatch();

Expand Down
2 changes: 2 additions & 0 deletions src/pages/NewIncident/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import axios from 'axios';

import { FiArrowLeft } from 'react-icons/fi';
import { Container } from './styles';
import useAuthenticated from '../../hooks/useAuthenticated';

import logoImg from '../../assets/images/logo.svg';

export default function NewIncident() {
useAuthenticated();
const history = useHistory();

const [title, setTitle] = useState('');
Expand Down

0 comments on commit 4719b55

Please sign in to comment.