From c591f786b9914c3cede6fdeaf88fbb33e35bd896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marie=20M=C3=A9lody=20F?= Date: Sat, 2 Sep 2023 14:47:19 +0200 Subject: [PATCH] route-login-ok --- src/App.css | 4 ++ src/App.jsx | 25 ++++++++++-- src/components/Header.jsx | 28 +++++++++---- src/pages/Login.jsx | 82 ++++++++++++++++++++++++++++++++++++--- src/pages/Signup.jsx | 29 +++++++++++--- 5 files changed, 146 insertions(+), 22 deletions(-) diff --git a/src/App.css b/src/App.css index 2b5610e..549fefe 100644 --- a/src/App.css +++ b/src/App.css @@ -15,6 +15,10 @@ span { a { text-decoration: none; } + +h1 { + color: white; +} /* --------------------- */ /* --------------------- */ /* --------HEADER------- */ diff --git a/src/App.jsx b/src/App.jsx index 3b335d9..38468ce 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,26 +1,43 @@ import { useState, useEffect } from "react"; import { BrowserRouter as Router, Routes, Route } from "react-router-dom"; +import Cookies from "js-cookie"; import "./App.css"; import Header from "./components/Header"; import Home from "./pages/Home"; import Comics from "./pages/Comics"; import Charaters from "./pages/Characters"; import Character from "./pages/Character"; -import Login from "./pages/Login"; import Signup from "./pages/Signup"; +import Login from "./pages/Login"; function App() { + const [token, setToken] = useState(Cookies.get("token")); + + useEffect(() => { + if (token) { + Cookies.set("token", token, { expires: 7 }); + } else { + Cookies.remove("token"); + } + }, [token]); + return ( <> -
+
} /> } /> } /> } /> - } /> - } /> + } + /> + } + /> diff --git a/src/components/Header.jsx b/src/components/Header.jsx index af511ae..009a75e 100644 --- a/src/components/Header.jsx +++ b/src/components/Header.jsx @@ -2,7 +2,7 @@ import { Link } from "react-router-dom"; import React, { Component } from "react"; import Nav from "./Nav"; -const Header = () => { +const Header = ({ token, setToken }) => { return ( <>
@@ -10,13 +10,25 @@ const Header = () => { - - - - - - - + {token ? ( + + ) : ( +
+ + + + + + +
+ )}
diff --git a/src/pages/Login.jsx b/src/pages/Login.jsx index d63fd22..03234fd 100644 --- a/src/pages/Login.jsx +++ b/src/pages/Login.jsx @@ -1,12 +1,84 @@ import axios from "axios"; -import React, { useState, useEffect } from "react"; -import { Link } from "react-router-dom"; +import React, { useState } from "react"; +import { Link, useNavigate } from "react-router-dom"; // import { useState } from "react"; -const Login = () => { +const Login = ({ token, setToken }) => { + const [email, setEmail] = useState(""); + const [password, setPassword] = useState(""); + const navigate = useNavigate(); + return ( -
- Login +
+
+
{ + event.preventDefault(); + + try { + const response = await axios.post( + `https://site--marvel-backend--r2txk865xjj8.code.run/user/login`, + { + email: email, + password: password, + } + ); + console.log("log connexion", response.data); + console.log("data.token", response.data.token); + navigate("/characters"); + setToken(response.data.token); + } catch (error) { + error.response; + console.log("=>", error.response); + if ( + error.response.data.message === + "Cannot read properties of null (reading 'salt')" + ) { + alert( + "Vous avez saisi un mauvais mot de passe ou identifiant ๐Ÿซค. Si vous n'avez pas de compte, merci de vous inscrire" + ); + } else if ( + error.response.data === + "Le mot de passe ou l'identifiant n'est pas correct." + ) { + alert( + "Vous avez saisi un mauvais mot de passe ou identifiant ๐Ÿซค. Si vous n'avez pas de compte, merci de vous inscrire" + ); + } + } + }} + > +

Log in

+ { + setEmail(event.target.value); + }} + /> + { + setPassword(event.target.value); + }} + /> + + + + You dont have a account ? Come on, come to Sign up ! + + +
+
); }; diff --git a/src/pages/Signup.jsx b/src/pages/Signup.jsx index 93beadf..d118523 100644 --- a/src/pages/Signup.jsx +++ b/src/pages/Signup.jsx @@ -1,9 +1,9 @@ import axios from "axios"; -import React, { useState, useEffect } from "react"; -import { Link, Navigate } from "react-router-dom"; -import { useNavigate } from "react-router-dom"; +import React, { useState } from "react"; +import { Link, useNavigate } from "react-router-dom"; const Signup = () => { + const navigate = useNavigate(); const [username, setUsername] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); @@ -44,10 +44,24 @@ const Signup = () => { console.log("Submit ==> ", data); const token = response.data.token; + navigate("/characters"); setToken(token); - Navigate("/characters"); } catch (error) { console.log(error.response); + alert("Vous devez remplir tout les champs ๐Ÿ˜‰ !"); + + if ( + error.response.data.message === + "Email already exist ! Use your account ๐Ÿš€" + ) { + setErrorMessage("Email already exist ! Use your account ๐Ÿš€"); + alert("Email already exist ! Use your account ๐Ÿš€"); + } else if ( + error.response.data.message === + "This username already exist ! Choose another username ๐ŸคŸ๐Ÿผ !" + ) { + alert("This username already exist ! Choose another username ๐ŸคŸ๐Ÿผ !"); + } } }; @@ -81,6 +95,8 @@ const Signup = () => { />
+
+ - + + +

Already registered ? use login

+ ); };