Skip to content

Commit 7da7464

Browse files
committed
added signup with google feature
1 parent 483c091 commit 7da7464

File tree

1 file changed

+102
-20
lines changed

1 file changed

+102
-20
lines changed

frontend/src/pages/signup.jsx

Lines changed: 102 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,123 @@
1-
import React, { useRef } from "react";
1+
import React, { useEffect, useState } from "react";
22
import { InputItem, SubmitButton} from "../components/form";
33
import styles from './css/signup.module.css'
44
import { auth } from "../config/firebaseConfig";
5-
import { createUserWithEmailAndPassword } from "firebase/auth";
5+
import { createUserWithEmailAndPassword, signInWithPopup } from "firebase/auth";
6+
import { Navigate, Link } from "react-router-dom";
7+
import {FcGoogle} from 'react-icons/fc'
8+
import { GoogleAuthProvider } from "firebase/auth";
69

10+
const provider = new GoogleAuthProvider();
711
function SignUpPage(){
812

9-
const emailRef = useRef();
10-
const passwordRef = useRef();
11-
// const Age = useRef();
12-
13+
const [email, setEmail] = useState('');
14+
const [password, setPassword] = useState('');
15+
const [confirmPass, setConfirmPass] = useState('');
16+
const [btn, setBtn] = useState(false);
17+
18+
const [loading, setLoading] = useState(true);
19+
const [User, setUser] = useState(false);
20+
const [error, setError] = useState(false);
21+
const validEmail = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/
22+
let checkEmail = email.match(validEmail);
23+
24+
1325
function onClickHandler(e){
1426
e.preventDefault();
15-
const email = emailRef.current.value;
16-
const passWord = emailRef.current.value;
17-
createUserWithEmailAndPassword(auth, email, passWord).then(
27+
28+
createUserWithEmailAndPassword(auth, email, password).then(
1829
(userCred)=>{
19-
const user = userCred.user;
20-
console.log(user);
30+
setUser(true);
2131
}
2232
).catch(e=>{
23-
console.error(e);
33+
console.log(e.message);
34+
setError(true);
2435
})
2536

2637
}
2738

39+
function googleSignup(e){
40+
e.preventDefault();
41+
signInWithPopup(auth, provider).then(userCred=>{
42+
setUser(true);
43+
}).catch(e=>{
44+
setUser(false)
45+
})
46+
}
47+
48+
useEffect(()=>{
49+
auth.onAuthStateChanged(user=>{
50+
if(!user){
51+
setUser(false);
52+
}
53+
else{
54+
setUser(true);
55+
}
56+
setLoading(false);
57+
})
58+
},[])
59+
60+
useEffect(()=>{
61+
setTimeout(()=>{
62+
setError(false);
63+
}, 3000)
64+
}, [error])
65+
66+
useEffect(()=>{
67+
function validateDetails(){
68+
69+
if(checkEmail && password.length> 6 && password === confirmPass) setBtn(true);
70+
else setBtn(false);
71+
72+
}
73+
74+
validateDetails()
75+
76+
}, [email, password, confirmPass, checkEmail]);
77+
78+
79+
80+
81+
if(loading) return (<h1>Loading...</h1>);
82+
2883
return (
2984
<div className={styles.container}>
30-
<h1>Signup</h1>
31-
<div className={styles.formControl}>
32-
<form action="" method="">
33-
<InputItem type={"email"} Lable={"email"} Ref={emailRef}></InputItem>
34-
<InputItem type={"password"} Lable={"password"} Ref={passwordRef}></InputItem>
35-
<InputItem type={"number"} Lable={"age"}></InputItem>
36-
<SubmitButton text={"submit"} onClickHandler={onClickHandler}></SubmitButton>
37-
</form>
85+
<div>
86+
87+
<h1>Signup</h1>
88+
<div className={styles.Errors}>
89+
{error && <h4 className={styles.error}>can not signup</h4>}
90+
{ (!checkEmail && email.length>5) && <h4 className={styles.error}>email is not valid</h4>}
91+
{ (password.length < 6 && password.length > 0) && <h4 className={styles.error}>password at least contain 6 characters</h4>}
92+
{((password !== confirmPass) && confirmPass.length>0) && <h4 className={styles.error}>it is not similar to the password</h4>}
93+
94+
</div>
95+
96+
{User && <Navigate to="/" replace={true}/>}
3897

98+
<div className={styles.formControl}>
99+
<form action="" method="">
100+
<InputItem type={"email"} Lable={"email"} val={email} setVal={setEmail}></InputItem>
101+
102+
<InputItem type={"password"} Lable={"password"} val={password} setVal={setPassword}></InputItem>
103+
104+
<InputItem type={"password"} Lable={"confirm password"} val={confirmPass} setVal={setConfirmPass}></InputItem>
105+
106+
<SubmitButton active={btn} text={"submit"} onClickHandler={onClickHandler}></SubmitButton>
107+
</form>
108+
</div>
109+
<form action="" method="" className={styles.Google}>
110+
<p>or</p>
111+
<button className={styles.GoogleBtn} onClick={googleSignup}>
112+
<FcGoogle className={styles.GoogleImg} />
113+
<div className={styles.GoogleText}>Signup with Google</div>
114+
</button>
115+
</form>
116+
117+
<div className={styles.line}></div>
118+
<div>
119+
<p className={styles.center}>Already have an Account: <Link to='/login'>Log in</Link></p>
120+
</div>
39121
</div>
40122
</div>
41123
);

0 commit comments

Comments
 (0)