From ca0c77a52268a3d350f4083b11da6f5b984be0a6 Mon Sep 17 00:00:00 2001 From: Divlo Date: Wed, 8 Apr 2020 15:26:18 +0200 Subject: [PATCH] Hotfix: withoutAuth + ajustements UserContext --- api/controllers/users.js | 2 +- api/models/users.js | 1 + website/contexts/UserContext.js | 63 +++++++++++++++++++-------------- website/hoc/withoutAuth.js | 19 ++++++++++ website/pages/login.js | 16 ++++++--- website/pages/profile/[name].js | 6 ++-- website/pages/register.js | 5 +-- 7 files changed, 75 insertions(+), 37 deletions(-) create mode 100644 website/hoc/withoutAuth.js diff --git a/api/controllers/users.js b/api/controllers/users.js index de0fd7f..329f2da 100644 --- a/api/controllers/users.js +++ b/api/controllers/users.js @@ -56,7 +56,7 @@ exports.putUser = (req, res, next) => { logo.mimetype !== 'image/jpeg' || logo.mimetype !== 'image/gif' )) { - return errorHandling(next, { message:"Le profil doit avoir une image valide.", statusCode: 400 }); + return errorHandling(next, { message:"Le profil doit avoir une image valide (PNG, JPG, GIF) et moins de 5mo.", statusCode: 400 }); } const logoName = name + req.userId + uuid.v4() + logo.name; // Supprime les anciens logo diff --git a/api/models/users.js b/api/models/users.js index c1d794c..f2755ff 100644 --- a/api/models/users.js +++ b/api/models/users.js @@ -22,6 +22,7 @@ module.exports = sequelize.define('user', { }, biography: { type: Sequelize.TEXT, + defaultValue: "" }, logo: { type: Sequelize.STRING, diff --git a/website/contexts/UserContext.js b/website/contexts/UserContext.js index 36f0cb2..aa4667f 100644 --- a/website/contexts/UserContext.js +++ b/website/contexts/UserContext.js @@ -14,45 +14,56 @@ function UserContextProvider(props) { const [messageLogin, setMessageLogin] = useState(""); useEffect(() => { - const user = cookies.get('user'); - setUser(user); - if (user != undefined) { + const newUser = cookies.get('user'); + if (newUser != undefined) { setIsAuth(true); + setUser(newUser); } }, []); - const loginUser = ({ email, password }) => { - setLoginLoading(true); - api.post('/users/login', { email, password }) - .then((response) => { - const user = response.data; - changeUserValue(user); - setIsAuth(true); - setMessageLogin('

Succès: Connexion réussi!

'); - setLoginLoading(false); - }) - .catch((error) => { - setMessageLogin(`

Erreur: ${error.response.data.message}

`); - setLoginLoading(false); - setIsAuth(false); - setUser(null); - }); - } + useEffect(() => { + if (isAuth) { + setMessageLogin('

Erreur: Vous devez être déconnecter avant de vous connecter.

'); + } else { + setMessageLogin(""); + } + }, [isAuth]); const logoutUser = () => { - cookies.remove('user'); + cookies.remove('user', { path: '/' }); setUser(null); setIsAuth(false); } - const changeUserValue = (user) => { - cookies.remove('user'); - cookies.set('user', user); - setUser(user); + const changeUserValue = (newUser) => { + cookies.remove('user', { path: '/' }); + cookies.set('user', newUser, { path: '/' }); + setUser(newUser); + } + + const loginUser = ({ email, password }) => { + setLoginLoading(true); + return new Promise(async (next) => { + try { + const response = await api.post('/users/login', { email, password }); + const newUser = response.data; + changeUserValue(newUser); + setIsAuth(true); + setMessageLogin('

Succès: Connexion réussi!

'); + setLoginLoading(false); + return next({ isSuccess: true, newUser }); + } catch (error) { + setMessageLogin(`

Erreur: ${error.response.data.message}

`); + setLoginLoading(false); + setIsAuth(false); + setUser(null); + return next({ isSuccess: false }); + } + }); } return ( - + {props.children} ); diff --git a/website/hoc/withoutAuth.js b/website/hoc/withoutAuth.js new file mode 100644 index 0000000..42d8125 --- /dev/null +++ b/website/hoc/withoutAuth.js @@ -0,0 +1,19 @@ +import { useContext } from 'react'; +import { UserContext } from '../contexts/UserContext'; +import redirect from '../utils/redirect'; + +const withoutAuth = (WrappedComponent) => { + + const Component = (props) => { + + const { isAuth, user } = useContext(UserContext); + + if (isAuth) return redirect({}, `/profile/${user.name}`); + + return ; + } + + return Component; +} + +export default withoutAuth; \ No newline at end of file diff --git a/website/pages/login.js b/website/pages/login.js index e51523d..f03341f 100644 --- a/website/pages/login.js +++ b/website/pages/login.js @@ -5,13 +5,14 @@ import htmlParser from 'html-react-parser'; import Loader from '../components/Loader'; import HeadTag from '../components/HeadTag'; import { UserContext } from '../contexts/UserContext'; +import redirect from '../utils/redirect'; import '../public/css/pages/register-login.css'; const Login = () => { - const router = useRouter(); - const [inputState, setInputState] = useState({}); - const { loginUser, messageLogin, loginLoading } = useContext(UserContext); + const router = useRouter(); + const [inputState, setInputState] = useState({}); + const { loginUser, messageLogin, loginLoading, isAuth } = useContext(UserContext); const handleChange = (event) => { const inputStateNew = { ...inputState }; @@ -19,9 +20,14 @@ const Login = () => { setInputState(inputStateNew); } - const handleSubmit = (event) => { + const handleSubmit = async (event) => { event.preventDefault(); - loginUser(inputState); + if (!isAuth) { + const loginObject = await loginUser(inputState); + if (loginObject.isSuccess) { + redirect({}, `/profile/${loginObject.newUser.name}`); + } + } } return ( diff --git a/website/pages/profile/[name].js b/website/pages/profile/[name].js index 0c74fd5..0dc6c45 100644 --- a/website/pages/profile/[name].js +++ b/website/pages/profile/[name].js @@ -21,7 +21,7 @@ const Profile = (props) => { const publicationDate = `${('0'+createdAt.getDate()).slice(-2)}/${('0'+(createdAt.getMonth()+1)).slice(-2)}/${createdAt.getFullYear()}`; const { isAuth, user, logoutUser } = useContext(UserContext); - const [isOpen, setIsOpen] = useState(false); + const [isOpen, setIsOpen] = useState(false); let defaultInputState = {}; if (isAuth) { @@ -81,7 +81,7 @@ const Profile = (props) => {

Éditer le profil

-

(Vous devrez vous reconnecter après la sauvegarde)
Si vous changez votre adresse email, vous devrez la confirmer (vérifier vos emails).

+

(Vous devrez vous reconnecter après la sauvegarde)
Si vous changez votre adresse email, vous devrez la confirmer comme à l'inscription (vérifier vos emails).

@@ -110,7 +110,7 @@ const Profile = (props) => {
-
+

Si aucun fichier est choisi, le logo ne sera pas modifié.

diff --git a/website/pages/register.js b/website/pages/register.js index e52127f..5f30656 100644 --- a/website/pages/register.js +++ b/website/pages/register.js @@ -2,8 +2,9 @@ import { Fragment, useState } from 'react'; import htmlParser from 'html-react-parser'; import Loader from '../components/Loader'; import HeadTag from '../components/HeadTag'; -import '../public/css/pages/register-login.css'; import api from '../utils/api'; +import withoutAuth from '../hoc/withoutAuth'; +import '../public/css/pages/register-login.css'; const Register = () => { @@ -76,4 +77,4 @@ const Register = () => { ); } -export default Register; \ No newline at end of file +export default withoutAuth(Register); \ No newline at end of file