1
1
import React from 'react'
2
2
import { navigate } from '@reach/router'
3
- import { signup } from '../../helpers/api'
3
+ import { signup , googleLoginSignup } from '../../helpers/api'
4
4
import { setAuthToken } from '../../helpers/storage'
5
5
import './loginsignup.css'
6
6
import Logo from '../../images/Keyboard.png'
7
7
import Para from './bgpara'
8
+ import { gapi } from 'gapi-script'
8
9
9
10
export default class Login extends React . Component {
10
11
constructor ( props ) {
@@ -18,7 +19,9 @@ export default class Login extends React.Component {
18
19
password : '' ,
19
20
email : '' ,
20
21
} ,
22
+ manualSignup : true ,
21
23
}
24
+ this . user_id = null
22
25
}
23
26
24
27
handleInput = ( stateName ) => ( e ) => {
@@ -41,7 +44,6 @@ export default class Login extends React.Component {
41
44
42
45
submitForm = async ( event ) => {
43
46
event . preventDefault ( )
44
-
45
47
const { username : name , password : pwd , email } = this . state
46
48
47
49
if ( ! pwd && ! name ) {
@@ -115,6 +117,70 @@ export default class Login extends React.Component {
115
117
}
116
118
}
117
119
120
+ onSignIn = ( ) => {
121
+ // Useful data for your client-side scripts:
122
+
123
+ gapi . load ( 'auth2' , ( ) => {
124
+ var auth2 = gapi . auth2 . init ( {
125
+ client_id : process . env . REACT_CLIENT_ID ,
126
+ } )
127
+
128
+ // Sign the user in, and then retrieve their ID.
129
+ auth2 . signIn ( ) . then ( ( googleUser ) => {
130
+ console . log ( 'chcek' )
131
+ console . log ( googleUser )
132
+ this . displayGoogelUser ( googleUser )
133
+ } )
134
+ } )
135
+ }
136
+
137
+ displayGoogelUser = async ( googleUser ) => {
138
+ if ( googleUser ) {
139
+ var profile = googleUser . getBasicProfile ( )
140
+ console . log ( 'ID: ' + profile . getId ( ) ) // Don't send this directly to your server!
141
+ console . log ( 'Full Name: ' + profile . getName ( ) )
142
+ console . log ( 'Given Name: ' + profile . getGivenName ( ) )
143
+ console . log ( 'Family Name: ' + profile . getFamilyName ( ) )
144
+ console . log ( 'Image URL: ' + profile . getImageUrl ( ) )
145
+ console . log ( 'Email: ' + profile . getEmail ( ) )
146
+ var id_token = googleUser . getAuthResponse ( ) . id_token
147
+ if ( id_token ) {
148
+ this . setState ( {
149
+ manualSignup : false ,
150
+ email : profile . getEmail ( ) ,
151
+ errors : {
152
+ username : 'Please fill unique username' ,
153
+ } ,
154
+ } )
155
+ this . user_id = id_token
156
+ }
157
+ }
158
+ }
159
+
160
+ submitUsername = async ( event ) => {
161
+ event . preventDefault ( )
162
+ const { username : name } = this . state
163
+ if ( ! name ) {
164
+ this . setState ( {
165
+ errors : {
166
+ username : 'Please fill the empty field' ,
167
+ } ,
168
+ } )
169
+ } else {
170
+ let obj = {
171
+ username : name ,
172
+ token : this . user_id ,
173
+ }
174
+ try {
175
+ const response = await googleLoginSignup ( obj )
176
+ setAuthToken ( response . data . token )
177
+ navigate ( '/' )
178
+ } catch ( e ) {
179
+ console . log ( e . response . data . error )
180
+ }
181
+ }
182
+ }
183
+
118
184
render ( ) {
119
185
return (
120
186
< div className = "login" >
@@ -150,6 +216,7 @@ export default class Login extends React.Component {
150
216
</ h6 >
151
217
}
152
218
</ div >
219
+
153
220
< div className = "detailsdiv" style = { { marginTop : '7%' } } >
154
221
< label className = "label" > Email</ label >
155
222
< br > </ br >
@@ -161,6 +228,7 @@ export default class Login extends React.Component {
161
228
type = "text"
162
229
placeholder = "Enter email"
163
230
onChange = { this . handleInput ( 'email' ) }
231
+ value = { this . state . email }
164
232
/>
165
233
{
166
234
< h6
@@ -169,34 +237,50 @@ export default class Login extends React.Component {
169
237
</ h6 >
170
238
}
171
239
</ div >
240
+ { this . state . manualSignup && (
241
+ < div className = "detailsdiv" style = { { marginTop : '7%' } } >
242
+ < label className = "label" > Password</ label >
172
243
173
- < div className = "detailsdiv" style = { { marginTop : '7%' } } >
174
- < label className = "label" > Password</ label >
244
+ < br > </ br >
175
245
176
- < br > </ br >
177
-
178
- < input
179
- className = {
180
- this . state . errors . password
181
- ? ' txtbox txtboxRed'
182
- : 'txtbox'
246
+ < input
247
+ className = {
248
+ this . state . errors . password
249
+ ? ' txtbox txtboxRed'
250
+ : 'txtbox'
251
+ }
252
+ type = "password"
253
+ placeholder = "Password"
254
+ onChange = { this . handleInput ( 'password' ) }
255
+ />
256
+ {
257
+ < h6
258
+ style = { {
259
+ color : 'red' ,
260
+ fontSize : '16px' ,
261
+ margin : '5px' ,
262
+ } } >
263
+ { this . state . errors . password }
264
+ </ h6 >
183
265
}
184
- type = "password"
185
- placeholder = "Password"
186
- onChange = { this . handleInput ( 'password' ) }
187
- />
188
- {
189
- < h6
190
- style = { { color : 'red' , fontSize : '16px' , margin : '5px' } } >
191
- { this . state . errors . password }
192
- </ h6 >
193
- }
194
- </ div >
266
+ </ div >
267
+ ) }
195
268
</ form >
196
- < div style = { { display : "flex" , justifyContent : "center" } } >
197
- < button className = "loginBtn" type = "submit" onClick = { this . submitForm } >
269
+ < div >
270
+ < button
271
+ className = "loginBtn"
272
+ type = "submit"
273
+ onClick = {
274
+ Boolean ( this . state . manualSignup )
275
+ ? this . submitForm
276
+ : this . submitUsername
277
+ } >
198
278
Sign Up
199
279
</ button >
280
+ < div
281
+ className = "g-signin2"
282
+ onClick = { this . onSignIn }
283
+ data-theme = "dark" > </ div >
200
284
</ div >
201
285
</ div >
202
286
</ div >
0 commit comments