From 72cf36e033ba794db7982befa45f035b62fa6cd2 Mon Sep 17 00:00:00 2001 From: "Alberto Duarte (PWC)" Date: Mon, 9 Oct 2023 17:32:25 +0100 Subject: Changes --- app/login/page.tsx | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 app/login/page.tsx (limited to 'app/login') diff --git a/app/login/page.tsx b/app/login/page.tsx new file mode 100644 index 0000000..35ed0aa --- /dev/null +++ b/app/login/page.tsx @@ -0,0 +1,125 @@ +'use client'; +import { useState, useEffect, useContext } from 'react'; +import { signInWithEmailAndPassword } from 'firebase/auth'; +import { auth } from '../../services/firebase'; +import Link from 'next/link'; +import { useRouter } from 'next/navigation'; +import { AuthContext } from '../components/AuthContext' + + +const Login = () => { + const [email, setEmail] = useState(''); + const [password, setPassword] = useState(''); + const [isValidEmail, setIsValidEmail] = useState(false); + const [isValidPassword, setIsValidPassword] = useState(false); + const [isPasswordVisible, setIsPasswordVisible] = useState(true); + const router = useRouter() + const user = useContext(AuthContext); + + useEffect(() => { + // Check if user is authenticated + if (!user) { + // Redirect or perform any necessary action + } else { + // User is authenticated, continue with desired logic + router.push('/profiles'); + } + }, [user]); + + useEffect(() => { + debounce(emailValidation()); + }, [email]); + + useEffect(() => { + debounce(passwordValidation()); + }, [password]); + + const emailValidation = () => { + const emailRegex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; + if (!email || emailRegex.test(email) === false) { + setIsValidEmail(false); + return false; + } + setIsValidEmail(true); + return true; + }; + + const passwordValidation = () => { + const passwordRegex = /^(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d!@#$%^&*()_+]{8,}$/i; + + if (!password || passwordRegex.test(password) === false) { + setIsValidPassword(false) + return false + } + setIsValidPassword(true) + return true + } + + const debounce = fn => { + let id = null; + + return (...args) => { + if (id) { + clearTimeout(id); + } + id = setTimeout(() => { + fn(...args); + id = null; + }, 300); + }; + }; + + const loginUser = () => { + if (isValidEmail && isValidPassword) { + signInWithEmailAndPassword(auth, email, password) + .then(data => {console.log(data.user) + + console.log('User signed in successfully!'); + router.push('/profiles'); + + }) + // Navigate to the home screen or other desired screen + .catch(error => { + console.error(error); + // Display an error message to the user + }); + } + }; + + return ( +
+

Login

+
{ + e.preventDefault(); + loginUser(); + }}> +
+ + setEmail(e.target.value)} /> +
+
+ + setPassword(e.target.value)} /> +
+ +
+ +
+ Don't have an account?{' '} + +

Create account

+ +
+ +
+ Forgot your password?{' '} + +

Recover

+ +
+
+ ); +} +export default Login; \ No newline at end of file -- cgit v1.2.3-54-g00ecf