From 1f646377c35b65b97d6eeebb1e88f6d8307e1ef0 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 29 Jun 2020 23:59:15 +0300 Subject: feat!: create useAuth hook --- src/hooks/useAuth.tsx | 75 +++++++++++++++++++++++++++++++++++ src/index.tsx | 45 ++------------------- src/pages/AuthPage/AuthPage.tsx | 11 ++--- src/pages/AuthPage/SignInForm.tsx | 10 ++--- src/pages/AuthPage/SignUpForm.tsx | 9 ++--- src/pages/FeedPage/FeedPage.tsx | 7 ++-- src/pages/FeedPage/PollSubmission.tsx | 9 +++-- src/pages/ProfilePage/MoreMenu.tsx | 11 +++-- src/pages/ProfilePage/ProfileInfo.tsx | 24 +++++------ src/pages/ProfilePage/ProfilePage.tsx | 10 +---- 10 files changed, 118 insertions(+), 93 deletions(-) create mode 100644 src/hooks/useAuth.tsx (limited to 'src') diff --git a/src/hooks/useAuth.tsx b/src/hooks/useAuth.tsx new file mode 100644 index 0000000..4f76593 --- /dev/null +++ b/src/hooks/useAuth.tsx @@ -0,0 +1,75 @@ +import React, { useState, useEffect, useContext, createContext } from 'react'; +import { User } from 'which-types'; +import { post, get } from '../requests'; + + +interface ContextType { + user: User | null, + setUser: (user: User) => void; + login: (username: string, password: string, remember?: boolean) => Promise; + logout: () => void; + isAuthenticated: () => boolean; +} + +const authContext = createContext({ + user: null, + setUser: () => {}, + login: async () => false, + logout: () => {}, + isAuthenticated: () => false +}); + +const useProvideAuth = () => { + const [user, setUser] = useState(null); + + const login = (username: string, password: string, remember = true): Promise => { + return post('/authentication', { + strategy: 'local', + username, + password + }).then(response => { + const me = response.data.user; + const token = response.data.accessToken; + setUser(me); + // navigate('profile', me._id); + localStorage.setItem('userId', me._id); + localStorage.setItem('token', token); + if (!remember) localStorage.setItem('shouldClear', 'true'); + return true; + }).catch(() => false); + }; + + const logout = () => { + setUser(null); + localStorage.removeItem('userId'); + localStorage.removeItem('token'); + // navigate('auth'); + }; + + const isAuthenticated = () => Boolean(user); + + useEffect(() => { + if (localStorage.getItem('shouldClear')) { + localStorage.clear(); + } + const userId = localStorage.getItem('userId'); + if (userId) { + get(`/users/${userId}`).then(response => { + setUser(response.data); + }); + } + }, []); + + return { user, setUser, login, logout, isAuthenticated }; +}; + +export const AuthProvider: React.FC = ({ children }) => { + const auth = useProvideAuth(); + const { Provider } = authContext; + return {children} +}; + +export const useAuth = () => { + return useContext(authContext); +} + diff --git a/src/index.tsx b/src/index.tsx index 02f7969..f9114c3 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -17,6 +17,7 @@ import AuthPage from './pages/AuthPage/AuthPage'; import { Page } from './types'; import { get, post } from './requests'; import ScrollTopArrow from './components/ScrollTopArrow/ScrollTopArrow'; +import { useAuth, AuthProvider } from './hooks/useAuth'; const theme = createMuiTheme({ @@ -37,9 +38,9 @@ const useStyles = makeStyles({ }); const App: React.FC = () => { - const [user, setUser] = React.useState(); const [page, setPage] = useState({ prefix: 'feed', id: '' }); const classes = useStyles(); + const user = { _id: '', avatarUrl: '' }; const navigate = (prefix: string, id?: string): void => { if (prefix === 'profile' && !id && !user) { @@ -55,42 +56,6 @@ const App: React.FC = () => { } }; - const logIn = (username: string, password: string, remember = true): Promise => { - return post('/authentication', { - strategy: 'local', - username, - password - }).then(response => { - const me = response.data.user; - const token = response.data.accessToken; - setUser(me); - navigate('profile', me._id); - localStorage.setItem('userId', me._id); - localStorage.setItem('token', token); - if (!remember) localStorage.setItem('shouldClear', 'true'); - return true; - }).catch(() => false); - }; - - const logOut = () => { - setUser(undefined); - localStorage.removeItem('userId'); - localStorage.removeItem('token'); - navigate('auth'); - }; - - useEffect(() => { - if (localStorage.getItem('shouldClear')) { - localStorage.clear(); - } - const userId = localStorage.getItem('userId'); - if (userId) { - get(`/users/${userId}`).then(response => { - setUser(response.data); - }); - } - }, []); - return ( @@ -98,14 +63,12 @@ const App: React.FC = () => {
{ page.prefix === 'profile' && ( ) } - { page.prefix === 'feed' && } - { page.prefix === 'auth' && } + { page.prefix === 'feed' && } + { page.prefix === 'auth' && }
diff --git a/src/pages/AuthPage/AuthPage.tsx b/src/pages/AuthPage/AuthPage.tsx index d2c2eec..ad93463 100644 --- a/src/pages/AuthPage/AuthPage.tsx +++ b/src/pages/AuthPage/AuthPage.tsx @@ -3,11 +3,6 @@ import { makeStyles } from '@material-ui/core/styles'; import SignInForm from './SignInForm'; import SignUpForm from './SignUpForm'; - -interface PropTypes { - logIn: (name: string, password: string, remember?: boolean) => Promise; -} - const useStyles = makeStyles({ formTransfer: { display: 'flex', @@ -20,7 +15,7 @@ const useStyles = makeStyles({ } }); -const AuthPage: React.FC = ({ logIn }) => { +const AuthPage: React.FC = () => { const [auth, setAuth] = useState<'signIn' | 'signUp'>('signIn'); const classes = useStyles(); @@ -35,8 +30,8 @@ const AuthPage: React.FC = ({ logIn }) => { return ( <> - {auth === 'signIn' && } - {auth === 'signUp' && } + {auth === 'signIn' && } + {auth === 'signUp' && }
{footerInfo[auth][0]}
Promise; -} +import { useAuth } from '../../hooks/useAuth'; const useStyles = makeStyles(theme => ({ root: { @@ -28,12 +25,13 @@ const useStyles = makeStyles(theme => ({ } })); -const SignInForm: React.FC = ({ logIn }) => { +const SignInForm: React.FC = () => { const [error, setError] = useState(false); const [remember, setRemember] = useState(true); const classes = useStyles(); const nameRef = useRef(); const passwordRef = useRef(); + const { login } = useAuth(); const handleCheck = () => { setRemember(!remember); @@ -43,7 +41,7 @@ const SignInForm: React.FC = ({ logIn }) => { const name = nameRef.current?.value; const password = passwordRef.current?.value; if (name && password) { - logIn(name, password, remember).then(success => { + login(name, password, remember).then(success => { if (!success) setError(true); }); } diff --git a/src/pages/AuthPage/SignUpForm.tsx b/src/pages/AuthPage/SignUpForm.tsx index 25b79ff..af7a0f8 100644 --- a/src/pages/AuthPage/SignUpForm.tsx +++ b/src/pages/AuthPage/SignUpForm.tsx @@ -3,10 +3,8 @@ import { makeStyles } from '@material-ui/core/styles'; import TextField from '@material-ui/core/TextField'; import Button from '@material-ui/core/Button'; import { post } from '../../requests'; +import { useAuth } from '../../hooks/useAuth'; -interface PropTypes { - logIn: (name: string, password: string) => Promise; -} const useStyles = makeStyles(theme => ({ root: { @@ -25,12 +23,13 @@ const useStyles = makeStyles(theme => ({ } })); -const SignUpForm: React.FC = ({ logIn }) => { +const SignUpForm: React.FC = () => { const [error, setError] = useState(false); const classes = useStyles(); const usernameRef = useRef(); const emailRef = useRef(); const passwordRef = useRef(); + const { login } = useAuth(); const onClick = () => { const username = usernameRef.current?.value; @@ -38,7 +37,7 @@ const SignUpForm: React.FC = ({ logIn }) => { const email = emailRef.current?.value; if (username && password) { post('/users', { username, password, email }).then(() => { - logIn(username, password); + login(username, password); }); } else setError(true); }; diff --git a/src/pages/FeedPage/FeedPage.tsx b/src/pages/FeedPage/FeedPage.tsx index 0017275..6561991 100644 --- a/src/pages/FeedPage/FeedPage.tsx +++ b/src/pages/FeedPage/FeedPage.tsx @@ -4,15 +4,16 @@ import { Poll, User } from 'which-types'; import Feed from '../../components/Feed/Feed'; import { get } from '../../requests'; import PollSubmission from './PollSubmission'; +import { useAuth } from '../../hooks/useAuth'; interface PropTypes { navigate: (prefix: string, id: string) => void; - user: User | undefined; } -const FeedPage: React.FC = ({ navigate, user }) => { +const FeedPage: React.FC = ({ navigate }) => { const [polls, setPolls] = useState([]); + const { isAuthenticated } = useAuth(); useEffect(() => { get('/feed').then(response => { @@ -28,7 +29,7 @@ const FeedPage: React.FC = ({ navigate, user }) => { return ( <> - {user && } + {isAuthenticated() && } ); diff --git a/src/pages/FeedPage/PollSubmission.tsx b/src/pages/FeedPage/PollSubmission.tsx index 16c8350..4e06254 100644 --- a/src/pages/FeedPage/PollSubmission.tsx +++ b/src/pages/FeedPage/PollSubmission.tsx @@ -12,9 +12,9 @@ import PollSubmissionImage from './PollSubmissionImage'; import UserStrip from '../../components/UserStrip/UserStrip'; import { post } from '../../requests'; import { Contents } from './types'; +import { useAuth } from '../../hooks/useAuth'; interface PropTypes{ - user: User; addPoll: (poll: Poll) => void; } @@ -30,10 +30,11 @@ const emptyContents: Contents = { right: { url: '' } }; -const PollSubmission: React.FC = ({ user, addPoll }) => { +const PollSubmission: React.FC = ({ addPoll }) => { const classes = useStyles(); const [expanded, setExpanded] = useState(false); const [contents, setContents] = useState(emptyContents); + const { user } = useAuth(); const readyToSubmit = contents.left.url && contents.right.url; @@ -47,7 +48,7 @@ const PollSubmission: React.FC = ({ user, addPoll }) => { const handleClick = () => { if (expanded && readyToSubmit) { - post('/polls/', { authorId: user._id, contents }).then(response => { + post('/polls/', { contents }).then(response => { addPoll(response.data); }); setContents({ ...emptyContents }); @@ -59,7 +60,7 @@ const PollSubmission: React.FC = ({ user, addPoll }) => { - {}} /> + {user && {}} />}
diff --git a/src/pages/ProfilePage/MoreMenu.tsx b/src/pages/ProfilePage/MoreMenu.tsx index bf3347b..7c17f1e 100644 --- a/src/pages/ProfilePage/MoreMenu.tsx +++ b/src/pages/ProfilePage/MoreMenu.tsx @@ -4,10 +4,7 @@ import Menu from '@material-ui/core/Menu'; import MenuItem from '@material-ui/core/MenuItem'; import MoreHorizIcon from '@material-ui/icons/MoreHoriz'; import { makeStyles } from '@material-ui/core'; - -interface PropTypes { - logOut: () => void; -} +import { useAuth } from '../../hooks/useAuth'; const ITEM_HEIGHT = 48; @@ -19,9 +16,11 @@ const useStyles = makeStyles({ } }); -const MoreMenu: React.FC = ({ logOut }) => { +const MoreMenu: React.FC = () => { const classes = useStyles(); const [anchorEl, setAnchorEl] = React.useState(null); + const { logout } = useAuth(); + const open = Boolean(anchorEl); const handleClick = (event: React.MouseEvent) => { @@ -56,7 +55,7 @@ const MoreMenu: React.FC = ({ logOut }) => { } }} > - Log out + Log out
diff --git a/src/pages/ProfilePage/ProfileInfo.tsx b/src/pages/ProfilePage/ProfileInfo.tsx index 9fe5912..9557a5e 100644 --- a/src/pages/ProfilePage/ProfileInfo.tsx +++ b/src/pages/ProfilePage/ProfileInfo.tsx @@ -8,15 +8,14 @@ import MoreMenu from './MoreMenu'; import Highlight from './Highlight'; import UploadImage from '../../components/UploadImage/UploadImage'; import { patch } from '../../requests'; +import { useAuth } from '../../hooks/useAuth'; interface PropTypes { - user: User | undefined; - logOut: () => void; savedPolls: number; totalVotes: number; - setUserInfo: (a: User) => void; - setUser: (a:User) => void; + userInfo: User | undefined; + setUserInfo: (userInfo: User) => void; } const useStyles = makeStyles(theme => ({ @@ -78,13 +77,14 @@ const useStyles = makeStyles(theme => ({ const ProfileInfo: React.FC = ({ - user, logOut, savedPolls, totalVotes, setUserInfo, setUser + savedPolls, totalVotes, setUserInfo, userInfo }) => { const classes = useStyles(); const [input, setInput] = useState(false); + const { setUser } = useAuth(); - const dateSince = new Date(user?.createdAt || '').toLocaleDateString(); + const dateSince = new Date(userInfo?.createdAt || '').toLocaleDateString(); const handleClick = () => { setInput(!input); @@ -101,10 +101,10 @@ const ProfileInfo: React.FC = ({ return (
{ - user?._id === localStorage.getItem('userId') + userInfo?._id === localStorage.getItem('userId') ? (
- +
= ({
)} > - +
) - : + : } - {user?.username} - {user?.verified && } + {userInfo?.username} + {userInfo?.verified && }
diff --git a/src/pages/ProfilePage/ProfilePage.tsx b/src/pages/ProfilePage/ProfilePage.tsx index b0ac103..ad2da46 100644 --- a/src/pages/ProfilePage/ProfilePage.tsx +++ b/src/pages/ProfilePage/ProfilePage.tsx @@ -6,15 +6,11 @@ import Feed from '../../components/Feed/Feed'; import { get } from '../../requests'; interface PropTypes { - logOut: () => void; navigate: (prefix: string, id: string) => void; id: string; - setUser:(a:User)=>void; } -const ProfilePage: React.FC = ({ - logOut, id, navigate, setUser -}) => { +const ProfilePage: React.FC = ({ id, navigate }) => { const [userInfo, setUserInfo] = useState(); const [polls, setPolls] = useState([]); const [totalVotes, setTotalVotes] = useState(0); @@ -40,10 +36,8 @@ const ProfilePage: React.FC = ({ return ( <> -- cgit v1.2.3