|
1 |
| -import React, { useRef } from "react"; |
| 1 | +import React, { useEffect, useState } from "react"; |
2 | 2 | import { InputItem, SubmitButton} from "../components/form";
|
3 | 3 | import styles from './css/signup.module.css'
|
4 | 4 | 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"; |
6 | 9 |
|
| 10 | +const provider = new GoogleAuthProvider(); |
7 | 11 | function SignUpPage(){
|
8 | 12 |
|
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 | + |
13 | 25 | function onClickHandler(e){
|
14 | 26 | 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( |
18 | 29 | (userCred)=>{
|
19 |
| - const user = userCred.user; |
20 |
| - console.log(user); |
| 30 | + setUser(true); |
21 | 31 | }
|
22 | 32 | ).catch(e=>{
|
23 |
| - console.error(e); |
| 33 | + console.log(e.message); |
| 34 | + setError(true); |
24 | 35 | })
|
25 | 36 |
|
26 | 37 | }
|
27 | 38 |
|
| 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 | + |
28 | 83 | return (
|
29 | 84 | <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}/>} |
38 | 97 |
|
| 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> |
39 | 121 | </div>
|
40 | 122 | </div>
|
41 | 123 | );
|
|
0 commit comments