Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email Regex #60

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
258 changes: 125 additions & 133 deletions client/app/page.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
'use client'
import React from 'react'
import Link from 'next/link'
import Router from 'next/router';
import { EyeNoneIcon, EyeOpenIcon } from '@radix-ui/react-icons'

import { useRouter } from 'next/navigation';
import { useState } from 'react';
import axios from 'axios';
import { ToastContainer, toast } from 'react-toastify';
import { EyeNoneIcon, EyeOpenIcon } from '@radix-ui/react-icons';
import 'react-toastify/dist/ReactToastify.css';

const page = () => {
const router = useRouter();

const [loginData, setloginData] = useState({
email: '',
password: ''
})
});

const [newUser, setnewUser] = useState({
username:"",
email:"",
password:""
})
username: "",
email: "",
password: ""
});

const [loginHeight, setLoginHeight] = useState('100%');
const [signupHeight, setSignupHeight] = useState('10%');
const [showLoginForm, setShowLoginForm] = useState(true);
const [showSignupForm, setShowSignupForm] = useState(false);
const[showPassword,setshowPassword] = useState(false);
const handleeyeclick=()=>{
setshowPassword(!showPassword);
}
const [showPassword, setshowPassword] = useState(false);

const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; // Email regex pattern

const notify = (message) => toast(message, {
position: "bottom-left",
autoClose: 5000,
Expand All @@ -38,7 +38,6 @@ const page = () => {
draggable: true,
progress: undefined,
theme: "dark",
// transition: Bounce,
});

const getCookieValue = (name) => {
Expand All @@ -49,7 +48,7 @@ const page = () => {
return cookieValue;
}
}
return null; // Cookie not found
return null;
};

const handleLoginClick = () => {
Expand All @@ -64,76 +63,80 @@ const page = () => {
setSignupHeight('100%');
setShowLoginForm(false);
setShowSignupForm(true);
};


const handleeyeclick = () => {
setshowPassword(!showPassword);
};

const handleSubmit = (e) => {
e.preventDefault();
loginUser(loginData);
}
e.preventDefault();
loginUser(loginData);
};

const handleChange = (e) => {
const { name, value } = e.target;
setloginData({
...loginData,
[name]: value,
});
}
const { name, value } = e.target;
if (name === "email" && !emailRegex.test(value)) {
notify("Invalid email format");
}
setloginData({
...loginData,
[name]: value,
});
};

const loginUser = async (credentials) => {
try {
const res = await axios.post("https://investra-26xe.vercel.app/login", credentials);
// const res = await axios.post("http://localhost:4000/login", loginData)
if (res.data.message === 'Login Successful') {
const userData = JSON.stringify(res.data)
//sessionStorage.setItem('jwt', res.data.jwt);
//sessionStorage.setItem('activeUser',userData)
document.cookie = "jwt="+res.data.jwt+"; path=/";
document.cookie = "activeUser="+userData+"; path=/";
const cookies = document.cookie.split(';').map(cookie => cookie.trim());
console.log(getCookieValue('jwt'));
router.push('/Dash');
}
} catch (error) {
notify(error.response.data)
console.log(error);
try {
const res = await axios.post("https://investra-26xe.vercel.app/login", credentials);
if (res.data.message === 'Login Successful') {
const userData = JSON.stringify(res.data);
document.cookie = "jwt=" + res.data.jwt + "; path=/";
document.cookie = "activeUser=" + userData + "; path=/";
router.push('/Dash');
}
}
} catch (error) {
notify(error.response.data);
console.log(error);
}
};

const handleSubmit2 = (e) => {
e.preventDefault();
signupUser();
}
e.preventDefault();
signupUser();
};

const handleChange2 = (e) => {
const { name, value } = e.target;
if (name === "email" && !emailRegex.test(value)) {
notify("Invalid email format");
}
setnewUser({
...newUser,
[name]: value,
});
}
const signupUser = async() => {
};

const signupUser = async () => {
try {
const res = await axios.post("https://investra-26xe.vercel.app/signup", newUser);
if (res.data.message === "User added successfully") {
const userData = JSON.stringify(res.data)
document.cookie = "jwt="+res.data.jwt+"; path=/";
document.cookie = "activeUser="+userData+"; path=/";
await loginUser(userData);
}
const res = await axios.post("https://investra-26xe.vercel.app/signup", newUser);
if (res.data.message === "User added successfully") {
const userData = JSON.stringify(res.data);
document.cookie = "jwt=" + res.data.jwt + "; path=/";
document.cookie = "activeUser=" + userData + "; path=/";
await loginUser(userData);
}
} catch (error) {
console.error(error);
notify("Signup failed. Please try again.");
console.error(error);
notify("Signup failed. Please try again.");
}
}
};

const showForgetPage=() =>
{
//e.preventDefault();
const showForgetPage = () => {
router.push('/Forget-Password');
}
};

return (
<>
{/* Your existing JSX */}
<ToastContainer
position="bottom-left"
autoClose={5000}
Expand All @@ -145,35 +148,33 @@ const page = () => {
draggable
pauseOnHover
theme="dark"
/>
/>
<div className="bgcontainer">
<div className="titlecard">
Investra
<br></br>
<p className='text-11xl'>
-- a place to grow --
</p>
<br />
<p className='text-11xl'>-- a place to grow --</p>
</div>
<div className="infocard">
<div className="logincard" style={{ height: loginHeight}} onClick={handleLoginClick}>
<div style={{opacity: showLoginForm ? 1 : 0, transition: 'all 0.5s ease'}}>
<div className="logincard" style={{ height: loginHeight }} onClick={handleLoginClick}>
<div style={{ opacity: showLoginForm ? 1 : 0, transition: 'all 0.5s ease' }}>
Login
<form className="loginform" method='post' onSubmit={handleSubmit}>
<input
className='login-text-field'
placeholder='email'
type="email"
name="email"
onChange={handleChange}
/>
<input
className='login-text-field'
placeholder='password'
type="password"
name="password"
onChange={handleChange}
/>
<span
<form className="loginform" method='post' onSubmit={handleSubmit}>
<input
className='login-text-field'
placeholder='email'
type="email"
name="email"
onChange={handleChange}
/>
<input
className='login-text-field'
placeholder='password'
type="password"
name="password"
onChange={handleChange}
/>
<span
className='eye-icon'
onClick={handleeyeclick}
style={{
Expand All @@ -183,44 +184,40 @@ const page = () => {
transform: 'translateY(-50%)',
cursor: 'pointer'
}}
>
{showPassword ? <EyeNoneIcon /> : <EyeOpenIcon />}
</span>
<button
className='login-button cursor-pointer'
type="submit">
Login
</button>
</form>
<div style={{fontSize:'20px',marginTop:'10px'}} className='cursor-pointer' onClick={showForgetPage}>Forget Password</div>
</div>
>
{showPassword ? <EyeNoneIcon /> : <EyeOpenIcon />}
</span>
<button className='login-button cursor-pointer' type="submit">Login</button>
</form>
<div style={{ fontSize: '20px', marginTop: '10px' }} className='cursor-pointer' onClick={showForgetPage}>Forget Password</div>
</div>
</div>
<div className="logincard signupcard" style={{ height: signupHeight}} onClick={handleSignupClick}>
<div style={{opacity: showSignupForm ? 1 : 0, transition: 'all 0.5s ease',display:showSignupForm?'flex':'none'}}>
<form className="signupform" method='post' onSubmit={handleSubmit2}>
<div className="logincard signupcard" style={{ height: signupHeight }} onClick={handleSignupClick}>
<div style={{ opacity: showSignupForm ? 1 : 0, transition: 'all 0.5s ease', display: showSignupForm ? 'flex' : 'none' }}>
<form className="signupform" method='post' onSubmit={handleSubmit2}>
Signup
<input
className='signup-text-field'
placeholder='username'
type="text"
name="username"
onChange={handleChange2}
/>
<input
className='signup-text-field'
placeholder='email'
type="email"
name="email"
onChange={handleChange2}
/>
<input
className='signup-text-field'
placeholder='password'
type="password"
name="password"
onChange={handleChange2}
/>
<span
className='signup-text-field'
placeholder='username'
type="text"
name="username"
onChange={handleChange2}
/>
<input
className='signup-text-field'
placeholder='email'
type="email"
name="email"
onChange={handleChange2}
/>
<input
className='signup-text-field'
placeholder='password'
type="password"
name="password"
onChange={handleChange2}
/>
<span
className='eye-icon'
onClick={handleeyeclick}
style={{
Expand All @@ -230,22 +227,17 @@ const page = () => {
transform: 'translateY(-50%)',
cursor: 'pointer'
}}
>
{showPassword ? <EyeNoneIcon /> : <EyeOpenIcon />}
</span>
<button
className='signup-button cursor-pointer'
type="submit">
Signup
</button>
</form>
</div>
>
{showPassword ? <EyeNoneIcon /> : <EyeOpenIcon />}
</span>
<button className='signup-button cursor-pointer' type="submit">Signup</button>
</form>
</div>
</div>
</div>
</div>
</>
);
};


export default page
export default page;