-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSignInModal.js
More file actions
163 lines (136 loc) · 4.85 KB
/
SignInModal.js
File metadata and controls
163 lines (136 loc) · 4.85 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import React, { useState, useRef, useEffect, useCallback } from 'react';
import useForm from '../../../../utils/useForm';
import validate from '../../../../utils/validateInfo'
import '../../../../styles/SignInModal.css'
import { useSpring, animated } from 'react-spring';
import { MdClose } from 'react-icons/md';
import GoogleLogin from 'react-google-login';
// import * as db from '../utils/dbUtils.js';
import sha256 from 'crypto-js/sha256';
import * as api from '../../../../utils/apiUtils.js'
/**
* @module SignInModal
* @param {Function} onLogin
* @param {Boolean} showSignIn
* @param {Function} setShowSignIn
* @description Popup modal that displays sign in instructions
* @returns Component to be displayed
* @example
* <SignInModal onLogin={onLogin} showSignIn={showSignIn} setShowSignIn={setShowSignIn} />
*/
const SignInModal = ({ onLogin, showSignIn, setShowSignIn }) => {
const modalRef = useRef();
const [showSignUp, setShowSignUp] = useState(false);
/**
* @function login
* @description Api call to log in and passes result to onLogin()
*/
// const login = (email) => {
// onLogin(api.callLogin(email));
// }
/**
* @function register
* @description Api call to register new account and passes result to onLogin()
*/
const register = () => {
onLogin(api.callRegisterAccount(values.email, values.username, values.password));
}
/**
@function HashedEmail
@description Api call to log in but the email is HASHED and passes result to onLogin(). THIS IS A WORKING METHOD TO BE USED IN THE FUTURE
*/
const login = (email, photo) => {
var hash = sha256(email)
console.log(hash.toString() + " " + hash.toString().length)
onLogin(api.callLogin(hash.toString(), photo));
}
/**
* @function submitForm
* @description Method that is activated on signin/register button press, login or registers account
*/
function submitForm() {
if (!showSignUp) {
console.log("Login Pressed");
login();
} else if (showSignUp) {
console.log("SignUp Pressed");
register();
}
setShowSignIn(false);
values.email = '';
values.username = '';
values.password = '';
values.password2 = '';
}
const { handleChange, handleSubmit, values, errors } = useForm(
submitForm,
validate
);
/**
* @function openSignUp
* @description Flips boolean value to show signup insead of register
*/
const openSignUp = () => {
setShowSignUp(prev => !prev);
};
/**
* @function closeModal
* @param {Event} e
* @description Closes current modalRef
*/
const closeModal = e => {
if (modalRef.current === e.target) {
setShowSignIn(false);
}
};
const keyPress = useCallback(
e => {
if (e.key === 'Escape' && showSignIn) {
setShowSignIn(false);
console.log('I pressed');
}
},
[setShowSignIn, showSignIn]
);
useEffect(
() => {
document.addEventListener('keydown', keyPress);
// db.initCon();
return () => document.removeEventListener('keydown', keyPress);
},
[keyPress]
);
const responseGoogle = response => {
console.log(response);
console.log(response.profileObj.imageUrl)
console.log(response.profileObj.email)
login(response.profileObj.email, response.profileObj.imageUrl)
//after login in on google login, we call login
};
return (
<>
{showSignIn ? (
<div className='Background' onClick={closeModal} ref={modalRef}>
<animated.div >
<div className='ModalWrapper' showSignIn={showSignIn}>
<MdClose
aria-label='Close modal'
className='close-modal'
onClick={() => setShowSignIn(prev => !prev)}
/>
<div className='ModalContent'>
<GoogleLogin
clientId="568691465172-6a0kbo3t147jc4oi2bfomq8fjcq6cj2k.apps.googleusercontent.com"
buttonText="Login with Google"
onSuccess={responseGoogle}
onFailure={responseGoogle}
cookiePolicy="single_host_origin" />
</div>
</div>
</animated.div>
</div>
) : null}
</>
);
}
export default SignInModal