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";
import React from "react";
import React, { useState } from "react";
import axios from "axios";
import LoginForm from "@/components/auth/LoginForm";
const LoginPage: React.FC = () => {
const [error, setError] = useState("");
const handleLogin = async (email: string, password: string) => {
console.log('Connexion de l\'utilisateur:', email, password);
try {
const response = await axios.post("/api/auth/login", {
@@ -14,15 +15,21 @@ const LoginPage: React.FC = () => {
password,
});
console.log('Utilisateur connecté:', response.data);
} catch (error) {
console.error(error);
setError(""); // Reset errors
// Redirect to home page
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 (
<div className="min-h-screen flex items-center justify-center bg-gray-100">
<LoginForm onSubmit={handleLogin} />
<LoginForm onSubmit={handleLogin} error={error} setError={setError} />
</div>
);
};
+18 -6
View File
@@ -1,12 +1,13 @@
"use client";
import React from "react";
import React, { useState } from "react";
import axios from "axios";
import SignupForm from "@/components/auth/SignupForm";
const SignupPage: React.FC = () => {
const [error, setError] = useState("");
const handleSignup = async (username: string, email: string, password: string) => {
console.log('Création de l\'utilisateur:', username, email, password);
try {
const response = await axios.post("/api/auth/signup", {
@@ -15,15 +16,26 @@ const SignupPage: React.FC = () => {
password,
});
console.log('Utilisateur créé:', response.data);
} catch (error) {
console.error(error);
// Redirect to home page
window.location.href = "/";
} 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 (
<div className="min-h-screen flex items-center justify-center bg-gray-100">
<SignupForm onSubmit={handleSignup} />
<SignupForm onSubmit={handleSignup} error={error} setError={setError} />
</div>
);
};
+3 -2
View File
@@ -4,12 +4,13 @@ import React, { useState } from "react";
interface LoginFormProps {
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 [password, setPassword] = useState("");
const [error, setError] = useState("");
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
+3 -2
View File
@@ -4,13 +4,14 @@ import React, { useState } from "react";
interface SignupFormProps {
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 [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState("");
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();