File size: 4,108 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import React from 'react'
import './Login.css'
import { Link, useHistory } from 'react-router-dom'
import { provider, auth } from '../../firebase/firebaseConfig'
import { useState } from 'react'
import { toast, ToastContainer } from 'react-toastify';

function Login() {

    const history = useHistory()
    const [email, setEmail] = useState('')
    const [password, setPassword] = useState('')

    const login = (e) => {
        e.preventDefault() //stops refresh
        auth
            .signInWithEmailAndPassword(email, password)
            .then((auth) => {
                // redirect to homepage
                toast.success(`Login Successful, redirecting to homepage`);
                setTimeout(function () { history.push("/") }, 1000);
            })
            .catch(e => {
                toast.error(`unable to login, please check your credentials`);
                setTimeout(function () { history.push("/login") }, 4000);
                console.warn(e.message)
            })
    }

    const register = (e) => {
        e.preventDefault() //stops refresh
        auth
            .createUserWithEmailAndPassword(email, password)
            .then((auth) => {
                // create and redirect to homepage
                toast.success(`Registration Successful, redirecting to homepage`);
                setTimeout(function () { history.push("/") }, 1000);
            })
            .catch(e => {
                toast.error(`unable to register, please check your credentials`);
                setTimeout(function () { history.push("/login") }, 4000);
                console.warn(e.message)
            })

    }

    const loginWithGoogle = (e) => {
        e.preventDefault() //stops refresh

        auth.signInWithPopup(provider)
            .then(function (result) {
                // redirect to homepage
                toast.success(`Login Successful, redirecting to homepage`);
                setTimeout(function () { history.push("/") }, 1000);
            })
            .catch(e => {
                toast.error(`unable to login, please check your credentials`);
                setTimeout(function () { history.push("/login") }, 4000);
                console.warn(e.message)
            });
    }

    return (
        <div className="login">
            <ToastContainer />
            <Link to="/">
                <img
                    className="login__logo"
                    src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a9/Amazon_logo.svg/1024px-Amazon_logo.svg.png"
                    alt="" />
            </Link>

            <div className="login__container">
                <h1>Sign In</h1>
                <form>
                    <h5>Email</h5>
                    <input
                        type="email"
                        value={email}
                        onChange={e => setEmail(e.target.value)}
                    />
                    <h5>password</h5>
                    <input
                        type="password"
                        value={password}
                        onChange={e => setPassword(e.target.value)}
                    />
                    <button onClick={login} className="login__signInButton">Sign In</button>
                </form>

                <p>
                    By continuing, you agree to Amazon's Conditions of Use and Privacy Notice.
                </p>

                <button onClick={register} className="login__registerButton">Create your amazon account</button>

                <br />
                <p style={{ textAlign: "center" }}>
                    Login With :
                </p>

                <a
                    className="login__signInGoogleButton"
                    onClick={loginWithGoogle}
                >
                    <img
                        className="login__signInGoogleIcon"
                        src="https://elearnam.com/assets/frontend/elegant/images/gicon.png"
                        alt="google login" />
                </a>
            </div>
        </div>
    )
}


export default Login