Hotfix: withoutAuth + ajustements UserContext
This commit is contained in:
parent
5d048f3010
commit
ca0c77a522
@ -56,7 +56,7 @@ exports.putUser = (req, res, next) => {
|
|||||||
logo.mimetype !== 'image/jpeg' ||
|
logo.mimetype !== 'image/jpeg' ||
|
||||||
logo.mimetype !== 'image/gif'
|
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;
|
const logoName = name + req.userId + uuid.v4() + logo.name;
|
||||||
// Supprime les anciens logo
|
// Supprime les anciens logo
|
||||||
|
@ -22,6 +22,7 @@ module.exports = sequelize.define('user', {
|
|||||||
},
|
},
|
||||||
biography: {
|
biography: {
|
||||||
type: Sequelize.TEXT,
|
type: Sequelize.TEXT,
|
||||||
|
defaultValue: ""
|
||||||
},
|
},
|
||||||
logo: {
|
logo: {
|
||||||
type: Sequelize.STRING,
|
type: Sequelize.STRING,
|
||||||
|
@ -14,45 +14,56 @@ function UserContextProvider(props) {
|
|||||||
const [messageLogin, setMessageLogin] = useState("");
|
const [messageLogin, setMessageLogin] = useState("");
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const user = cookies.get('user');
|
const newUser = cookies.get('user');
|
||||||
setUser(user);
|
if (newUser != undefined) {
|
||||||
if (user != undefined) {
|
|
||||||
setIsAuth(true);
|
setIsAuth(true);
|
||||||
|
setUser(newUser);
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isAuth) {
|
||||||
|
setMessageLogin('<p class="form-error"><b>Erreur:</b> Vous devez être déconnecter avant de vous connecter.</p>');
|
||||||
|
} else {
|
||||||
|
setMessageLogin("");
|
||||||
|
}
|
||||||
|
}, [isAuth]);
|
||||||
|
|
||||||
|
const logoutUser = () => {
|
||||||
|
cookies.remove('user', { path: '/' });
|
||||||
|
setUser(null);
|
||||||
|
setIsAuth(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
const changeUserValue = (newUser) => {
|
||||||
|
cookies.remove('user', { path: '/' });
|
||||||
|
cookies.set('user', newUser, { path: '/' });
|
||||||
|
setUser(newUser);
|
||||||
|
}
|
||||||
|
|
||||||
const loginUser = ({ email, password }) => {
|
const loginUser = ({ email, password }) => {
|
||||||
setLoginLoading(true);
|
setLoginLoading(true);
|
||||||
api.post('/users/login', { email, password })
|
return new Promise(async (next) => {
|
||||||
.then((response) => {
|
try {
|
||||||
const user = response.data;
|
const response = await api.post('/users/login', { email, password });
|
||||||
changeUserValue(user);
|
const newUser = response.data;
|
||||||
|
changeUserValue(newUser);
|
||||||
setIsAuth(true);
|
setIsAuth(true);
|
||||||
setMessageLogin('<p class="form-success"><b>Succès:</b> Connexion réussi!</p>');
|
setMessageLogin('<p class="form-success"><b>Succès:</b> Connexion réussi!</p>');
|
||||||
setLoginLoading(false);
|
setLoginLoading(false);
|
||||||
})
|
return next({ isSuccess: true, newUser });
|
||||||
.catch((error) => {
|
} catch (error) {
|
||||||
setMessageLogin(`<p class="form-error"><b>Erreur:</b> ${error.response.data.message}</p>`);
|
setMessageLogin(`<p class="form-error"><b>Erreur:</b> ${error.response.data.message}</p>`);
|
||||||
setLoginLoading(false);
|
setLoginLoading(false);
|
||||||
setIsAuth(false);
|
setIsAuth(false);
|
||||||
setUser(null);
|
setUser(null);
|
||||||
|
return next({ isSuccess: false });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const logoutUser = () => {
|
|
||||||
cookies.remove('user');
|
|
||||||
setUser(null);
|
|
||||||
setIsAuth(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
const changeUserValue = (user) => {
|
|
||||||
cookies.remove('user');
|
|
||||||
cookies.set('user', user);
|
|
||||||
setUser(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<UserContext.Provider value={{ user, loginUser, logoutUser, loginLoading, messageLogin, isAuth, changeUserValue }}>
|
<UserContext.Provider value={{ user, loginUser, logoutUser, loginLoading, messageLogin, isAuth, changeUserValue, setMessageLogin }}>
|
||||||
{props.children}
|
{props.children}
|
||||||
</UserContext.Provider>
|
</UserContext.Provider>
|
||||||
);
|
);
|
||||||
|
19
website/hoc/withoutAuth.js
Normal file
19
website/hoc/withoutAuth.js
Normal file
@ -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 <WrappedComponent { ...props } />;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Component;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default withoutAuth;
|
@ -5,13 +5,14 @@ import htmlParser from 'html-react-parser';
|
|||||||
import Loader from '../components/Loader';
|
import Loader from '../components/Loader';
|
||||||
import HeadTag from '../components/HeadTag';
|
import HeadTag from '../components/HeadTag';
|
||||||
import { UserContext } from '../contexts/UserContext';
|
import { UserContext } from '../contexts/UserContext';
|
||||||
|
import redirect from '../utils/redirect';
|
||||||
import '../public/css/pages/register-login.css';
|
import '../public/css/pages/register-login.css';
|
||||||
|
|
||||||
const Login = () => {
|
const Login = () => {
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [inputState, setInputState] = useState({});
|
const [inputState, setInputState] = useState({});
|
||||||
const { loginUser, messageLogin, loginLoading } = useContext(UserContext);
|
const { loginUser, messageLogin, loginLoading, isAuth } = useContext(UserContext);
|
||||||
|
|
||||||
const handleChange = (event) => {
|
const handleChange = (event) => {
|
||||||
const inputStateNew = { ...inputState };
|
const inputStateNew = { ...inputState };
|
||||||
@ -19,9 +20,14 @@ const Login = () => {
|
|||||||
setInputState(inputStateNew);
|
setInputState(inputStateNew);
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleSubmit = (event) => {
|
const handleSubmit = async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
loginUser(inputState);
|
if (!isAuth) {
|
||||||
|
const loginObject = await loginUser(inputState);
|
||||||
|
if (loginObject.isSuccess) {
|
||||||
|
redirect({}, `/profile/${loginObject.newUser.name}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -81,7 +81,7 @@ const Profile = (props) => {
|
|||||||
<FontAwesomeIcon icon={faTimes} style={{ width: '1.5rem', color: 'red' }} />
|
<FontAwesomeIcon icon={faTimes} style={{ width: '1.5rem', color: 'red' }} />
|
||||||
</span>
|
</span>
|
||||||
<h2 className="text-center">Éditer le profil</h2>
|
<h2 className="text-center">Éditer le profil</h2>
|
||||||
<p className="text-center"><em>(Vous devrez vous reconnecter après la sauvegarde) <br/> Si vous changez votre adresse email, vous devrez la confirmer (vérifier vos emails).</em></p>
|
<p className="text-center"><em>(Vous devrez vous reconnecter après la sauvegarde) <br/> Si vous changez votre adresse email, vous devrez la confirmer comme à l'inscription (vérifier vos emails).</em></p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -110,7 +110,7 @@ const Profile = (props) => {
|
|||||||
|
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
<label className="form-label" htmlFor="logo">Logo <em>(400x400 recommandé)</em> :</label>
|
<label className="form-label" htmlFor="logo">Logo <em>(400x400 recommandé)</em> :</label>
|
||||||
<br/>
|
<p style={{ margin: 0 }}><em>Si aucun fichier est choisi, le logo ne sera pas modifié.</em></p>
|
||||||
<input onChange={handleChange} accept="image/jpeg,image/jpg,image/png,image/gif" type="file" name="logo" id="logo" />
|
<input onChange={handleChange} accept="image/jpeg,image/jpg,image/png,image/gif" type="file" name="logo" id="logo" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -2,8 +2,9 @@ import { Fragment, useState } from 'react';
|
|||||||
import htmlParser from 'html-react-parser';
|
import htmlParser from 'html-react-parser';
|
||||||
import Loader from '../components/Loader';
|
import Loader from '../components/Loader';
|
||||||
import HeadTag from '../components/HeadTag';
|
import HeadTag from '../components/HeadTag';
|
||||||
import '../public/css/pages/register-login.css';
|
|
||||||
import api from '../utils/api';
|
import api from '../utils/api';
|
||||||
|
import withoutAuth from '../hoc/withoutAuth';
|
||||||
|
import '../public/css/pages/register-login.css';
|
||||||
|
|
||||||
const Register = () => {
|
const Register = () => {
|
||||||
|
|
||||||
@ -76,4 +77,4 @@ const Register = () => {
|
|||||||
</Fragment>
|
</Fragment>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
export default Register;
|
export default withoutAuth(Register);
|
Reference in New Issue
Block a user