I'm trying to create a Login and Sign Up page in React Js, the idea is that that they should be rendered on the same page, and user should be able to switch between them (something like this example:https://codepen.io/guycode/pen/VwaKVrb)
i create a toggle function and the switching between the two mode work but the problem is when i switch to signup mode the url still "/Login" and the other big problem that the style and the front end design of the page stop work as it should Although when the tow mode signup and login are separate without the toggle mode the style work great
import UserServices from '../Services/service';
import img1 from '../pages/students.png';
import img2 from '../pages/students2.png';
import React, { useState } from "react";
import toast, {Toaster} from 'react-hot-toast';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {faCircleExclamation} from '@fortawesome/free-solid-svg-icons';
const Signup = () => {
const [firstName , setfirstName] = useState('')
const [lastName , setlastName] = useState('')
const [birthday , setbirthday] = useState('')
const [password , setpassword] = useState('')
const [email , setemail] = useState('')
const [errors , setErrors] = useState(
{
firstName : '',
lastName : '',
email : '',
password : '',
birthday : '',
});
const formValidation = () =>{
const pattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@$!%*?&])[A-Za-z0-9@$!%*?&]{8,}$/;
let localErrors = {...errors}
if(firstName === ""){
localErrors.firstName = 'First name required';
}
if(lastName === ""){
localErrors.lastName = 'Last name required';
}
if(email === ""){
localErrors.email = 'Email required';
}
if ((password === "") || (!pattern.test(password))){
localErrors.password = 'Password required';
}
if(birthday === ""){
localErrors.birthday = 'date required';
}
setErrors(localErrors);
console.log(localErrors);
const isValid = Object.values(localErrors).every(error => error === '');
return isValid;
}
const signUp = async (e) =>{
e.preventDefault();
if (formValidation()){
const data = {
firstName : firstName,
lastName : lastName,
email : email,
password : password,
birthday : birthday,
}
try {
const response = await UserServices.signup(data)
console.log("response ====>", response);
toast.success('Successfully created!');
setfirstName('');
setlastName('');
setemail('');
setpassword('');
setbirthday('');
}catch(err){
console.log(err)
toast.success('Successfully created!');
}
}else{
console.log("form invalid");
}
}
return (
<div class="container">
<div class="forms-container">
<Toaster />
<div class="signin-signup">
<form action="#" class="sign-up-form" onSubmit={signUp}>
<h2 class="title">Sign up</h2>
<div class="input-field">
<i class="fas fa-user"></i>
<input type="text" placeholder="First Name" value={firstName} onChange={ (e)=>setfirstName(e.target.value)}/>
{errors.firstName !== '' && (
<div className="error-message">
<FontAwesomeIcon icon={faCircleExclamation} />
{errors.firstName}
</div>
)}
</div>
<div class="input-field">
<i class="fas fa-user"></i>
<input type="text" placeholder="Last Name" value={lastName} onChange={ (e)=>setlastName(e.target.value)}/>
{errors.lastName !== '' && (
<div className="error-message">
<FontAwesomeIcon icon={faCircleExclamation} />
{errors.lastName}
</div>
)}
</div>
<div class="input-field">
<i class="fas fa-envelope"></i>
<input type="email" placeholder="Email" value={email} onChange={ (e)=>setemail(e.target.value)} />
{errors.email !== '' && (
<div className="error-message">
<FontAwesomeIcon icon={faCircleExclamation} />
{errors.email}
</div>
)}
</div>
<div class="input-field">
<i class="fas fa-lock"></i>
<input type="password" placeholder="Password" value={password} onChange={ (e)=>setpassword(e.target.value)} />
{errors.password !== '' && (
<div className="error-message">
<FontAwesomeIcon icon={faCircleExclamation} />
{errors.password}
</div>
)}
</div>
<div class="input-field">
<i class="fas fa-user"></i>
<input type="date" placeholder="Date_of_Birth" value={birthday} onChange={ (e)=>setbirthday(e.target.value)}/>
{errors.birthday !== '' && (
<div className="error-message">
<FontAwesomeIcon icon={faCircleExclamation} />
{errors.birthday}
</div>
)}
</div>
<input type="submit" class="btn" value="Sign up" />
</form>
</div>
</div>
<div class="panels-container">
<div class="panel left-panel">
<div class="content">
<h3>New in our faClub ?</h3>
<p>Enter your personal details and start journey with us.</p>
<button class="btn transparent" id="sign-up-btn">Sign up</button>
</div>
<img src={img1} class="image" alt="" />
</div>
<div class="panel right-panel">
<div class="content">
<h3>One of our faClub ?</h3>
<p>To keep connected with us please login with your personal info.</p>
<button class="btn transparent" id="sign-in-btn">Sign in</button>
</div>
<img src={img2} class="image" alt="" />
</div>
</div>
</div>
)
}
export default Signup
import UserServices from '../Services/service';
import img1 from '../pages/students.png';
import img2 from '../pages/students2.png';
import React, { useState } from "react";
import toast, {Toaster} from 'react-hot-toast';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {faCircleExclamation} from '@fortawesome/free-solid-svg-icons';
const SignIn = () => {
const [password , setpassword] = useState('')
const [email , setemail] = useState('')
const [errors , setErrors] = useState(
{
email : '',
password : '',
});
const formValidation = () =>{
const pattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@$!%*?&])[A-Za-z0-9@$!%*?&]{8,}$/;
let localErrors = {...errors}
if(email === ""){
localErrors.email = 'Email required';
}
if ((password === "") || (!pattern.test(password))){
localErrors.password = 'Password required';
}
setErrors(localErrors);
const isValid = Object.values(localErrors).every(error => error === '');
return isValid;
}
const signin = async (e) =>{
e.preventDefault();
console.log("forme submitted");
if (formValidation()){
const data = {
email : email,
password : password,
}
try {
const response = await UserServices.signin(data)
console.log("response ====>", response);
toast.success('Successfully Loget in!');
setemail('');
setpassword('');
}catch(err){
console.log(err)
toast.success('Successfully created!');
}
}else{
console.log("forme invalid")
}
}
return (
<div class="container">
<div class="forms-container">
<Toaster />
<div class="signin-signup">
<form action="#" class="sign-in-form" onSubmit={signin}>
<h2 class="title">Sign in</h2>
<div class="input-field">
<i class="fas fa-user"></i>
<input type="text" placeholder="Email" value={email} onChange={ (e)=>setemail(e.target.value)}/>
{errors.email !== '' && (
<div className="error-message">
<FontAwesomeIcon icon={faCircleExclamation} />
{errors.email}
</div>
)}
</div>
<div class="input-field">
<i class="fas fa-lock"></i>
<input type="password" placeholder="Password" value={password} onChange={ (e)=>setpassword(e.target.value)} />
{errors.password !== '' && (
<div className="error-message">
<FontAwesomeIcon icon={faCircleExclamation} />
{errors.password}
</div>
)}
</div>
<input type="submit" value="Login" class="btn solid" />
</form>
</div>
</div>
<div class="panels-container">
<div class="panel left-panel">
<div class="content">
<h3>New in our faClub ?</h3>
<p>Enter your personal details and start journey with us.</p>
<button class="btn transparent" id="sign-up-btn">Sign up</button>
</div>
<img src={img1} class="image" alt="" />
</div>
<div class="panel right-panel">
<div class="content">
<h3>One of our faClub ?</h3>
<p>To keep connected with us please login with your personal info.</p>
<button class="btn transparent" id="sign-in-btn">Sign in</button>
</div>
<img src={img2} class="image" alt="" />
</div>
</div>
</div>
)
}
export default SignIn;
import './App.css';
import Signup from './pages/signup.js';
import SignIn from './pages/login.js';
import img1 from '../src/pages/students.png';
import img2 from '../src/pages/students2.png';
import { useState } from 'react';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
function App() {
const [isSignUpMode, setIsSignUpMode] = useState(false);
const toggleMode = () => {
setIsSignUpMode(prevMode => !prevMode);
};
return (
<div className={`container ${isSignUpMode ? 'sign-up-mode' : ''}`}>
<div className="forms-container">
{isSignUpMode ? <Signup /> : <SignIn />}
</div>
<div className="panels-container">
<div className="panel left-panel">
<div className="content">
<h3>New in our faClub ?</h3>
<p>Enter your personal details and start journey with us.</p>
<button className="btn transparent" onClick={toggleMode}>Sign up</button>
</div>
<img src={img1} className="image" alt="" />
</div>
<div className="panel right-panel">
<div className="content">
<h3>One of our faClub ?</h3>
<p>To keep connected with us please login with your personal info.</p>
<button className="btn transparent" onClick={toggleMode}>Sign in</button>
</div>
<img src={img2} className="image" alt="" />
</div>
</div>
</div>
)}
export default App;
import express from 'express';
import { signup } from '../controllers/user.controller.js';
import { signin } from '../controllers/user.controller.js';
const router = express.Router();
router.post('/signup' ,signup );
router.post('/login',signin);
export {router};
the there is no problem in the App.css