add alert message on auth systeme

This commit is contained in:
Arhur
2024-12-08 04:08:58 +01:00
parent 3efd4c9345
commit 1e87cdeae0
5 changed files with 37 additions and 16 deletions
BIN
View File
Binary file not shown.
+13 -6
View File
@@ -1,12 +1,13 @@
"use client"; "use client";
import React from "react"; import React, { useState } from "react";
import axios from "axios"; import axios from "axios";
import LoginForm from "@/components/auth/LoginForm"; import LoginForm from "@/components/auth/LoginForm";
const LoginPage: React.FC = () => { const LoginPage: React.FC = () => {
const [error, setError] = useState("");
const handleLogin = async (email: string, password: string) => { const handleLogin = async (email: string, password: string) => {
console.log('Connexion de l\'utilisateur:', email, password);
try { try {
const response = await axios.post("/api/auth/login", { const response = await axios.post("/api/auth/login", {
@@ -14,15 +15,21 @@ const LoginPage: React.FC = () => {
password, password,
}); });
console.log('Utilisateur connecté:', response.data); setError(""); // Reset errors
} catch (error) { // Redirect to home page
console.error(error); window.location.href = "/";
} catch (error: any) {
if (error.response && error.response.status === 400) {
setError("L'adresse e-mail ou le mot de passe est incorrect.");
} else {
setError("Une erreur s'est produite lors de la connexion.");
}
} }
}; };
return ( return (
<div className="min-h-screen flex items-center justify-center bg-gray-100"> <div className="min-h-screen flex items-center justify-center bg-gray-100">
<LoginForm onSubmit={handleLogin} /> <LoginForm onSubmit={handleLogin} error={error} setError={setError} />
</div> </div>
); );
}; };
+18 -6
View File
@@ -1,12 +1,13 @@
"use client"; "use client";
import React from "react"; import React, { useState } from "react";
import axios from "axios"; import axios from "axios";
import SignupForm from "@/components/auth/SignupForm"; import SignupForm from "@/components/auth/SignupForm";
const SignupPage: React.FC = () => { const SignupPage: React.FC = () => {
const [error, setError] = useState("");
const handleSignup = async (username: string, email: string, password: string) => { const handleSignup = async (username: string, email: string, password: string) => {
console.log('Création de l\'utilisateur:', username, email, password);
try { try {
const response = await axios.post("/api/auth/signup", { const response = await axios.post("/api/auth/signup", {
@@ -15,15 +16,26 @@ const SignupPage: React.FC = () => {
password, password,
}); });
console.log('Utilisateur créé:', response.data); // Redirect to home page
} catch (error) { window.location.href = "/";
console.error(error); } catch (error: any) {
if (error.response && error.response.status === 400) {
if (error.response.data.error === "Username already taken") {
setError("Le nom d'utilisateur est déjà pris.");
} else if (error.response.data.error === "Email already taken") {
setError("L'adresse e-mail est déjà utilisée.");
} else {
setError("Une erreur s'est produite lors de la création de l'utilisateur.");
}
} else {
setError("Une erreur s'est produite lors de la création de l'utilisateur.");
}
} }
}; };
return ( return (
<div className="min-h-screen flex items-center justify-center bg-gray-100"> <div className="min-h-screen flex items-center justify-center bg-gray-100">
<SignupForm onSubmit={handleSignup} /> <SignupForm onSubmit={handleSignup} error={error} setError={setError} />
</div> </div>
); );
}; };
+3 -2
View File
@@ -4,12 +4,13 @@ import React, { useState } from "react";
interface LoginFormProps { interface LoginFormProps {
onSubmit: (email: string, password: string) => void; onSubmit: (email: string, password: string) => void;
error: string;
setError: (message: string) => void;
} }
const LoginForm: React.FC<LoginFormProps> = ({ onSubmit }) => { const LoginForm: React.FC<LoginFormProps> = ({ onSubmit, error, setError }) => {
const [email, setEmail] = useState(""); const [email, setEmail] = useState("");
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
const [error, setError] = useState("");
const handleSubmit = (e: React.FormEvent) => { const handleSubmit = (e: React.FormEvent) => {
e.preventDefault(); e.preventDefault();
+3 -2
View File
@@ -4,13 +4,14 @@ import React, { useState } from "react";
interface SignupFormProps { interface SignupFormProps {
onSubmit: (username: string, email: string, password: string) => void; onSubmit: (username: string, email: string, password: string) => void;
error: string;
setError: (message: string) => void;
} }
const SignupForm: React.FC<SignupFormProps> = ({ onSubmit }) => { const SignupForm: React.FC<SignupFormProps> = ({ onSubmit, error, setError }) => {
const [username, setUsername] = useState(""); const [username, setUsername] = useState("");
const [email, setEmail] = useState(""); const [email, setEmail] = useState("");
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
const [error, setError] = useState("");
const handleSubmit = (e: React.FormEvent) => { const handleSubmit = (e: React.FormEvent) => {
e.preventDefault(); e.preventDefault();