Skip to content

Commit

Permalink
Merge pull request #61 from Rizvi-Faiz/pass-regex
Browse files Browse the repository at this point in the history
Added Password regex
  • Loading branch information
SanchitGeez authored Oct 21, 2024
2 parents 3780953 + e185d1e commit c19b92d
Showing 1 changed file with 133 additions and 131 deletions.
264 changes: 133 additions & 131 deletions client/app/page.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use client'
import React from 'react'
import Link from 'next/link'
import React, { useState } from 'react';
import Link from 'next/link';
import Router from 'next/router';
import { EyeNoneIcon, EyeOpenIcon } from '@radix-ui/react-icons'

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 'react-toastify/dist/ReactToastify.css';
Expand All @@ -16,20 +14,22 @@ const page = () => {
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=()=>{
const [showPassword, setshowPassword] = useState(false);

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

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

const getCookieValue = (name) => {
Expand All @@ -65,79 +64,81 @@ const page = () => {
setSignupHeight('100%');
setShowLoginForm(false);
setShowSignupForm(true);
};


const validatePassword = (password) => {
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
return passwordRegex.test(password);
};

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;
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=/";
console.log(getCookieValue('jwt'));
router.push('/Dash');
}
}
} catch (error) {
notify(error.response.data);
console.log(error);
}
};

const handleSubmit2 = (e) => {
e.preventDefault();
e.preventDefault();
if (validatePassword(newUser.password)) {
signupUser();
}
} else {
notify("Password must be at least 8 characters long, contain one uppercase letter, one lowercase letter, one number, and one special character.");
}
};

const handleChange2 = (e) => {
const { name, value } = e.target;
setnewUser({
...newUser,
[name]: value,
});
}
const signupUser = async() => {
};

const signupUser = async () => {
try {
//const res = await axios.post("http://localhost:4000/signup", newUser);
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=/";
notify("User added successfully. Login to Continue");

//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 @@ -149,37 +150,37 @@ const page = () => {
draggable
pauseOnHover
theme="dark"
/>
/>
<div className="bgcontainer">
<div className="titlecard">
Investra
<br></br>
<p className='text-11xl'>
-- a place to grow --
-- 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', pointerEvents: showLoginForm ? 'auto' : 'none'}}>
<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={showPassword ? 'text' : '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 @@ -189,44 +190,46 @@ 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:'20px',padding:'10px',color:'#94ffae'}} 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={{marginTop:0, paddingTop:5, opacity: showSignupForm ? 1 : 1, transition: 'all 0.5s ease',}}>Sign up</div>
<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='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='email'
type="email"
name="email"
onChange={handleChange2}
/>
<input
className='signup-text-field'
placeholder='password'
type={showPassword ? 'text' : 'password'}
name="password"
onChange={handleChange2}
/>
<span
className='eye-icon'
onClick={handleeyeclick}
style={{
Expand All @@ -236,16 +239,16 @@ 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>
Expand All @@ -254,5 +257,4 @@ const page = () => {
);
};


export default page
export default page;

0 comments on commit c19b92d

Please sign in to comment.